Class: Sucker::Request

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/sucker/request.rb,
lib/sucker/synchrony.rb

Overview

We minimally patch Request and Response to make them fiber-aware.

Constant Summary collapse

HOSTS =
{
:us  => 'ecs.amazonaws.com',
:uk  => 'ecs.amazonaws.co.uk',
:de  => 'ecs.amazonaws.de',
:ca  => 'ecs.amazonaws.ca',
:fr  => 'ecs.amazonaws.fr',
:jp  => 'ecs.amazonaws.jp' }
LOCALES =
HOSTS.keys

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Request

Creates a new a request.

Takes an optional hash of attribute and value pairs.

request = Sucker.new(
  :locale        => :us,
  :key           => amazon_key,
  :secret        => amazon_secret)
  :associate_tag => amazon_associate_tag)


34
35
36
37
# File 'lib/sucker/request.rb', line 34

def initialize(args = {})
  @config = Config
  args.each { |k, v| self.send("#{k}=", v) }
end

Instance Method Details

#<<(hash) ⇒ Object

Merges a hash into the existing parameters.

request << {
  'Operation' => 'ItemLookup',
  'IdType'    => 'ASIN',
  'ItemId'    => '0816614024' }


46
47
48
# File 'lib/sucker/request.rb', line 46

def <<(hash)
  parameters.merge!(hash)
end

#adapterObject

The HTTP adapter.



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

def adapter
  @adapter ||= HTTPClient.new
end

#aget(&block) ⇒ Object

Performs an evented request and yields a response to given block.



13
14
15
16
17
# File 'lib/sucker/synchrony.rb', line 13

def aget(&block)
  http = EM::HttpRequest.new(url).aget
  http.callback { block.call(Response.new(http)) }
  http.errback  { block.call(Response.new(http)) }
end

#getObject

Performs an evented request and returns a response.



59
60
61
62
# File 'lib/sucker/request.rb', line 59

def get
  response = adapter.get(url)
  Response.new(response)
end

#parametersObject

The query parameters.



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

def parameters
  @parameters ||= Parameters.new
end

#resetObject

Resets parameters and returns self.



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

def reset
  parameters.reset
  self
end

#urlObject

The request URL.



76
77
78
79
80
81
82
# File 'lib/sucker/request.rb', line 76

def url
  URI::HTTP.build(
    :host   => host,
    :path   => '/onca/xml',
    :query  => sign(build_query_string)
  )
end

#version=(version) ⇒ Object

Sets the Amazon API version timestamp.



85
86
87
# File 'lib/sucker/request.rb', line 85

def version=(version)
  parameters['Version'] = version
end