amazon_pa_api 0.1.1 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: edd7205207a1d63992e1ce76e00ea23a84da1dcf
4
- data.tar.gz: 116ef6b511304f7902da9ab287c56022fba74ab6
3
+ metadata.gz: 9d40c14d5eefeb894fb39a056f1e1a1c0f73d199
4
+ data.tar.gz: 3b8cd40d8f39d1e9e054ea9ea6beb4be33e32dc2
5
5
  SHA512:
6
- metadata.gz: 0f48685698d113ddb7e7dda899711959eef12f4423b59658c023e2290629e69cec65fbe2124af6a779795731322b6e355ab9f52d9021aa3bf38005239621b2e8
7
- data.tar.gz: 1769d6dd9694c0330792a975bad26e2981e9500924ac89a57c934ade208561a7605faa8c133a1c384003702b1259ac450a31e17a739fc3afb020c2ea1e4a678a
6
+ metadata.gz: 8635e29f1d5464973baed13c5ff644e5cb30eab917e4d39eb61f7d4e0b9430c1955aeb72bc72d65556feef6d1586064ba23b0fe30978fe9873607f5ecd330247
7
+ data.tar.gz: 23190915af42be806daa5f4880b59006de700d10610b766b5ef33bf1623f3f44696a78fb58d8f402e9f9751ac2f8fec69dd6703712f766738d5049708aed3343
data/README.md CHANGED
@@ -1,6 +1,11 @@
1
1
  # AmazonPaApi
2
2
 
3
- The purpose of this gem is to make use of Amazon Product Advertising API and pull easily Amazon products information.
3
+ The purpose of this gem is to make use of Amazon Product Advertising API and pull easily Amazon products information.
4
+ Supported Amazon Product Advertising API operations.
5
+
6
+ * ItemLookup
7
+ * ItemSearch
8
+ * SimilarityLookup
4
9
 
5
10
  ## Installation
6
11
 
@@ -21,16 +26,19 @@ Or install it yourself as:
21
26
  ## Usage
22
27
 
23
28
  You can use easily Amazon Product Advertising API calls, like this.
24
- ```ruby
25
- item_lookup = AmazonPaApi::ItemLookup.new('B0026IAWMU')
26
- item_lookup.access_key_id = "Your Amazon AWS access key id"
27
- item_lookup.secret_access_key = "Your Amazon AWS secret key"
28
- item_lookup.associate_tag = "Your Amazon associate tag"
29
+
30
+ ```
31
+ item_lookup = AmazonPaApi::ItemLookup.new('B0026IAWMU')
32
+ item_lookup.access_key_id = "Your Amazon AWS access key id" # required
33
+ item_lookup.secret_access_key = "Your Amazon AWS secret key" # required
34
+ item_lookup.associate_tag = "Your Amazon associate tag" # required
29
35
 
30
- puts item_lookup.get.body # => XML
36
+ puts item_lookup.get.body # => XML
31
37
  ```
38
+
32
39
  Or you can set Amazon credentials as hash, like this.
33
- ```ruby
40
+
41
+ ```
34
42
  credentials = { access_key_id: "Your Amazon AWS access key id",
35
43
  secret_access_key: "Your Amazon AWS secret key",
36
44
  associate_tag: "Your Amazon associate tag"
@@ -38,20 +46,27 @@ Or you can set Amazon credentials as hash, like this.
38
46
  item_search = AmazonPaApi::ItemSearch.new('Attack on Titan', 'Books')
39
47
  item_search.credentials = credentials
40
48
  puts item_search.get.body # => XML
41
-
42
- item_lookup = AmazonPaApi::ItemLookup.new('B0026IAWMU')
49
+
50
+ item_lookup = AmazonPaApi::ItemLookup.new('B0026IAWMU')
43
51
  item_lookup.credentials = credentials
44
- puts item_lookup.get.body # => XML
52
+ puts item_lookup.get.body # => XML
45
53
  ```
54
+
46
55
  If you want to choice Amazon ECommerce end point, like this.
47
- ```ruby
48
- item_lookup = = AmazonPaApi::ItemLookup.new('B0026IAWMU', region: :uk)
56
+
49
57
  ```
58
+ item_lookup = AmazonPaApi::ItemLookup.new('B00NLDYGDK', region: :uk)
59
+ ```
60
+
50
61
  You can use Amazon Product Advertising API operation's request parameters as instance methods.
51
- ```ruby
52
- item_lookup.id_type = :ean
53
- item_lookup.condition = :new
62
+
54
63
  ```
64
+ item_lookup.id_type = 'EAN'
65
+ item_lookup.seach_index = 'Books'
66
+ item_lookup.condition = 'New'
67
+ puts item_search.get.body # => XML
68
+ ```
69
+
55
70
  ## Development
56
71
 
57
72
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -1,9 +1,8 @@
1
1
  require "amazon_pa_api/version"
2
- require "amazon_pa_api/request"
3
2
  require "amazon_pa_api/extensions"
