Class: AmazonAssociate::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/amazon_associate/request.rb

Constant Summary collapse

SERVICE_URLS =
{:us => "http://webservices.amazon.com/onca/xml?Service=AWSECommerceService",
    :uk => "http://webservices.amazon.co.uk/onca/xml?Service=AWSECommerceService",
    :ca => "http://webservices.amazon.ca/onca/xml?Service=AWSECommerceService",
    :de => "http://webservices.amazon.de/onca/xml?Service=AWSECommerceService",
    :jp => "http://webservices.amazon.co.jp/onca/xml?Service=AWSECommerceService",
    :fr => "http://webservices.amazon.fr/onca/xml?Service=AWSECommerceService"
}
SORT_TYPES =

The sort types available to each product search index.

{
  "Apparel" => %w[relevancerank salesrank pricerank inverseprice -launch-date sale-flag],
  "Automotive" => %w[salesrank price -price titlerank -titlerank],
  "Baby" => %w[psrank salesrank price -price titlerank],
  "Beauty" => %w[pmrank salesrank price -price -launch-date sale-flag],
  "Books" => %w[relevancerank salesrank reviewrank pricerank inverse-pricerank daterank titlerank -titlerank],
  "Classical" => %w[psrank salesrank price -price titlerank -titlerank orig-rel-date],
  "DigitalMusic" => %w[songtitlerank uploaddaterank],
  "DVD" => %w[relevancerank salesrank price -price titlerank -video-release-date],
  "Electronics" => %w[pmrank salesrank reviewrank price -price titlerank],
  "GourmetFood" => %w[relevancerank salesrank pricerank inverseprice launch-date sale-flag],
  "HealthPersonalCare" => %w[pmrank salesrank pricerank inverseprice launch-date sale-flag],
  "Jewelry" => %w[pmrank salesrank pricerank inverseprice launch-date],
  "Kitchen" => %w[pmrank salesrank price -price titlerank -titlerank],
  "Magazines" => %w[subslot-salesrank reviewrank price -price daterank titlerank -titlerank],
  "Merchants" => %w[relevancerank salesrank pricerank inverseprice launch-date sale-flag],
  "Miscellaneous" => %w[pmrank salesrank price -price titlerank -titlerank],
  "Music" => %w[psrank salesrank price -price titlerank -titlerank artistrank orig-rel-date release-date],
  "MusicalInstruments" => %w[pmrank salesrank price -price -launch-date sale-flag],
  "MusicTracks" => %w[titlerank -titlerank],
  "OfficeProducts" => %w[pmrank salesrank reviewrank price -price titlerank],
  "OutdoorLiving" => %w[psrank salesrank price -price titlerank -titlerank],
  "PCHardware" => %w[psrank salesrank price -price titlerank],
  "PetSupplies" => %w[+pmrank salesrank price -price titlerank -titlerank],
  "Photo" => %w[pmrank salesrank titlerank -titlerank],
  "Restaurants" => %w[relevancerank titlerank],
  "Software" => %w[pmrank salesrank titlerank price -price],
  "SportingGoods" => %w[relevancerank salesrank pricerank inverseprice launch-date sale-flag],
  "Tools" => %w[pmrank salesrank titlerank -titlerank price -price],
  "Toys" => %w[pmrank salesrank price -price titlerank -age-min],
  "VHS" => %w[relevancerank salesrank price -price titlerank -video-release-date],
  "Video" => %w[relevancerank salesrank price -price titlerank -video-release-date],
  "VideoGames" => %w[pmrank salesrank price -price titlerank],
  "Wireless" => %w[daterank pricerank invers-pricerank reviewrank salesrank titlerank -titlerank], 
  "WirelessAccessories" => %w[psrank salesrank titlerank -titlerank]
}
@@options =
{}
@@debug =
false

Class Method Summary collapse

Class Method Details

.browse_node_lookup(browse_node_id, opts = {}) ⇒ Object

Performs BrowseNodeLookup request, defaults to TopSellers ResponseGroup



88
89
90
91
92
93
94
# File 'lib/amazon_associate/request.rb', line 88

def self.browse_node_lookup(browse_node_id, opts = {})
  opts = self.options.merge(opts) if self.options
  opts[:operation] = "BrowseNodeLookup"
  opts[:browse_node_id] = browse_node_id

  self.send_request(opts)
end

.cart_add(items, cart_id, hmac, opts = {}) ⇒ Object

Adds items to remote shopping cart



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/amazon_associate/request.rb', line 119

def self.cart_add(items, cart_id, hmac, opts = {})
  opts = self.options.merge(opts) if self.options
  opts[:operation] = "CartAdd"
  
  if items.is_a?(String)
    asin = items
    opts["Item.#{asin}.Quantity"] = opts[:quantity] || 1
    opts["Item.#{asin}.ASIN"] = asin
  else
    items.each do |item|
      (item[:offer_listing_id].nil? || item[:offer_listing_id].empty?) ? opts["Item.#{item[:asin]}.ASIN"] = item[:asin] : opts["Item.#{item[:asin]}.OfferListingId"] = item[:offer_listing_id]
      opts["Item.#{item[:asin]}.Quantity"] = item[:quantity] || 1
    end
  end
  
  opts[:cart_id] = cart_id
  opts[:hMAC] = hmac
  
  self.send_request(opts)
end

.cart_clear(cart_id, hmac, opts = {}) ⇒ Object

clears contents of remote shopping cart



165
166
167
168
169
170
171
172
# File 'lib/amazon_associate/request.rb', line 165

