amazon_pa_api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b58d4eea3a6af7df87c5d39d0022be5491a43935
4
+ data.tar.gz: c6dc032953e373889ccb94a0dba0cb58ea450a23
5
+ SHA512:
6
+ metadata.gz: 5c27cde0d227b78ce348b1427c843546f5ec47fe33b744736ade7ec6c276f5bbde2653e9b1a980a9bd873e09f49b425c2b4433fb1a665fda665033044fcfd863
7
+ data.tar.gz: eeb3e0e16b3192abb5ab335aea065734a6d2a82fda10e6be077d057206ea2fa750bb2a00b488f69cc481ea1b27cfa54c36511deae1c0ffbee57c6dbacca21a29
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ .pryrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in amazon_pa_api.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Masaru Ishimoto
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # AmazonPaApi
2
+
3
+ The purpose of this gem is to make use of Amazon Product Advertising API and pull easily Amazon products information.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'amazon_pa_api'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install amazon_pa_api
20
+
21
+ ## Usage
22
+
23
+ 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
+ puts item_lookup.get.body # => XML
31
+ ```
32
+ Or, you can set Amazon credentials as hash, like this.
33
+ ```ruby
34
+ credentials = { access_key_id: "Your Amazon AWS access key id",
35
+ secret_access_key: "Your Amazon AWS secret key",
36
+ associate_tag: "Your Amazon associate tag"
37
+ }
38
+ item_search = AmazonPaApi::ItemSearch.new('Attack on Titan', 'Books')
39
+ item_search.credentials = credentials
40
+ puts item_search.get.body # => XML
41
+
42
+ item_lookup = AmazonPaApi::ItemLookup.new('B0026IAWMU')
43
+ item_lookup.credentials = credentials
44
+ puts item_lookup.get.body # => XML
45
+ ```
46
+
47
+ ## Development
48
+
49
+ 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.
50
+
51
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
52
+
53
+ ## Contributing
54
+
55
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/amazon_pa_api. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
56
+
57
+
58
+ ## License
59
+
60
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
61
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'amazon_pa_api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "amazon_pa_api"
8
+ spec.version = AmazonPaApi::VERSION
9
+ spec.authors = ["sakura-computer"]
10
+ spec.email = ["ishimoto.masaru@sakura-computer.co.jp"]
11
+
12
+ spec.summary = %q{For Amazon Product Advertiseing API.}
13
+ spec.description = %q{The purpose of this gem is to make use of Amazon Product Advertising API and pull easily Amazon products information.}
14
+ spec.homepage = "https://github.com/sakura-computer/amazon_pa_api"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "amazon_pa_api"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,11 @@
1
+ require "amazon_pa_api/version"
2
+ require "amazon_pa_api/request"
3
+ 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"
7
+
8
+ require "net/http"
9
+ require "cgi"
10
+ require "openssl"
11
+ require "base64"
@@ -0,0 +1,16 @@
1
+ module AmazonPaApi
2
+ module Extensions
3
+ class ::String
4
+ def camel_case
5
+ self.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
6
+ end
7
+ def snake_case
8
+ self.to_s.gsub(/::/, '/').
9
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
10
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
11
+ tr("-", "_").
12
+ downcase
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,31 @@
1
+ module AmazonPaApi
2
+ class ItemLookup < AmazonPaApi::Operation
3
+
4
+ #= this operation has request parameters.
5
+ REQUEST_PARAMETERS = [
6
+ :condition,
7
+ :id_type,
8
+ :item_id,
9
+ :merchant_id,
10
+ :offer_page,
11
+ :related_items_page,
12
+ :relationship_type,
13
+ :review_page,
14
+ :review_sort,
15
+ :search_index,
16
+ :tag_page,
17
+ :tags_per_page,
18
+ :tag_sort,
19
+ :variation_page,
20
+ :response_group,
21
+ ]
22
+
23
+ #= ItemLookup requires ItemId.
24
+ def initialize(item_id, region: :jp)
25
+ super()
26
+ self.item_id = item_id
27
+ self.region = region
28
+ self.operation = "ItemLookup"
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,52 @@
1
+ module AmazonPaApi
2
+ class ItemSearch < AmazonPaApi::Operation
3
+
4
+ #= this operation has request parameters.
5
+ REQUEST_PARAMETERS = [
6
+ :actor,
7
+ :artist,
8
+ :audience_rating,
9
+ :author,
10
+ :availability,
11
+ :brand,
12
+ :browse_node,
13
+ :city,
14
+ :composer,
15
+ :condition,
16
+ :conductor,
17
+ :director,
18
+ :item_page,
19
+ :keywords,
20
+ :manufacturer,
21
+ :maximum_price,
22
+ :merchant_id,
23
+ :minimum_price,
24
+ :neighborhood,
25
+ :orchestra,
26
+ :postal_code,
27
+ :power,
28
+ :publisher,
29
+ :related_items_page,
30
+ :relationship_type,
31
+ :review_sort,
32
+ :search_index,
33
+ :sort,
34
+ :tag_page,
35
+ :tags_per_page,
36
+ :tag_sort,
37
+ :text_stream,
38
+ :title,
39
+ :variation_page,
40
+ :response_group
41
+ ]
42
+
43
+ #= ItemSearch requires keywords and search index.
44
+ def initialize(keywords, search_index, region: :jp)
45
+ super()
46
+ self.keywords = keywords
47
+ self.search_index = search_index
48
+ self.region = region
49
+ self.operation = "ItemSearch"
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,117 @@
1
+ module AmazonPaApi
2
+ #= This is the parent class of every PA api Operations performed.
3
+ class Operation
4
+
5
+ include AmazonPaApi::Request
6
+
7
+ #= PA api requires thease credentials.
8
+ # String - You must to set thease before performing operations.
9
+ attr_accessor :access_key_id, :secret_access_key, :associate_tag
10
+
11
+ #= Subclass set this variables at initialize.
12
+ # String - The name of operation you want to perform.
13
+ attr_accessor :operation
14
+
15
+ #= Subclass set this variables at initialize.
16
+ # String - It will be use this to pick the right associates key up.
17
+ # default is jp.
18
+ attr_accessor :region
19
+
20
+ #= You can get PA api response via thease.
21
+ attr_reader :header, :body
22
+
23
+ API_VERSION = "2010-09-01".freeze
24
+
25
+ #= PA api end points.
26
+ # PA api calls can be sent to any of 6 regions.
27
+ END_POINTS = {
28
+ ca: "http://ecs.amazonaws.ca/onca/xml",
29
+ de: "http://ecs.amazonaws.de/onca/xml",
30
+ fr: "http://ecs.amazonaws.fr/onca/xml",
31
+ jp: "http://ecs.amazonaws.jp/onca/xml",
32
+ uk: "http://ecs.amazonaws.co.uk/onca/xml",
33
+ us: "http://ecs.amazonaws.com/onca/xml",
34
+ }.freeze
35
+
36
+ def initialize
37
+ self.class.const_get("REQUEST_PARAMETERS").each do |param|
38
+ self.class.send(:attr_accessor, param)
39
+ end
40
+ end
41
+
42
+ #= Execute the request to Amazon.
43
+ 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
58
+ end
59
+
60
+ #= You can set PA api credentials via this.
61
+ def credentials=(access_key_id: '',
62
+ secret_access_key: '',
63
+ associate_tag:''
64
+ )
65
+ self.access_key_id = access_key_id
66
+ self.secret_access_key = secret_access_key
67
+ self.associate_tag = associate_tag
68
+ end
69
+
70
+ protected
71
+
72
+ #= It set instance variable from defined request parameters in subclass.
73
+ def params
74
+ self.class.const_get("REQUEST_PARAMETERS").inject({}) do |parameters, param|
75
+ value = instance_variable_get("@#{param}")
76
+ parameters[param.to_s.camel_case] = value unless value.nil?
77
+ parameters
78
+ end
79
+ end
80
+
81
+ #= It set common query parameters as hash.
82
+ 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)
90
+ end
91
+
92
+ #= It sort and set all query parameters as hash.
93
+ def request_params_string
94
+ request_params.sort.inject('') do |params_string, (k,v)|
95
+ params_string += '&' unless params_string.size == 0
96
+ params_string += "#{k.to_s}=#{CGI.escape(v.to_s)}"
97
+ params_string
98
+ end
99
+ end
100
+
101
+ private
102
+
103
+ #= it add signature parameter from uri
104
+ # URI - To Amazon AWS ECommerce Service.
105
+ def add_signature(unsigned_uri)
106
+ signature = OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new("sha256"),
107
+ self.secret_access_key,
108
+ "GET\n#{unsigned_uri.host}\n#{unsigned_uri.path}\n#{unsigned_uri.query}"
109
+ )
110
+ signature = [signature].pack("m").chomp
111
+ signature = URI.escape(signature, Regexp.new("[+=]"))
112
+ URI.parse "#{unsigned_uri}&Signature=#{signature}"
113
+
114
+ end
115
+
116
+ end
117
+ end
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ module AmazonPaApi
3
+ module Request
4
+ attr_accessor :open_timeout, :read_timeout
5
+
6
+ private
7
+
8
+ def get_response(uri_or_url, limit = 10)
9
+ raise "Redirect is too depp." if limit <= 0
10
+ uri = uri_or_url.class == String ? URI.parse(uri_or_url) : uri_or_url
11
+
12
+ http = Net::HTTP.new(uri.host)
13
+
14
+ http.open_timeout = self.open_timeout || 30
15
+ http.read_timeout = self.read_timeout || 60
16
+
17
+ response = http.start do
18
+ http.get(uri.request_uri)
19
+ end
20
+
21
+ case response
22
+ when Net::HTTPSuccess
23
+ response
24
+ when Net::HTTPMovedPermanently, Net::HTTPRedirection
25
+ redirect_uri = URI::parse(response.header['location'])
26
+ return get_response(redirect_uri, limit -1 )
27
+ else
28
+ raise "Error connecting to Amazon. response: #{response.to_w}"
29
+ end
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module AmazonPaApi
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: amazon_pa_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - sakura-computer
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: The purpose of this gem is to make use of Amazon Product Advertising
56
+ API and pull easily Amazon products information.
57
+ email:
58
+ - ishimoto.masaru@sakura-computer.co.jp
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - .gitignore
64
+ - .rspec
65
+ - .travis.yml
66
+ - CODE_OF_CONDUCT.md
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - amazon_pa_api.gemspec
72
+ - bin/console
73
+ - bin/setup
74
+ - lib/amazon_pa_api.rb
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
79
+ - lib/amazon_pa_api/request.rb
80
+ - lib/amazon_pa_api/version.rb
81
+ homepage: https://github.com/sakura-computer/amazon_pa_api
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.4.8
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: For Amazon Product Advertiseing API.
105
+ test_files: []
106
+ has_rdoc: