Module: Amazon::AWS

Defined in:
lib/ruby-paa/aws.rb,
lib/ruby-paa/aws/cache.rb,
lib/ruby-paa/aws/search.rb,
lib/ruby-paa/aws/shoppingcart.rb

Defined Under Namespace

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

Constant Summary collapse

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

Default Associate tags to use per locale.

{
  'ca' => 'caliban-20',
  'de' => 'calibanorg0a-21',
  'fr' => 'caliban08-21',
  'jp' => 'calibanorg-20',
  'uk' => 'caliban-21',
  'us' => 'calibanorg-20'
}
SERVICE =

Service name and API version for AWS. The version of the API used can be changed via the user configuration file.

{ 'Service' => 'AWSECommerceService',
		'Version' => '2011-08-01'
}
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 appropriate for that kind of operation.

{
  'ItemSearch'	      => { 'parameter' => 'ItemPage',
		  'max_page' => 400 },
  'ItemLookup'	      => { 'parameter' => '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 },
  'VehiclePartLookup'     => { 'parameter' => 'FitmentPage',
		  'max_page' =>  10 }
}
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' )
}
@@encodings =

A hash to store character encoding converters.

{}

Class Method Summary collapse

Class Method Details

.assemble_query(items, encoding = nil) ⇒ Object

:nodoc:



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

def AWS.assemble_query(items, encoding=nil)  # :nodoc:

  query = ''
  @@encodings[encoding] ||= Iconv.new( 'utf-8', encoding ) if encoding

  # 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 parameter values to strings, in case Symbols
  # have been used as the values.
  #
  items.sort { |a,b| a.to_s <=> b.to_s }.each do |k, v|
  	if encoding
  	  query << '&%s=%s' %
  	    [ k, Amazon.url_encode( @@encodings[encoding].iconv( v.to_s ) ) ]
  	else
  	  query << '&%s=%s' % [ k, Amazon.url_encode( v.to_s ) ]
  	end
  end

  # Replace initial ampersand with question-mark.
  #
  query[0] = '?'

  query
end

.get_page(request) ⇒ Object

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



114
115
116
117
118
119
120
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
# File 'lib/ruby-paa/aws.rb', line 114

def AWS.get_page(request)  # :nodoc:

  url = ENDPOINT[request.locale].path + request.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

  # Check whether we have a secret key available for signing the request.
  # If so, sign the request for authentication.
  #
  if request.config['secret_key_id']
  	unless request.sign
  	  Amazon.dprintf( 'Warning! Failed to sign request. No OpenSSL support for SHA256 digest.' )
  	end

  	url = ENDPOINT[request.locale].path + request.query
  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

  Amazon.dprintf( 'Fetching http://%s%s ...', conn.address, url )

  begin
   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 EOFError,		Errno::ECONNABORTED, Errno::ECONNREFUSED,
  Errno::ECONNRESET, Errno::EPIPE,	     Errno::ETIMEDOUT,
  Timeout::Error	=> error
  	Amazon.dprintf( 'Connection to server lost: %s. Retrying...', error )
  	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