Class: AmazonProductAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/amazon_product_api.rb,
lib/amazon_product_api/version.rb

Overview

A very simple client for the Amazon Product Advertising API.

Constant Summary collapse

ENDPOINTS =
{
  :ca => 'https://ecs.amazonaws.ca/onca/xml',
  :cn => 'https://webservices.amazon.cn/onca/xml',
  :de => 'https://ecs.amazonaws.de/onca/xml',
  :es => 'https://webservices.amazon.es/onca/xml',
  :fr => 'https://ecs.amazonaws.fr/onca/xml',
  :it => 'https://webservices.amazon.it/onca/xml',
  :jp => 'https://ecs.amazonaws.jp/onca/xml',
  :uk => 'https://ecs.amazonaws.co.uk/onca/xml',
  :us => 'https://webservices.amazon.com/onca/xml'
}
SERVICE =
'AWSECommerceService'
API_VERSION =
'2011-08-01'
AMPERSAND =
'&'
COMMA =
','
PERCENT =
'%'
HTTPS =
'https'
USER_AGENT =
"amazon_product_api #{VERSION}"
HEADERS =
{'User-Agent' => USER_AGENT, 'Accept' => 'application/xml'}
DIGEST =
OpenSSL::Digest::SHA256.new
ENCODE =
/([^a-zA-Z0-9_.~-]+)/
VERSION =
"0.1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locale, access_key_id, secret_access_key, associate_tag) ⇒ AmazonProductAPI

Possible locale values are - :ca, :cn, :de, :es, :fr, :it, :jp, :uk, :us

Parameters:

  • locale (Symbol)

    locale corresponding to an API endpoint

  • access_key_id (String)

    your Amazon access key ID

  • secret_access_key (String)

    your Amazon secret access key

  • associate_tag (String)

    your Amazon associate tag

Raises:

  • (ArgumentError)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/amazon_product_api.rb', line 40

def initialize(locale, access_key_id, secret_access_key, associate_tag)
  # Check that the locale provided maps to an endpoint string:
  raise ArgumentError, "invalid locale '#{locale}'" if ENDPOINTS[locale].nil?

  @endpoint = URI.parse(ENDPOINTS[locale])
  @access_key_id = access_key_id
  @secret_access_key = secret_access_key
  @associate_tag = associate_tag
  @connection = Net::HTTP.new(@endpoint.host, @endpoint.port)
  @default_params = {
    'Service' => SERVICE,
    'Version' => API_VERSION,
    'AWSAccessKeyId' => @access_key_id,
    'AssociateTag' => @associate_tag
  }

  if @endpoint.scheme == HTTPS
    @connection.use_ssl = true
  end
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



32
33
34
# File 'lib/amazon_product_api.rb', line 32

def connection
  @connection
end

Instance Method Details

#get(operation, response_groups, operation_params = {}) ⇒ Net::HTTPResponse

Parameters:

  • operation (String)

    the operation name

  • response_groups (Array<String>)

    a list of response groups

  • operation_params (Hash) (defaults to: {})

    all other parameters required by the operation

Returns:

  • (Net::HTTPResponse)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/amazon_product_api.rb', line 65

def get(operation, response_groups, operation_params = {})
  base_params = {
    'Operation' => operation,
    'ResponseGroup' => response_groups.join(COMMA),
    'Timestamp' => Time.now.xmlschema
  }

  unsigned_params = operation_params.merge(@default_params.merge(base_params))

  signed_params = unsigned_params.merge({'Signature' => signature(unsigned_params)})

  request_uri = @endpoint.dup
  request_uri.query = query_string(signed_params)

  @connection.request(Net::HTTP::Get.new(request_uri.to_s, HEADERS))
end