amazonian 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/amazonian.rb +43 -22
- data/lib/amazonian/version.rb +1 -1
- metadata +3 -3
data/lib/amazonian.rb
CHANGED
@@ -1,18 +1,29 @@
|
|
1
|
+
require 'patron'
|
2
|
+
require 'crack'
|
3
|
+
require 'hashie'
|
4
|
+
|
1
5
|
module Amazonian
|
2
|
-
|
3
|
-
@@path = '/onca/xml'
|
6
|
+
#worker objects
|
4
7
|
@@digest = OpenSSL::Digest::Digest.new('sha256')
|
5
8
|
@@logger = Logger.new(STDERR)
|
6
9
|
@@patron = Patron::Session.new
|
7
|
-
|
10
|
+
|
11
|
+
#hold the most recent request/response
|
12
|
+
@@request = nil
|
13
|
+
@@response = nil
|
14
|
+
@@query = nil
|
15
|
+
|
16
|
+
#configuration variables
|
17
|
+
@@host = 'webservices.amazon.com'
|
18
|
+
@@path = '/onca/xml'
|
19
|
+
@@debug = true
|
8
20
|
@@key = ''
|
9
21
|
@@secret = ''
|
10
|
-
@@debug = false
|
11
22
|
|
12
|
-
mattr_reader :host,:path,:
|
23
|
+
mattr_reader :host,:path,:response,:request
|
13
24
|
mattr_accessor :key,:secret,:debug
|
14
25
|
|
15
|
-
#Configure
|
26
|
+
#Configure Patron
|
16
27
|
@@patron.timeout = 10
|
17
28
|
|
18
29
|
# Configure the basic request parameters for Amazonian.
|
@@ -43,21 +54,31 @@ module Amazonian
|
|
43
54
|
private
|
44
55
|
|
45
56
|
def self.call(params)
|
46
|
-
raise "
|
57
|
+
raise "Cannot call the Amazon API without key and secret key." if @@key.blank? || @@secret.blank?
|
47
58
|
|
48
59
|
#get the signed query, and assemble the querystring
|
49
60
|
log :debug, "calling with params=#{params}" if @@debug
|
50
|
-
signed = assemble_querystring params
|
51
|
-
url = "http://#{@@host}#{@@path}?#{signed}"
|
52
61
|
|
53
|
-
|
54
|
-
|
62
|
+
#memoize the last request for faster API querying...
|
63
|
+
query = assemble_querystring params
|
64
|
+
return Crack::XML.parse @@response.body if query == @@query
|
65
|
+
@@query = query
|
66
|
+
|
67
|
+
#sign the query
|
68
|
+
signed = sign_query query
|
55
69
|
|
56
|
-
#
|
57
|
-
|
70
|
+
#assemble the full URL
|
71
|
+
@@request = "http://#{@@host}#{@@path}?#{signed}"
|
58
72
|
|
73
|
+
#make the call
|
74
|
+
log :info, "performing rest call to '#{@@request}'" if @@debug
|
75
|
+
@@response = @@patron.get @@request
|
76
|
+
|
77
|
+
log :debug, "Response Code: #{@@response.status}" if @@debug
|
78
|
+
|
79
|
+
raise "Amazon API Error: #{@@response.status}" if @@response.status >= 400
|
59
80
|
#parse the response and return it
|
60
|
-
Crack::XML.parse @@
|
81
|
+
Crack::XML.parse @@response.body
|
61
82
|
end
|
62
83
|
|
63
84
|
def self.assemble_querystring(params)
|
@@ -65,20 +86,19 @@ module Amazonian
|
|
65
86
|
params[:Service] = :AWSECommerceService
|
66
87
|
params[:AWSAccessKeyId] = @@key
|
67
88
|
|
68
|
-
# UTC timestamp needed for signing
|
69
|
-
params[:Timestamp] = Time.now.utc.strftime '%Y-%m-%dT%H:%M:%SZ'
|
70
|
-
|
71
89
|
# CGI escape each param
|
72
90
|
# signing needs to order the query alphabetically
|
73
|
-
|
74
|
-
|
75
|
-
sign_query query
|
91
|
+
params.map{|key, value| "#{key}=#{CGI.escape(value.to_s)}" }.sort.join('&')
|
76
92
|
end
|
77
93
|
|
78
94
|
def self.sign_query(query)
|
95
|
+
q = query.clone
|
96
|
+
# UTC timestamp needed for signing
|
97
|
+
q <<= '&Timestamp=' << CGI.escape(Time.now.utc.strftime '%Y-%m-%dT%H:%M:%SZ')
|
98
|
+
|
79
99
|
# Sign the entire get-request (not just the querystring)
|
80
100
|
# possible gotcha if Patron starts using more/different headers.
|
81
|
-
request_to_sign = %Q{GET\n#{@@host}\n#{@@path}\n#{
|
101
|
+
request_to_sign = %Q{GET\n#{@@host}\n#{@@path}\n#{q}}
|
82
102
|
|
83
103
|
# Sign it.
|
84
104
|
hmac = OpenSSL::HMAC.digest(@@digest, @@secret, request_to_sign)
|
@@ -86,7 +106,7 @@ module Amazonian
|
|
86
106
|
# Don't forget to remove the newline from base64
|
87
107
|
signature = CGI.escape(Base64.encode64(hmac).chomp)
|
88
108
|
|
89
|
-
"#{
|
109
|
+
"#{q}&Signature=#{signature}"
|
90
110
|
end
|
91
111
|
|
92
112
|
def self.log(severity, message)
|
@@ -113,4 +133,5 @@ module Amazonian
|
|
113
133
|
end
|
114
134
|
|
115
135
|
end
|
136
|
+
|
116
137
|
end
|
data/lib/amazonian/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: amazonian
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Robert L. Carpenter
|