4
- require "amazon_pa_api/operation"
5
- require "amazon_pa_api/item_lookup"
6
- require "amazon_pa_api/item_search"
3
+ require "amazon_pa_api/operations/item_lookup"
4
+ require "amazon_pa_api/operations/item_search"
5
+ require "amazon_pa_api/operations/similarity_lookup"
7
6
 
8
7
  require "net/http"
9
8
  require "cgi"
@@ -1,7 +1,9 @@
1
+ require 'amazon_pa_api/operations/operation'
2
+
1
3
  module AmazonPaApi
2
4
  class ItemLookup < AmazonPaApi::Operation
3
5
 
4
- #= this operation has request parameters.
6
+ # This is list of Amazon Product Advertising API request parameters.
5
7
  REQUEST_PARAMETERS = [
6
8
  :condition,
7
9
  :id_type,
@@ -20,7 +22,7 @@ module AmazonPaApi
20
22
  :response_group,
21
23
  ]
22
24
 
23
- #= ItemLookup requires ItemId.
25
+ # ItemLookup requires ItemId.
24
26
  def initialize(item_id, region: :jp)
25
27
  super()
26
28
  self.item_id = item_id
@@ -1,7 +1,9 @@
1
+ require 'amazon_pa_api/operations/operation'
2
+
1
3
  module AmazonPaApi
2
4
  class ItemSearch < AmazonPaApi::Operation
3
5
 
4
- #= this operation has request parameters.
6
+ # This is list of Amazon Product Advertising API request parameters.
5
7
  REQUEST_PARAMETERS = [
6
8
  :actor,
7
9
  :artist,
@@ -40,7 +42,7 @@ module AmazonPaApi
40
42
  :response_group
41
43
  ]
42
44
 
43
- #= ItemSearch requires keywords and search index.
45
+ # ItemSearch requires keywords and search index.
44
46
  def initialize(keywords, search_index, region: :jp)
45
47
  super()
46
48
  self.keywords = keywords
@@ -1,28 +1,30 @@
1
+ require 'amazon_pa_api/request'
2
+
1
3
  module AmazonPaApi
2
4
  #= This is the parent class of every PA api Operations performed.
3
5
  class Operation
4
6
 
5
7
  include AmazonPaApi::Request
6
8
 
7
- #= PA api requires thease credentials.
9
+ # PA api requires thease credentials.
8
10
  # String - You must to set thease before performing operations.
9
11
  attr_accessor :access_key_id, :secret_access_key, :associate_tag
10
12
 
11
- #= Subclass set this variables at initialize.
13
+ # Subclass set this variables at initialize.
12
14
  # String - The name of operation you want to perform.
13
15
  attr_accessor :operation
14
16
 
15
- #= Subclass set this variables at initialize.
17
+ # Subclass set this variables at initialize.
16
18
  # String - It will be use this to pick the right associates key up.
17
19
  # default is jp.
18
20
  attr_accessor :region
19
21
 
20
- #= You can get PA api response via thease.
22
+ # You can get PA api response via thease.
21
23
  attr_reader :header, :body
22
24
 
23
25
  API_VERSION = "2010-09-01".freeze
24
26
 
25
- #= PA api end points.
27
+ # PA api end points.
26
28
  # PA api calls can be sent to any of 6 regions.
