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, BatchError, BrowseNodeLookup, Cache, CustomerContentLookup, CustomerContentSearch, Endpoint, HTTPError, Help, ItemLookup, ItemSearch, ListLookup, ListSearch, MultipleOperation, Operation, ResponseGroup, SellerListingLookup, SellerListingSearch, SellerLookup, SimilarityLookup, TagLookup, TransactionLookup, VehiclePartLookup, VehiclePartSearch, VehicleSearch

Constant Summary collapse

NAME =
'%s/%s' % [ Amazon::NAME, 'AWS' ]
VERSION =
'0.7.0'
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' => '2009-03-31'
}
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 },
  '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

Instance Method Summary collapse

Class Method Details

.assemble_query(items, encoding = nil) ⇒ Object

:nodoc:



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/amazon/aws.rb', line 183

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.



109
110
111
112
113
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
# File 'lib/amazon/aws.rb', line 109

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

Instance Method Details

#batch_response_groups(operation) ⇒ Object

Convert response groups to batch format, e.g. ItemSearch.1.ResponseGroup.



736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
# File 'lib/amazon/aws.rb', line 736

def batch_response_groups(operation)

  rg = {}
  op_count = Hash.new( 1 )

  [ self, operation ].each do |op|
	rg_hash = op.response_group.params

	if m = rg_hash.to_a[0][0].match( /^(.+)\..+\..+$/ )
	  # This hash is already in batch format.
	  #
	  rg.merge!( rg_hash )

	  # Keep a record of the highest index currently in use for each type
	  # of operation.
	  #
	  rg_hash.each do |key, val|
 op_kind, index = key.match( /^(.+)\.(\d+)\..+$/ )[1, 2]
 if index.to_i == op_count[op_kind]
   op_count[op_kind] += 1
 end
	  end

	else
	  # Convert hash to batch format.
	  #
	  rg_hash.each_value do |val|
 rg_str = '%s.%d.ResponseGroup' % [ op.kind, op_count[op.kind] ]
 op_count[op.kind] += 1
 rg[rg_str] = val
	  end
	end

  end

 rg 
end