Class: AmazonProduct::Request
- Inherits:
-
Object
- Object
- AmazonProduct::Request
- Includes:
- Operations
- Defined in:
- lib/amazon_product/request.rb
Overview
A wrapper around the API request.
Constant Summary collapse
- CURRENT_API_VERSION =
The latest Amazon API version. See: aws.amazon.com/archives/Product%20Advertising%20API
'2011-08-01'
Class Attribute Summary collapse
-
.adapter ⇒ Object
The HTTP client.
Instance Method Summary collapse
-
#<<(hash) ⇒ Object
Merges a hash of request parameters into the query.
-
#adapter ⇒ Object
The HTTP client.
-
#aget(&block) ⇒ Object
Performs an asynchronous request with the EM async HTTP client.
-
#configure(&block) ⇒ Object
Configures the Amazon locale.
-
#get ⇒ Object
Performs a request.
-
#initialize(locale) ⇒ Request
constructor
Creates a new request for specified locale.
-
#params ⇒ Object
The request parameters.
-
#query ⇒ Object
A string representation of the request parameters.
-
#reset ⇒ Object
Resets the request parameters.
-
#sign(unsigned_query) ⇒ Object
Adds a signature to a query.
-
#timestamp ⇒ Object
The current timestamp.
-
#url ⇒ Object
The Amazon URL.
Methods included from Operations
#add_to_cart, #clear_cart, #create_cart, #find, #find_browse_node, #find_similar, #get_cart, #modify_cart, #search
Constructor Details
Class Attribute Details
.adapter ⇒ Object
The HTTP client.
12 13 14 |
# File 'lib/amazon_product/request.rb', line 12 def adapter @adapter end |
Instance Method Details
#<<(hash) ⇒ Object
Merges a hash of request parameters into the query.
request << { :key => 'value' }
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/amazon_product/request.rb', line 54 def <<(hash) hash.each do |k, v| # Cast value to string. v = v.is_a?(Array) ? v.join(',') : v.to_s # Camelize key. k = k.to_s.split('_').map { |w| w[0, 1] = w[0, 1].upcase; w }.join @params[k] = v end end |
#adapter ⇒ Object
The HTTP client.
67 68 69 |
# File 'lib/amazon_product/request.rb', line 67 def adapter Request.adapter end |
#aget(&block) ⇒ Object
Performs an asynchronous request with the EM async HTTP client.
Yields response to given block.
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/amazon_product/request.rb', line 74 def aget(&block) unless adapter == :synchrony raise TypeError, "Set HTTP client to :synchrony" end http = EM::HttpRequest.new(url).aget resp = lambda { Response.new(http.response, http.response_header.status) } http.callback { block.call(resp.call) } http.errback { block.call(resp.call) } end |
#configure(&block) ⇒ Object
Configures the Amazon locale.
request.configure do |c|
c.key = YOUR_KEY
c.secret = YOUR_SECRET
c.tag = YOUR_ASSOCIATE_TAG
end
93 94 95 |
# File 'lib/amazon_product/request.rb', line 93 def configure(&block) block.call @locale end |
#get ⇒ Object
Performs a request.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/amazon_product/request.rb', line 98 def get case adapter when :curb http = Curl::Easy.perform(url.to_s) body, code = http.body_str, http.response_code when :synchrony http = EM::HttpRequest.new(url).get body, code = http.response, http.response_header.status when :net_http resp = Net::HTTP.get_response(url) body, code = resp.body, resp.code end Response.new(body, code) end |
#params ⇒ Object
The request parameters.
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/amazon_product/request.rb', line 115 def params raise MissingKey unless @locale.key raise MissingTag unless @locale.tag { 'AWSAccessKeyId' => @locale.key, 'AssociateTag' => @locale.tag, 'Service' => 'AWSECommerceService', 'Timestamp' => , 'Version' => CURRENT_API_VERSION }.merge(@params) end |
#query ⇒ Object
A string representation of the request parameters.
127 128 129 |
# File 'lib/amazon_product/request.rb', line 127 def query params.sort.map { |k, v| "#{k}=" + escape(v) }.join('&') end |
#reset ⇒ Object
Resets the request parameters.
132 133 134 |
# File 'lib/amazon_product/request.rb', line 132 def reset @params = Hash.new end |
#sign(unsigned_query) ⇒ Object
Adds a signature to a query
137 138 139 140 141 142 143 144 145 146 |
# File 'lib/amazon_product/request.rb', line 137 def sign(unsigned_query) raise MissingSecret unless @locale.secret digest = OpenSSL::Digest::Digest.new('sha256') url_string = ['GET', @locale.host, '/onca/xml', unsigned_query].join("\n") hmac = OpenSSL::HMAC.digest(digest, @locale.secret, url_string) signature = escape([hmac].pack('m').chomp) "#{unsigned_query}&Signature=#{signature}" end |
#timestamp ⇒ Object
The current timestamp.
149 150 151 |
# File 'lib/amazon_product/request.rb', line 149 def Time.now.utc.strftime('%Y-%m-%dT%H:%M:%SZ') end |
#url ⇒ Object
The Amazon URL.
154 155 156 157 158 |
# File 'lib/amazon_product/request.rb', line 154 def url URI::HTTP.build(:host => @locale.host, :path => '/onca/xml', :query => sign(query)) end |