Module: Zanox::API

Defined in:
lib/zanox_api.rb

Defined Under Namespace

Classes: Client, ConnectClient, DataClient, PublisherClient

Class Method Summary collapse

Class Method Details

.normalize_response(response) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/zanox_api.rb', line 30

def self.normalize_response(response)
  # Convert items to array
  if response.has_key?(:items)
    items = response[:items].to_i
    if items == 0
      response = []
    else
      items_key = response.keys.select { |k| k.to_s.end_with?('_items') }.first
      response = response[items_key]

      # converts xxx_items to xxx_item
      item_key = items_key.to_s[0..-2].to_sym

      response = response[item_key]

      if items == 1
        response = [response]
      end
    end
  end

  response
end

.params_for_method(service_name, method_name, secret_key = nil) ⇒ Object

Build the adecuate soap parameters for a method, signing the request following Zanox spec



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/zanox_api.rb', line 16

def self.params_for_method(service_name, method_name, secret_key = nil)
  if secret_key
    timestamp = Time.new.gmtime.strftime("%Y-%m-%dT%H:%M:%S.000Z")
    nonce = SecureRandom.uuid
    {
      timestamp: timestamp,
      nonce: nonce,
      signature: signature(service_name, method_name, timestamp, nonce, secret_key),
    }
  else
    {}
  end
end

.signature(service_name, method_name, timestamp, nonce, secret_key) ⇒ Object

Returns the signature for the Zanox auth



7
8
9
10
11
12
# File 'lib/zanox_api.rb', line 7

def self.signature(service_name, method_name, timestamp, nonce, secret_key)
  string_to_sign = service_name + method_name + timestamp + nonce
  digest = OpenSSL::Digest.new('sha1')

  Base64.encode64(OpenSSL::HMAC.digest(digest, secret_key, string_to_sign))[0..-2]
end