27
29
  END_POINTS = {
28
30
  ca: "http://ecs.amazonaws.ca/onca/xml",
@@ -39,25 +41,12 @@ module AmazonPaApi
39
41
  end
40
42
  end
41
43
 
42
- #= Execute the request to Amazon.
44
+ # Execute the request to Amazon.
43
45
  def get
44
- if self.access_key_id.nil? || self.access_key_id.to_s.size == 0 ||
45
- self.secret_access_key.nil? || self.secret_access_key.to_s.size == 0 ||
46
- self.associate_tag.nil? || self.associate_tag.to_s.size == 0
47
- raise "PA api requires AWS credentials."
48
- end
49
-
50
- unsigned_uri = URI.parse("#{END_POINTS[self.region]}?#{request_params_string}")
51
- uri = add_signature(unsigned_uri)
52
-
53
- response = get_response uri
54
- @header = response.header
55
- @body = response.body
56
-
57
- response
46
+ request
58
47
  end
59
48
 
60
- #= You can set PA api credentials via this.
49
+ # You can set PA api credentials via this.
61
50
  def credentials=(access_key_id: '',
62
51
  secret_access_key: '',
63
52
  associate_tag:''
@@ -69,7 +58,7 @@ module AmazonPaApi
69
58
 
70
59
  protected
71
60
 
72
- #= It set instance variable from defined request parameters in subclass.
61
+ # It set instance variable from defined request parameters in subclass.
73
62
  def params
74
63
  self.class.const_get("REQUEST_PARAMETERS").inject({}) do |parameters, param|
75
64
  value = instance_variable_get("@#{param}")
@@ -78,18 +67,19 @@ module AmazonPaApi
78
67
  end
79
68
  end
80
69
 
81
- #= It set common query parameters as hash.
70
+ # It set common query parameters as hash.
82
71
  def request_params
83
- {"Service" => "AWSECommerceService",
84
- "Timestamp" => Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
85
- "AWSAccessKeyId" => self.access_key_id.to_s,
86
- "Operation" => self.operation,
87
- "AssociateTag" => self.associate_tag.to_s,
88
- "Version" => API_VERSION,
89
- }.merge!(params)
72
+ parameters ={
73
+ "Service" => "AWSECommerceService",
74
+ "Timestamp" => Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
75
+ "AWSAccessKeyId" => self.access_key_id.to_s,
76
+ "AssociateTag" => self.associate_tag.to_s,
77
+ "Operation" => self.operation,
78
+ "Version" => API_VERSION,
79
+ }.merge!(params)
90
80
  end
91
81
 
92
- #= It sort and set all query parameters as hash.
82
+ # It sort and set all query parameters as hash.
93
83
  def request_params_string
94
84
  request_params.sort.inject('') do |params_string, (k,v)|
95
85
  params_string += '&' unless params_string.size == 0
@@ -100,7 +90,7 @@ module AmazonPaApi
100
90
 
101
91
  private
102
92
 
103
- #= it add signature parameter from uri
93
+ # it add signature parameter from uri
104
94
  # URI - To Amazon AWS ECommerce Service.
105
95
  def add_signature(unsigned_uri)
106
96
  signature = OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new("sha256"),
@@ -113,5 +103,24 @@ module AmazonPaApi
113
103
 
114
104
  end
115
105
 
106
+ # it add signature and requests Amazon via http.
107
+ def request
108
+ if self.access_key_id.nil? ||
109
+ self.secret_access_key.nil? ||
110
+ self.associate_tag.nil?
111
+ raise "PA api requires AWS credentials. Please set access_key_id, secret_access_key and associate_tag, try again."
112
+ end
113
+ raise "Invalid region. region: #{self.region}" unless END_POINTS.include?(self.region)
114
+
115
+ unsigned_uri = URI.parse("#{END_POINTS[self.region]}?#{request_params_string}")
116
+ uri = add_signature(unsigned_uri)
117
+
118
+ response = get_response uri
119
+ @header = response.header
120
+ @body = response.body
121
+
122
+ response
123
+ end
124
+
116
125
  end
117
126
  end
@@ -0,0 +1,23 @@
1
+ require 'amazon_pa_api/operations/operation'
2
+
3
+ module AmazonPaApi
4
+ class SimilarityLookup < AmazonPaApi::Operation
5
+
6
+ # This is list of Amazon Product Advertising API request parameters.
7
+ REQUEST_PARAMETERS = [
8
+ :condition,
9
+ :item_id,
10
+ :merchant_id,
11
+ :similarity_type,
12
+ :response_group,
13
+ ]
14
+
15
+ def initialize(item_id, region: :jp)
16
+ super()
17
+ self.item_id = item_id
18
+ self.region = region
19
+ self.operation = "SimilarityLookup"
20
+ end
21
+
22
+ end
23
+ end
@@ -1,10 +1,13 @@
1
1
  # coding: utf-8
2
2
  module AmazonPaApi
3
+ #= This is mixed into operations class.
3
4
  module Request
5
+ # HTTP access parameters
4
6
  attr_accessor :open_timeout, :read_timeout
5
7
 
6
8
  private
7
9
 
10
+ # it get HTTP response.
8
11
  def get_response(uri_or_url, limit = 10)
9
12
  raise "Redirect is too depp." if limit <= 0
10
13
  uri = uri_or_url.class == String ? URI.parse(uri_or_url) : uri_or_url
@@ -25,7 +28,7 @@ module AmazonPaApi
25
28
  redirect_uri = URI::parse(response.header['location'])
26
29
  return get_response(redirect_uri, limit -1 )
27
30
  else
28
- raise "Error connecting to Amazon. response: #{response.to_w}"
31
+ raise "Error connecting to Amazon. response: #{response.to_s}"
29
32
  end
30
33
  end
31
34
 
@@ -1,3 +1,3 @@
1
1
  module AmazonPaApi
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: amazon_pa_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - sakura-computer
@@ -73,9 +73,10 @@ files:
73
73
  - bin/setup
74
74
  - lib/amazon_pa_api.rb
75
75
  - lib/amazon_pa_api/extensions.rb
76
- - lib/amazon_pa_api/item_lookup.rb
77
- - lib/amazon_pa_api/item_search.rb
78
- - lib/amazon_pa_api/operation.rb
76
+ - lib/amazon_pa_api/operations/item_lookup.rb
77
+ - lib/amazon_pa_api/operations/item_search.rb
78
+ - lib/amazon_pa_api/operations/operation.rb
79
+ - lib/amazon_pa_api/operations/similarity_lookup.rb
79
80
  - lib/amazon_pa_api/request.rb
80
81
  - lib/amazon_pa_api/version.rb
81
82
  homepage: https://github.com/sakura-computer/amazon_pa_api