Sha256: d1d771d8e73251f1559619729ddd3d8a9d3ff8785e8ed184415791a24d33e2d3

Contents?: true

Size: 1.8 KB

Versions: 4

Compression:

Stored size: 1.8 KB

Contents

require 'builder'

module Wework
  module Api
    class Suite < Base

      include Wework::Cipher
      include Methods::Suite

      attr_reader :encoding_aes_key, :suite_id, :suite_secret, :suite_token

      def initialize(options={})
        @suite_id = options.delete(:suite_id)
        @suite_secret = options.delete(:suite_secret)
        @suite_token = options.delete(:suite_token)
        @encoding_aes_key = options.delete(:encoding_aes_key)
        super(options)
      end

      def msg_decrypt message
        unpack(decrypt(Base64.decode64(message), encoding_aes_key))[0]
      end

      def msg_encrypt message
        Base64.strict_encode64(encrypt(pack(message, corp_id), encoding_aes_key))
      end

      def signature(timestamp, nonce, encrypt)
        array = [suite_token, timestamp, nonce]
        array << encrypt unless encrypt.nil?
        Digest::SHA1.hexdigest array.compact.collect(&:to_s).sort.join
      end

      def generate_xml(msg, timestamp, nonce)
        encrypt = msg_encrypt(msg)
        {
          Encrypt: encrypt,
          MsgSignature: signature(timestamp, nonce, encrypt),
          TimeStamp: timestamp,
          Nonce: nonce
        }.to_xml(root: 'xml', children: 'item', skip_instruct: true, skip_types: true)
      end

      def suite_ticket= ticket
        Wework.redis.set ticket_key, ticket
      end

      def suite_ticket
        Wework.redis.get ticket_key
      end

      def corp(corp_id, permanent_code)
        Wework::Api::Corp.new(suite: self, corp_id: corp_id, permanent_code: permanent_code)
      end

      private

      def token_params
        {suite_access_token: access_token}
      end

      def ticket_key
        "SUITE_TICKET_#{suite_id}"
      end

      def token_store
        @token_store ||= Token::SuiteToken.new self
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
wework-1.1.3 lib/wework/api/suite.rb
wework-1.1.2 lib/wework/api/suite.rb
wework-1.1.1 lib/wework/api/suite.rb
wework-1.1.0 lib/wework/api/suite.rb