def self.cart_clear(cart_id, hmac, opts = {})
  opts = self.options.merge(opts) if self.options
  opts[:operation] = "CartClear"
  opts[:cart_id] = cart_id
  opts[:hMAC] = hmac
  
  self.send_request(opts)
end

.cart_create(items, opts = {}) ⇒ Object

Creates remote shopping cart containing asin



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/amazon_associate/request.rb', line 100

def self.cart_create(items, opts = {})
  opts = self.options.merge(opts) if self.options
  opts[:operation] = "CartCreate"
  
  if items.is_a?(String)
    asin = items
    opts["Item.#{asin}.Quantity"] = opts[:quantity] || 1
    opts["Item.#{asin}.ASIN"] = asin
  else
    items.each do |item|
      (item[:offer_listing_id].nil? || item[:offer_listing_id].empty?) ? opts["Item.#{item[:asin]}.ASIN"] = item[:asin] : opts["Item.#{item[:asin]}.OfferListingId"] = item[:offer_listing_id]
      opts["Item.#{item[:asin]}.Quantity"] = item[:quantity] || 1
    end
  end
  
  self.send_request(opts)
end

.cart_get(cart_id, hmac, opts = {}) ⇒ Object

Retrieve a remote shopping cart



141
142
143
144
145
146
147
148
# File 'lib/amazon_associate/request.rb', line 141

def self.cart_get(cart_id, hmac, opts = {})
  opts = self.options.merge(opts) if self.options
  opts[:operation] = "CartGet"
  opts[:cart_id] = cart_id
  opts[:hMAC] = hmac
  
  self.send_request(opts)
end

.cart_modify(cart_item_id, cart_id, hmac, quantity = 0, opts = {}) ⇒ Object

modifies cart_item_id in remote shopping cart quantity defaults to 0 to remove the given cart_item_id specify quantity to update cart contents



153
154
155
156
157
158
159
160
161
162
# File 'lib/amazon_associate/request.rb', line 153

def self.cart_modify(cart_item_id, cart_id, hmac, quantity=0, opts = {})
  opts = self.options.merge(opts) if self.options
  opts[:operation] = "CartModify"
  opts["Item.1.CartItemId"] = cart_item_id
  opts["Item.1.Quantity"] = quantity
  opts[:cart_id] = cart_id
  opts[:hMAC] = hmac
  
  self.send_request(opts)
end

.configure {|@@options| ... } ⇒ Object

Yields:

Raises:

  • (ArgumentError)


196
197
198
199
200
201
202
203
# File 'lib/amazon_associate/request.rb', line 196

def self.configure(&proc)
  raise ArgumentError, "Block is required." unless block_given?
  
  yield @@options
  if !@@options[:caching_strategy].nil?
    @@options.merge!(CacheFactory.initialize_options(@@options))
  end
end

.debugObject

Get debug flag.



187
188
189
# File 'lib/amazon_associate/request.rb', line 187

def self.debug
  @@debug
end

.debug=(dbg) ⇒ Object

Set debug flag to true or false.



192
193
194
# File 'lib/amazon_associate/request.rb', line 192

def self.debug=(dbg)
  @@debug = dbg
end

.item_lookup(item_id, opts = {}) ⇒ Object

Search an item by ASIN no.



222
223
224
225
226
227
# File 'lib/amazon_associate/request.rb', line 222

def self.item_lookup(item_id, opts = {})
  opts[:operation] = "ItemLookup"
  opts[:item_id] = item_id

  self.send_request(opts)
end

.item_search(terms, opts = {}) ⇒ Object

Search amazon items with search terms. Default search index option is “Books”. For other search type other than keywords, please specify :type => [search type param name].



207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/amazon_associate/request.rb', line 207

def self.item_search(terms, opts = {})
  opts[:operation] = "ItemSearch"
  opts[:search_index] = opts[:search_index] || "Books"

  type = opts.delete(:type)
  if type 
    opts[type.to_sym] = terms
  else 
    opts[:keywords] = terms
  end

  self.send_request(opts)
end

.optionsObject

Default search options



177
178
179
# File 'lib/amazon_associate/request.rb', line 177

def self.options
  @@options
end

.options=(opts) ⇒ Object

Set default search options



182
183
184
# File 'lib/amazon_associate/request.rb', line 182

def self.options=(opts)
  @@options = opts
end

.send_request(opts) ⇒ Object

Generic send request to ECS REST service. You have to specify the :operation parameter.



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/amazon_associate/request.rb', line 230

def self.send_request(opts)
  opts = self.options.merge(opts) if self.options
  request_url = prepare_url(opts)
  response = nil

  if caching_enabled?
    AmazonAssociate::CacheFactory.sweep(self.options[:caching_strategy])
    
    res = AmazonAssociate::CacheFactory.get(request_url, self.options[:caching_strategy]) 
    response = Response.new(res, request_url) unless res.nil?
  end
  
  if !caching_enabled? || response.nil?
    log "Request URL: #{request_url}"
    res = Net::HTTP.get_response(URI::parse(request_url))
    unless res.kind_of? Net::HTTPSuccess
      raise AmazonAssociate::RequestError, "HTTP Response: #{res.code} #{res.message}"
    end
    response = Response.new(res.body, request_url)
    cache_response(request_url, response, self.options[:caching_strategy]) if caching_enabled?
  end
    
  response
end

.sort_types(search_index) ⇒ Object

Returns an Array of valid sort types for search_index, or nil if search_index is invalid.



83
84
85
# File 'lib/amazon_associate/request.rb', line 83

def self.sort_types(search_index)
  SORT_TYPES.has_key?(search_index) ? SORT_TYPES[search_index] : nil
end