Class: Vacuum::Request

Inherits:
Object
  • Object
show all
Includes:
Jeff
Defined in:
lib/vacuum/request.rb

Overview

An Amazon Product Advertising API request.

Constant Summary collapse

BadLocale =
Class.new(ArgumentError)
HOSTS =

A list of Amazon Product Advertising API hosts.

{
  'CA' => 'ecs.amazonaws.ca',
  'CN' => 'webservices.amazon.cn',
  'DE' => 'ecs.amazonaws.de',
  'ES' => 'webservices.amazon.es',
  'FR' => 'ecs.amazonaws.fr',
  'IT' => 'webservices.amazon.it',
  'JP' => 'ecs.amazonaws.jp',
  'GB' => 'ecs.amazonaws.co.uk',
  'US' => 'ecs.amazonaws.com'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locale = nil) ⇒ Request

Create a new request for given locale.

locale - The String Product Advertising API locale (default: US).

Raises a Bad Locale error if locale is not valid.



32
33
34
35
36
37
38
39
40
# File 'lib/vacuum/request.rb', line 32

def initialize(locale = nil)
  if locale == 'UK'
    warn '[DEPRECATION] Use GB instead of UK'
    locale = 'GB'
  end

  host = HOSTS[locale || 'US'] or raise BadLocale
  self.endpoint = "http://#{host}/onca/xml"
end

Instance Attribute Details

#associate_tagObject Also known as: tag

Get/Sets the String Associate Tag.



57
58
59
# File 'lib/vacuum/request.rb', line 57

def associate_tag
  @associate_tag
end

Instance Method Details

#configure(credentials) ⇒ Object

Configure the Amazon Product Advertising API request.

credentials - The Hash credentials of the API endpoint.

:aws_access_key_id     - The String Amazon Web Services
                         (AWS) key.
:aws_secret_access_key - The String AWS secret.
:associate_tag         - The String Associate Tag.

Returns self.



51
52
53
54
# File 'lib/vacuum/request.rb', line 51

def configure(credentials)
  credentials.each { |key, val| self.send("#{key}=", val) }
  self
end

#url(params) ⇒ Object

Build a URL.

params - A Hash of Amazon Product Advertising query params.

Returns the built URL String.



67
68
69
70
71
72
73
74
# File 'lib/vacuum/request.rb', line 67

def url(params)
  opts = {
    method: :get,
    query:  params
  }

  [endpoint, build_options(opts).fetch(:query)].join('?')
end