zanox_api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/zanox_api.rb +102 -0
  3. metadata +58 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f12e56c8b91def1f2b4169a1c17548a904f02023
4
+ data.tar.gz: 9066f42fa81302b68363afb5c8d6e6c6e150bdf4
5
+ SHA512:
6
+ metadata.gz: 6efeb2b386fc91cc123ef6de01000b8def939e2200b9842a3ce182e2fa4486b324a179b7399b3515c56366dc659bf7cb35934a7ea4a4397c4309f7ee6b4006bd
7
+ data.tar.gz: 0c1f2d8cfadf208055af3a06516501fb2ec97fd91c9ebb584bc70549fc1c1fe2ca198277b7ab00868b41694f06690495751919307a91e605b9f3ac860b1543eb
data/lib/zanox_api.rb ADDED
@@ -0,0 +1,102 @@
1
+ require 'securerandom'
2
+ require 'savon'
3
+
4
+ module Zanox
5
+ module API
6
+ # Returns the signature for the Zanox auth
7
+ def self.signature(service_name, method_name, timestamp, nonce, secret_key)
8
+ string_to_sign = service_name + method_name + timestamp + nonce
9
+ digest = OpenSSL::Digest.new('sha1')
10
+
11
+ Base64.encode64(OpenSSL::HMAC.digest(digest, secret_key, string_to_sign))[0..-2]
12
+ end
13
+
14
+ # Build the adecuate soap parameters for a method, signing the request
15
+ # following Zanox spec
16
+ def self.params_for_method(service_name, method_name, secret_key = nil)
17
+ if secret_key
18
+ timestamp = Time.new.gmtime.strftime("%Y-%m-%dT%H:%M:%S.000Z")
19
+ nonce = SecureRandom.uuid
20
+ {
21
+ timestamp: timestamp,
22
+ nonce: nonce,
23
+ signature: signature(service_name, method_name, timestamp, nonce, secret_key),
24
+ }
25
+ else
26
+ {}
27
+ end
28
+ end
29
+
30
+ def self.normalize_response(response)
31
+ # Convert items to array
32
+ if response.has_key?(:items)
33
+ items_key = response.keys.select { |k| k.to_s.end_with?('_items') }.first
34
+ response = response[items_key]
35
+
36
+ # converts xxx_items to xxx_item
37
+ item_key = items_key.to_s[0..-2].to_sym
38
+
39
+ response = response[item_key]
40
+ end
41
+
42
+ response
43
+ end
44
+
45
+ class Client
46
+ # Service name is one of 'publisherservice', 'dataservice' or 'connectservice'
47
+ # Valid options are:
48
+ # * connect_id
49
+ # * secret_key
50
+ # * api_url (optional, defaults to the current API url: http://api.zanox.com/wsdl/2011-03-01')
51
+ # * normalize_responses: Get the responses normalized.
52
+ # This means that if the response is an array it will return just the array itself, for example
53
+ # * log, log_level, logger, pretty_print_xml: This options are passed directly to `Savon.client`
54
+ def initialize(service_name, options = {})
55
+ @service_name = service_name
56
+ @connect_id = options[:connect_id]
57
+ @secret_key = options[:secret_key]
58
+ @base_api_url = options[:api_url] || 'http://api.zanox.com/wsdl/2011-03-01'
59
+ @normalize_responses = options[:normalize_responses]
60
+
61
+ @savon_client = Savon.client({
62
+ wsdl: @base_api_url,
63
+ log: options[:log],
64
+ log_level: options[:log_level],
65
+ logger: options[:logger],
66
+ pretty_print_xml: options[:pretty_print_html],
67
+ })
68
+ end
69
+
70
+ def method_missing(name, *args, &block)
71
+ params = Zanox::API.params_for_method(@service_name, name.to_s.gsub('_', '').downcase, @secret_key)
72
+ params.merge!(connect_id: @connect_id, date: Date.today.to_s)
73
+ params.merge!(args[0] || {})
74
+ response = @savon_client.call(name.to_sym, message: params)
75
+ contents = response.body[(name.to_s + '_response').to_sym]
76
+ if @normalize_responses
77
+ Zanox::API.normalize_response(contents)
78
+ else
79
+ contents
80
+ end
81
+ end
82
+ end
83
+
84
+ class PublisherClient < Client
85
+ def initialize(options = {})
86
+ super('publisherservice', options)
87
+ end
88
+ end
89
+
90
+ class DataClient < Client
91
+ def initialize(options = {})
92
+ super('dataservice', options)
93
+ end
94
+ end
95
+
96
+ class ConnectClient < Client
97
+ def initialize(options = {})
98
+ super('connectservice', options)
99
+ end
100
+ end
101
+ end
102
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zanox_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Emilio Cobos Álvarez
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: savon
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ description: Simple client for zanox SOAP API
28
+ email: emiliocobos@usal.es
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/zanox_api.rb
34
+ homepage: https://github.com/ecoal95/zanox-api-rb
35
+ licenses:
36
+ - MIT
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 2.2.2
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: Zanox API client
58
+ test_files: []