amazon-ruby 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,44 +1,89 @@
1
- require "amazon-ruby/cart"
2
- require "amazon-ruby/product"
3
1
  require "amazon-ruby/response"
2
+ require "amazon-ruby/client/itemtype"
3
+ require "amazon-ruby/client/operation"
4
+ require "amazon-ruby/client/responsegroup"
5
+ require "amazon-ruby/client/searchindex"
6
+ require "amazon-ruby/client/countrycode"
4
7
 
5
8
  module AmazonRuby
6
9
  class Client
7
10
 
8
- include Cart
9
-
10
- include Product
11
-
12
11
  API_VERSION = '2011-08-01'
13
12
  API_SERVICE = 'AWSECommerceService'
14
13
 
15
- def initialize(country, amazon_key, amazon_secret, amazon_associate_tag)
14
+ def initialize(country, amazon_key, amazon_secret, amazon_associate_tag)
16
15
  @locale = Locale.new(country);
17
16
  @amazon_key = amazon_key
18
17
  @amazon_secret = amazon_secret
19
18
  @amazon_associate_tag = amazon_associate_tag
20
19
  end
21
20
 
21
+ #Amazon access key id
22
22
  def amazon_key
23
23
  @amazon_key
24
24
  end
25
25
 
26
+ #Amazon secret access key
26
27
  def amazon_secret
27
28
  @amazon_secret
28
29
  end
29
30
 
31
+ #Amazon associate tag
30
32
  def amazon_associate_tag
31
33
  @amazon_associate_tag
32
34
  end
33
35
 
36
+ #Locale
34
37
  def locale
35
38
  @locale
36
39
  end
37
40
 
38
- def request_params
39
- @request_params
41
+ def add_to_cart()
42
+ raise UnsupportedFeature
43
+ end
44
+
45
+ def clear_cart()
46
+ raise UnsupportedFeature
47
+ end
48
+
49
+ def create_cart()
50
+ raise UnsupportedFeature
51
+ end
52
+
53
+ def get_cart()
54
+ raise UnsupportedFeature
55
+ end
56
+
57
+ def modify_cart()
58
+ raise UnsupportedFeature
59
+ end
60
+
61
+ def itemSearch()
62
+ raise UnsupportedFeature
63
+ end
64
+
65
+ def item_lookup(id, item_type)
66
+ request_params = {
67
+ 'Operation' => AmazonRuby::Client::Operation::ITEM_LOOKUP,
68
+ 'ItemId' => id,
69
+ 'IdType' => AmazonRuby::Client::ItemType::UPC,
70
+ 'SearchIndex' => AmazonRuby::Client::SearchIndex::ALL,
71
+ 'ResponseGroup' => AmazonRuby::Client::ResponseGroup::ITEM_ATTRIBUTES
72
+ }
73
+ get(request_params)
74
+ end
75
+
76
+ def browse_node_lookup()
77
+ raise UnsupportedFeature
40
78
  end
41
79
 
80
+ def similar_lookup()
81
+ raise UnsupportedFeature
82
+ end
83
+
84
+ private
85
+
86
+ #Base parameters that should be merged with request parameters
42
87
  def base_params
43
88
  {
44
89
  'AWSAccessKeyId' => @amazon_key,
@@ -49,28 +94,15 @@ module AmazonRuby
49
94
  }
50
95
  end
51
96
 
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)
97
+ #Perform request
98
+ def get(request_params)
59
99
  raise KeyNotFound unless @amazon_key
60
100
  raise TagNotFound unless @amazon_associate_tag
61
101
  raise SecretNotFound unless @amazon_secret
62
102
 
63
- #Reset parameters to new parameters for new request
64
- @request_params = sent_params
65
-
66
103
  #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('&')
104
+ response = Net::HTTP.get_response(url(request_params))
105
+ Response.new(response)
74
106
  end
75
107
 
76
108
  # Adds a signature to a query
@@ -83,8 +115,11 @@ module AmazonRuby
83
115
  "#{unsigned_query}&Signature=#{signature}"
84
116
  end
85
117
 
