Class: Vacuum::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/vacuum/request.rb,
lib/vacuum/em.rb

Overview

A wrapper around the request to the Amazon Product Advertising API.

Constant Summary collapse

CURRENT_API_VERSION =

The latest Amazon API version.

'2011-08-01'
HOSTS =

A list of Amazon endpoints.

{
  :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',
  :uk => 'ecs.amazonaws.co.uk',
  :us => 'ecs.amazonaws.com'
}

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ self

Creates a new request for given locale and credentials.

Parameters:

  • options (Hash)
  • opts (Hash)

    a customizable set of options

Raises:

  • (MissingKey)

    An Amazon AWS access key ID was not given

  • (MissingSecret)

    An Amazon AWS secret key was not given

  • (MissingTag)

    An Amazon Associate tag was not given



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

def initialize(options)
  _reset!

  locale  = (options[:locale] || :us).to_sym
  @host   = HOSTS[locale]    or raise BadLocale
  @key    = options[:key]    or raise MissingKey
  @secret = options[:secret] or raise MissingSecret
  @tag    = options[:tag]    or raise MissingTag
end

Instance Method Details

#aget { ... } ⇒ Object

Performs an async request.

Yields:

  • Passes response to given block



9
10
11
12
13
14
15
# File 'lib/vacuum/em.rb', line 9

def aget(&blk)
  http = EM::HttpRequest.new(url).get
  http.callback { blk.call _response(http) }
  http.errback  { blk.call _response(http) }

  nil
end

#build(hsh) ⇒ self

Merges given parameters into the request query.

Parameters:

  • hsh (Hash)

    Pairs of keys and values

Returns:

  • (self)


45
46
47
48
49
50
51
# File 'lib/vacuum/request.rb', line 45

def build(hsh)
  hsh.each do |k, v|
    @params[k] = v.is_a?(Array) ? v.join(',') : v.to_s
  end

  self
end

#build!(hsh = {}) ⇒ Object

Replaces the request query with given parameters.

see(#build)



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

def build!(hsh = {})
  _reset!
  build hsh
end

#getVacuum::Response

Performs a request.

Returns:



64
65
66
67
# File 'lib/vacuum/request.rb', line 64

def get
  res = Net::HTTP.get_response(url)
  Response.new(res.body, res.code)
end

#paramsHash

Returns The parameters that make up the request query.

Returns:

  • (Hash)

    The parameters that make up the request query.



70
71
72
# File 'lib/vacuum/request.rb', line 70

def params
  _default_params.merge(@params)
end

#urlURI::HTTP

Returns The URL for the API request.

Returns:

  • (URI::HTTP)

    The URL for the API request



75
76
77
78
79
# File 'lib/vacuum/request.rb', line 75

def url
  URI::HTTP.build :host  => @host,
                  :path  => '/onca/xml',
                  :query => _query_string
end