Class: AmazonProduct::Request

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from Operations

#add_to_cart, #clear_cart, #create_cart, #find, #find_browse_node, #find_similar, #get_cart, #modify_cart, #search

Constructor Details

#initialize(locale) ⇒ Request

Creates a new request for specified locale.



45
46
47
48
# File 'lib/amazon_product/request.rb', line 45

def initialize(locale)
  @locale = Locale.new(locale.to_sym)
  @params = Hash.new
end

Class Attribute Details

.adapterObject

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

#adapterObject

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

#getObject

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

#paramsObject

The request parameters.

Raises:



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'      => timestamp,
    'Version'        => CURRENT_API_VERSION }.merge(@params)
end

#queryObject

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

#resetObject

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

Raises:



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

#timestampObject

The current timestamp.



149
150
151
# File 'lib/amazon_product/request.rb', line 149

def timestamp
  Time.now.utc.strftime('%Y-%m-%dT%H:%M:%SZ')
end

#urlObject

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