Module: Amazon::AWS

Defined in:
lib/amazon/aws.rb,
lib/amazon/aws/cache.rb,
lib/amazon/aws/search.rb,
lib/amazon/aws/shoppingcart.rb

Defined Under Namespace

Modules: Error, Search, ShoppingCart Classes: AWSArray, AWSObject, BrowseNodeLookup, Cache, CustomerContentLookup, CustomerContentSearch, Endpoint, HTTPError, Help, ItemLookup, ItemSearch, ListLookup, ListSearch, MultipleOperation, Operation, ResponseGroup, SellerListingLookup, SellerListingSearch, SellerLookup, SimilarityLookup, TagLookup, TransactionLookup

Constant Summary collapse

NAME =
'%s/%s' % [ Amazon::NAME, 'AWS' ]
VERSION =
'0.4.4'
USER_AGENT =
'%s %s' % [ NAME, VERSION ]
DEF_ASSOC =

Default Associate tags to use per locale.

{
  'de' => 'magistrix-21'
}
SERVICE =

Service name and version for AWS.

{ 'Service' => 'AWSECommerceService',
		'Version' => '2008-08-19'
}
MAX_REDIRECTS =

Maximum number of 301 and 302 HTTP responses to follow, should Amazon later decide to change the location of the service.

3
PAGINATION =

Maximum number of results pages that can be retrieved for a given search operation, using whichever pagination parameter is relevant to that type of operation.

{
  'ItemSearch'	      => { 'parameter' => 'ItemPage',
		  'max_page' => 400 },
  'ItemLookup'	      => { 'paraneter' => 'OfferPage',
		  'max_page' => 100 },
  'ListLookup'	      => { 'parameter' => 'ProductPage',
		  'max_page' =>  30 },
  'ListSearch'	      => { 'parameter' => 'ListPage',
		  'max_page' =>  20 },
  'CustomerContentLookup' => { 'parameter' => 'ReviewPage',
		  'max_page' =>  10 },
  'CustomerContentSearch' => { 'parameter' => 'CustomerPage',
		  'max_page' =>  20 }
}
ENDPOINT =
{
  'ca' => Endpoint.new( 'http://ecs.amazonaws.ca/onca/xml' ),
  'de' => Endpoint.new( 'http://ecs.amazonaws.de/onca/xml' ),
  'fr' => Endpoint.new( 'http://ecs.amazonaws.fr/onca/xml' ),
  'jp' => Endpoint.new( 'http://ecs.amazonaws.jp/onca/xml' ),
  'uk' => Endpoint.new( 'http://ecs.amazonaws.co.uk/onca/xml' ),
  'us' => Endpoint.new( 'http://ecs.amazonaws.com/onca/xml' )
}

Class Method Summary collapse

Class Method Details

.assemble_query(items) ⇒ Object

:nodoc:



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/amazon/aws.rb', line 189

def AWS.assemble_query(items)  # :nodoc:
  query = ''
  
  # the new signature also needs a timestamp / mod netjungle
  
  items[:Timestamp] = DateTime.now.new_offset.strftime('%Y-%m-%dT%XZ')
  
  # We must sort the items into an array to get reproducible ordering
  # of the query parameters. Otherwise, URL caching would not work. We
  # must also convert the keys to strings, in case Symbols have been used
  # as the keys.
  #
  items.sort { |a,b| a.to_s <=> b.to_s }.each do |k, v|
    query << '&%s=%s' % [ k, Amazon.url_encode( v.to_s ) ]
  end
  
  # Replace initial ampersand with question-mark.
  #
  query[0] = '?'
  
  query
end

.get_page(request, query) ⇒ Object

Fetch a page, either from the cache or by HTTP. This is used internally.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/amazon/aws.rb', line 121

def AWS.get_page(request, query)  # :nodoc:
  
  url = ENDPOINT[request.locale].path + query
  cache_url = ENDPOINT[request.locale].host + url
  
  
  # Check for cached page and return that if it's there.
  #
  if request.cache && request.cache.cached?( cache_url )
    body = request.cache.fetch( cache_url )
    return body if body
  end
  
  # Get the existing connection. If there isn't one, force a new one.
  #
  conn = request.conn || request.reconnect.conn
  user_agent = request.user_agent
  
  
  
  begin
    url = ENDPOINT[request.locale].path + query + "&Signature=" + AWS.signature_for_request(request, query)
    
    
    
    Amazon.dprintf( 'Fetching http://%s%s ...', conn.address, url )  
    
    
    response = conn.get( url, { 'user-agent' => user_agent } )
    
    # If we've pulled and processed a lot of pages from the cache (or
    # just not passed by here recently), the HTTP connection to the server
    # will probably have timed out.
    #
  rescue Errno::ECONNRESET
    conn = request.reconnect.conn
    retry
  end
  
  redirects = 0
  while response.key? 'location'
    if ( redirects += 1 ) > MAX_REDIRECTS
      raise HTTPError, "More than #{MAX_REDIRECTS} redirections"
    end
    
    old_url = url
    url = URI.parse( response['location'] )
    url.scheme = old_url.scheme unless url.scheme
    url.host = old_url.host unless url.host
    Amazon.dprintf( 'Following HTTP %s to %s ...', response.code, url )
    response = Net::HTTP::start( url.host ).
    get( url.path, { 'user-agent' => user_agent } )
  end
  
  if response.code != '200'
    raise HTTPError, "HTTP response code #{response.code}"
  end
  
  # Cache the page if we're using a cache.
  #
  if request.cache
    request.cache.store( cache_url, response.body )
  end
  
  response.body
end

.signature_for_request(request, query, method = 'GET') ⇒ Object



108
109
110
111
112
113
114
115
116
117
# File 'lib/amazon/aws.rb', line 108

def self.signature_for_request(request, query, method = 'GET' )
  endpoint = ENDPOINT[request.locale]
  host = endpoint.host
  uri  = endpoint.path
  query.gsub!('?', '')      
  raw_signature = "#{method}\n#{host}\n#{uri}\n#{query}"      
  hash = HMAC::sha256(request.secret_id, raw_signature)
  signature = Base64.encode64(hash).chomp
  Amazon.rawurlencode signature
end