amazon-ruby 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,25 @@
1
+ module AmazonRuby
2
+ module Cart
3
+
4
+ def add_to_cart()
5
+ puts "ADD TO CART"
6
+ end
7
+
8
+ def clear_cart()
9
+ puts "CLEAR CART"
10
+ end
11
+
12
+ def create_cart()
13
+ puts "CREATE CART"
14
+ end
15
+
16
+ def get_cart()
17
+ puts "GET CART"
18
+ end
19
+
20
+ def modify_cart()
21
+ puts "MODIFY CART"
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,102 @@
1
+ require "amazon-ruby/cart"
2
+ require "amazon-ruby/product"
3
+ require "amazon-ruby/response"
4
+
5
+ module AmazonRuby
6
+ class Client
7
+
8
+ include Cart
9
+
10
+ include Product
11
+
12
+ API_VERSION = '2011-08-01'
13
+ API_SERVICE = 'AWSECommerceService'
14
+
15
+ def initialize(country, amazon_key, amazon_secret, amazon_associate_tag)
16
+ @locale = Locale.new(country);
17
+ @amazon_key = amazon_key
18
+ @amazon_secret = amazon_secret
19
+ @amazon_associate_tag = amazon_associate_tag
20
+ end
21
+
22
+ def amazon_key
23
+ @amazon_key
24
+ end
25
+
26
+ def amazon_secret
27
+ @amazon_secret
28
+ end
29
+
30
+ def amazon_associate_tag
31
+ @amazon_associate_tag
32
+ end
33
+
34
+ def locale
35
+ @locale
36
+ end
37
+
38
+ def request_params
39
+ @request_params
40
+ end
41
+
42
+ def base_params
43
+ {
44
+ 'AWSAccessKeyId' => @amazon_key,
45
+ 'AssociateTag' => @amazon_associate_tag,
46
+ 'Service' => API_SERVICE,
47
+ 'Timestamp' => Time.now.utc.strftime('%Y-%m-%dT%H:%M:%SZ'),
48
+ 'Version' => API_VERSION
49
+ }
50
+ end
51
+
52
+ #Get all parameters
53
+ def all_params
54
+ base_params.merge(@request_params)
55
+ end
56
+
57
+ #Do the request
58
+ def get(sent_params)
59
+ raise KeyNotFound unless @amazon_key
60
+ raise TagNotFound unless @amazon_associate_tag
61
+ raise SecretNotFound unless @amazon_secret
62
+
63
+ #Reset parameters to new parameters for new request
64
+ @request_params = sent_params
65
+
66
+ #Do request
67
+ resp = Net::HTTP.get_response(url)
68
+ Response.new(resp.body, resp.code)
69
+ end
70
+
71
+ #Build query string from parameter hash
72
+ def query_string
73
+ all_params.sort.map { |k, v| "#{k}=" + escape(v) }.join('&')
74
+ end
75
+
76
+ # Adds a signature to a query
77
+ def sign(unsigned_query)
78
+ digest = OpenSSL::Digest::Digest.new('sha256')
79
+ url_string = ['GET', @locale.host, '/onca/xml', unsigned_query].join("\n")
80
+ hmac = OpenSSL::HMAC.digest(digest, @amazon_secret, url_string)
81
+ signature = escape([hmac].pack('m').chomp)
82
+
83
+ "#{unsigned_query}&Signature=#{signature}"
84
+ end
85
+
86
+ # The Amazon URL.
87
+ def url
88
+ URI::HTTP.build(
89
+ :host => @locale.host,
90
+ :path => '/onca/xml',
91
+ :query => sign(query_string))
92
+ end
93
+
94
+ #Escape value
95
+ def escape(value)
96
+ value.gsub(/([^a-zA-Z0-9_.~-]+)/) do
97
+ '%' + $1.unpack('H2' * $1.bytesize).join('%').upcase
98
+ end
99
+ end
100
+
101
+ end
102
+ end
@@ -0,0 +1,18 @@
1
+ module AmazonRuby
2
+
3
+ #Amazon key not found
4
+ class KeyNotFound < ArgumentError; end
5
+
6
+ #Amazon secret not found
7
+ class SecretNotFound < ArgumentError; end
8
+
9
+ #Amazon tag not found
10
+ class TagNotFound < ArgumentError; end
11
+
12
+ #Unsupported Country
13
+ class UnsupportedCountry < ArgumentError; end
14
+
15
+ #Invalid HTTP Client
16
+ class InvalidHttpClient < ArgumentError; end
17
+
18
+ end
@@ -0,0 +1,34 @@
1
+ module AmazonRuby
2
+ class Locale
3
+
4
+ #Country hosts
5
+ HOSTS = {
6
+ 'ca' => 'ecs.amazonaws.ca',
7
+ 'cn' => 'webservices.amazon.cn',
8
+ 'de' => 'ecs.amazonaws.de',
9
+ 'fr' => 'ecs.amazonaws.fr',
10
+ 'it' => 'webservices.amazon.it',
11
+ 'jp' => 'ecs.amazonaws.jp',
12
+ 'us' => 'ecs.amazonaws.com',
13
+ 'uk' => 'ecs.amazonaws.co.uk'
14
+ }
15
+
16
+ #Set country on initialization, raise InvalidLocale if not found
17
+ def initialize(country)
18
+ raise UnsupportedCountry unless HOSTS.keys.include?(country)
19
+ @host = HOSTS[country]
20
+ @country = country
21
+ end
22
+
23
+ #Host
24
+ def host
25
+ @host
26
+ end
27
+
28
+ #Country
29
+ def country
30
+ @country
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,34 @@
1
+ module AmazonRuby
2
+ module Product
3
+
4
+ #Hacky mess
5
+ OPERATION_ITEM_LOOKUP = 'ItemLookup'
6
+
7
+ ID_TYPE_UPC = 'UPC'
8
+ ID_TYPE_EAN = 'EAN'
9
+ ID_TYPE_ISBN = 'ISBN'
10
+ ID_TYPE_SKU = 'SKU'
11
+
12
+ SEARCH_INDEX_ALL = 'All'
13
+
14
+ RESPONSE_GROUP_ITEM_ATTRIBUTES = 'ItemAttributes'
15
+
16
+ def itemSearch()
17
+ puts "DO ITEM SEARCH"
18
+ end
19
+
20
+ def item_lookup(id,item_type)
21
+ request_params = {'Operation' => OPERATION_ITEM_LOOKUP,'ItemId' => id,'IdType' => ID_TYPE_UPC, 'SearchIndex' => SEARCH_INDEX_ALL, 'ResponseGroup' => RESPONSE_GROUP_ITEM_ATTRIBUTES }
22
+ get(request_params)
23
+ end
24
+
25
+ def browse_node_lookup()
26
+ puts "DO BROWSE NODE LOOKUP"
27
+ end
28
+
29
+ def similar_lookup()
30
+ puts "DO SIMILAR LOOKUP"
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,80 @@
1
+ module AmazonRuby
2
+ class Response
3
+
4
+ def response_body
5
+ @response_body
6
+ end
7
+
8
+ def response_code
9
+ @response_code
10
+ end
11
+
12
+ def initialize(response_body, response_code)
13
+ @response_body = response_body
14
+ @response_code = response_code.to_i
15
+ @response_xml = nil
16
+ end
17
+
18
+ def errors
19
+ find('Error')
20
+ end
21
+
22
+ def find(attribute)
23
+ xml.xpath("//xmlns:#{attribute}").map { |e| parse_xml(e) }
24
+ end
25
+ alias [] find
26
+
27
+ def error_count
28
+ errors.count
29
+ end
30
+
31
+ def valid_response
32
+ response_body == 200 ? true : false;
33
+ end
34
+
35
+ # The XML document.
36
+ def xml
37
+ Nokogiri::XML(@response_body)
38
+ end
39
+
40
+ #Such an ugly hacky mess...
41
+ def parse_xml(xml_input)
42
+ case xml_input
43
+ when Nokogiri::XML::Document
44
+ parse_xml(xml_input.root)
45
+ when Nokogiri::XML::Element
46
+ hsh = {}
47
+
48
+ xml_input.attributes.each_pair do |key, attr|
49
+ hsh[key] = attr.value
50
+ end
51
+
52
+ xml_input.children.each do |child|
53
+ result = parse_xml(child)
54
+
55
+ if child.name == 'text'
56
+ if hsh.empty?
57
+ return result
58
+ else
59
+ hsh['__content__'] = result
60
+ end
61
+ elsif hsh[child.name]
62
+ case hsh[child.name]
63
+ when Array
64
+ hsh[child.name] << result
65
+ else
66
+ hsh[child.name] = [hsh[child.name]] << result
67
+ end
68
+ else
69
+ hsh[child.name] = result
70
+ end
71
+ end
72
+
73
+ hsh
74
+ else
75
+ xml_input.content.to_s
76
+ end
77
+ end
78
+
79
+ end
80
+ end
@@ -0,0 +1,3 @@
1
+ module AmazonRuby
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,18 @@
1
+ require 'nokogiri'
2
+ require 'openssl'
3
+
4
+ require "amazon-ruby/version"
5
+ require "amazon-ruby/exception"
6
+ require "amazon-ruby/locale"
7
+ require "amazon-ruby/client"
8
+
9
+ module AmazonRuby
10
+
11
+ class << self
12
+ #Alias for AmazonRuby::Client.new
13
+ def new(country, amazon_key, amazon_secret, amazon_associate_tag)
14
+ AmazonRuby::Client.new(country, amazon_key, amazon_secret, amazon_associate_tag);
15
+ end
16
+ end
17
+
18
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: amazon-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - rhoppes
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-13 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: &70348662472500 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.4'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70348662472500
25
+ description: A Ruby wrapper for the Amazon Advertising API
26
+ email:
27
+ - rhoppes@zappos.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - lib/amazon-ruby/cart.rb
33
+ - lib/amazon-ruby/client.rb
34
+ - lib/amazon-ruby/exception.rb
35
+ - lib/amazon-ruby/locale.rb
36
+ - lib/amazon-ruby/product.rb
37
+ - lib/amazon-ruby/response.rb
38
+ - lib/amazon-ruby/version.rb
39
+ - lib/amazon-ruby.rb
40
+ homepage: ''
41
+ licenses: []
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project: amazon-ruby
60
+ rubygems_version: 1.8.10
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: Amazon-Ruby API wrapper
64
+ test_files: []