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.3'
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 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:



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/amazon/aws.rb', line 151

def AWS.assemble_query(items)  # :nodoc:
  query = ''

  # 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.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
# File 'lib/amazon/aws.rb', line 91

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

  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 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