Class: AmazonPaApi::Operation

Inherits:
Object
  • Object
show all
Includes:
Request
Defined in:
lib/amazon_pa_api/operation.rb

Overview

This is the parent class of every PA api Operations performed.

Direct Known Subclasses

ItemLookup, ItemSearch

Constant Summary collapse

API_VERSION =
"2010-09-01".freeze
END_POINTS =

PA api end points.

PA api calls can be sent to any of 6 regions.
{
  ca: "http://ecs.amazonaws.ca/onca/xml",
  de: "http://ecs.amazonaws.de/onca/xml",
  fr: "http://ecs.amazonaws.fr/onca/xml",
  jp: "http://ecs.amazonaws.jp/onca/xml",
  uk: "http://ecs.amazonaws.co.uk/onca/xml",
  us: "http://ecs.amazonaws.com/onca/xml",
}.freeze

Instance Attribute Summary collapse

Attributes included from Request

#open_timeout, #read_timeout

Instance Method Summary collapse

Constructor Details

#initializeOperation

Returns a new instance of Operation.



36
37
38
39
40
# File 'lib/amazon_pa_api/operation.rb', line 36

def initialize
  self.class.const_get("REQUEST_PARAMETERS").each do |param|
    self.class.send(:attr_accessor, param)
  end
end

Instance Attribute Details

#access_key_idObject

PA api requires thease credentials.

String - You must to set thease before performing operations.



9
10
11
# File 'lib/amazon_pa_api/operation.rb', line 9

def access_key_id
  @access_key_id
end

#associate_tagObject

PA api requires thease credentials.

String - You must to set thease before performing operations.



9
10
11
# File 'lib/amazon_pa_api/operation.rb', line 9

def associate_tag
  @associate_tag
end

#bodyObject (readonly)

You can get PA api response via thease.



21
22
23
# File 'lib/amazon_pa_api/operation.rb', line 21

def body
  @body
end

#headerObject (readonly)

You can get PA api response via thease.



21
22
23
# File 'lib/amazon_pa_api/operation.rb', line 21

def header
  @header
end

#operationObject

Subclass set this variables at initialize.

String - The name of operation you want to perform.



13
14
15
# File 'lib/amazon_pa_api/operation.rb', line 13

def operation
  @operation
end

#regionObject

Subclass set this variables at initialize.

String - It will be use this to pick the right associates key up.

default is jp.


18
19
20
# File 'lib/amazon_pa_api/operation.rb', line 18

def region
  @region
end

#secret_access_keyObject

PA api requires thease credentials.

String - You must to set thease before performing operations.



9
10
11
# File 'lib/amazon_pa_api/operation.rb', line 9

def secret_access_key
  @secret_access_key
end

Instance Method Details

#credentials=(access_key_id: '', secret_access_key: '', associate_tag: '') ⇒ Object

You can set PA api credentials via this.



61
62
63
64
65
66
67
68
# File 'lib/amazon_pa_api/operation.rb', line 61

def credentials=(access_key_id: '',
                 secret_access_key: '',
                 associate_tag:''
                )
  self.access_key_id = access_key_id
  self.secret_access_key = secret_access_key
  self.associate_tag = associate_tag
end

#getObject

Execute the request to Amazon.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/amazon_pa_api/operation.rb', line 43

def get
  if self.access_key_id.nil? || self.access_key_id.to_s.size == 0 ||
     self.secret_access_key.nil? || self.secret_access_key.to_s.size == 0 ||
     self.associate_tag.nil? || self.associate_tag.to_s.size == 0 
    raise "PA api requires AWS  credentials."
  end

  unsigned_uri = URI.parse("#{END_POINTS[self.region]}?#{request_params_string}")
  uri = add_signature(unsigned_uri)

  response = get_response uri
  @header = response.header
  @body = response.body

  response
end