86
- # The Amazon URL.
87
- def url
118
+ # The Amazon URL.
119
+ def url(request_params)
120
+ combined_params = base_params.merge(request_params)
121
+ query_string = combined_params.sort.map { |k, v| "#{k}=" + escape(v) }.join('&')
122
+
88
123
  URI::HTTP.build(
89
124
  :host => @locale.host,
90
125
  :path => '/onca/xml',
@@ -0,0 +1,15 @@
1
+ module AmazonRuby
2
+ class Client
3
+ module CountryCode
4
+ CANADA = 'ca'
5
+ CHINA = 'cn'
6
+ GERMANY = 'de'
7
+ FRANCE = 'fr'
8
+ ITALY = 'it'
9
+ JAPAN = 'jp'
10
+ UNITED_STATES = 'us'
11
+ UNITED_KINGDOM = 'uk'
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,11 @@
1
+ module AmazonRuby
2
+ class Client
3
+ module ItemType
4
+ UPC = 'UPC'
5
+ EAN = 'EAN'
6
+ ISBN = 'ISBN'
7
+ SKU = 'SKU'
8
+ end
9
+ end
10
+ end
11
+
@@ -0,0 +1,25 @@
1
+ module AmazonRuby
2
+ class Client
3
+ module Operation
4
+ BROWSE_NODE_LOOKUP = 'BrowseNodeLookup'
5
+ ITEM_LOOKUP = 'ItemLookup'
6
+ ITEM_SEARCH = 'ItemSearch'
7
+ SIMILARITY_LOOKUP = 'SimilarityLookup'
8
+
9
+ #Not Yet Implemented
10
+ CUSTOMER_CONTENT_LOOKUP = 'CustomerContentLookup'
11
+ CUSTOMER_CONTENT_SEARCH = 'CustomerContentSearch'
12
+ HELP = 'Help'
13
+ LIST_LOOKUP = 'ListLookup'
14
+ LIST_SEARCH = 'ListSearch'
15
+ SELLER_LISTING_LOOKUP = 'SellerListingLookup'
16
+ SELLER_LISTING_SEARCH = 'SellerListingSearch'
17
+ SELLER_LOOKUP = 'SellerLookup'
18
+ TAG_LOOKUP = 'TagLookup'
19
+ TRANSACTION_LOOKUP = 'TransactionLookup'
20
+ VEHICLE_PART_LOOKUP = 'VehiclePartLookup'
21
+ VEHICLE_PART_SEARCH = 'VehiclePartSearch'
22
+ VEHICLE_SEARCH = 'VehicleSearch'
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,7 @@
1
+ module AmazonRuby
2
+ class Client
3
+ module ResponseGroup
4
+ ITEM_ATTRIBUTES = 'ItemAttributes'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module AmazonRuby
2
+ class Client
3
+ module SearchIndex
4
+ ALL = 'All'
5
+ end
6
+ end
7
+ end
@@ -15,4 +15,8 @@ module AmazonRuby
15
15
  #Invalid HTTP Client
16
16
  class InvalidHttpClient < ArgumentError; end
17
17
 
18
+ #Unsupported Feature
19
+ class UnsupportedFeature < ArgumentError; end
20
+
21
+
18
22
  end
@@ -1,3 +1,5 @@
1
+ require "amazon-ruby/client/countrycode"
2
+
1
3
  module AmazonRuby
2
4
  class Locale
3
5
 
@@ -1,35 +1,32 @@
1
1
  module AmazonRuby
2
2
  class Response
3
3
 
4
+ def initialize(response)
5
+ @response_body = response.body
6
+ @response_code = response.code.to_i
7
+ end
8
+
9
+ #Body of response
4
10
  def response_body
5
11
  @response_body
6
12
  end
7
13
 
14
+ #Response code
8
15
  def response_code
9
16
  @response_code
10
17
  end
11
18
 
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')
19
+ #Was the response valid?
20
+ def is_valid
21
+ @response_code == 200 ? true : false;
20
22
  end
21
23
 
24
+ #Return array of values
25
+ #This is pretty generic. This should be a LOT more robust.
22
26
  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;
27
+ result = Array.new
28
+ xml.xpath("//xmlns:#{attribute}").map do |e| result << e.text end
29
+ result
33
30
  end
34
31
 
35
32
  # The XML document.
@@ -37,44 +34,5 @@ module AmazonRuby
37
34
  Nokogiri::XML(@response_body)
38
35
  end
39
36
 
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
37
  end
80
38
  end
@@ -1,3 +1,3 @@
1
1
  module AmazonRuby
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: amazon-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-13 00:00:00.000000000Z
12
+ date: 2011-12-14 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
16
- requirement: &70348662472500 !ruby/object:Gem::Requirement
16
+ requirement: &70205913570620 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '1.4'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70348662472500
24
+ version_requirements: *70205913570620
25
25
  description: A Ruby wrapper for the Amazon Advertising API
26
26
  email:
27
27
  - rhoppes@zappos.com
@@ -29,15 +29,18 @@ executables: []
29
29
  extensions: []
30
30
  extra_rdoc_files: []
31
31
  files:
32
- - lib/amazon-ruby/cart.rb
32
+ - lib/amazon-ruby/client/countrycode.rb
33
+ - lib/amazon-ruby/client/itemtype.rb
34
+ - lib/amazon-ruby/client/operation.rb
35
+ - lib/amazon-ruby/client/responsegroup.rb
36
+ - lib/amazon-ruby/client/searchindex.rb
33
37
  - lib/amazon-ruby/client.rb
34
38
  - lib/amazon-ruby/exception.rb
35
39
  - lib/amazon-ruby/locale.rb
36
- - lib/amazon-ruby/product.rb
37
40
  - lib/amazon-ruby/response.rb
38
41
  - lib/amazon-ruby/version.rb
39
42
  - lib/amazon-ruby.rb
40
- homepage: ''
43
+ homepage: http://www.zappos.com
41
44
  licenses: []
42
45
  post_install_message:
43
46
  rdoc_options: []
@@ -1,25 +0,0 @@
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
@@ -1,34 +0,0 @@
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