amazon-ecs 1.0.0 → 1.1.0

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.
data/CHANGELOG CHANGED
@@ -1,3 +1,8 @@
1
+ 1.1.0 2010-11-11
2
+ ----------------
3
+ * Add get_elements, get_element, and attributes instance methods in Amazon::Element
4
+ * Deprecate Amazon::Element#search_and_convert
5
+
1
6
  1.0.0 2010-11-09
2
7
  ----------------
3
8
  * Set default Amazon API version to 2010-10-01
data/README CHANGED
@@ -1,16 +1,15 @@
1
1
  == amazon-ecs
2
2
 
3
- Generic Product Advertising API (Amazon E-commerce REST API) using Hpricot with configurable
4
- default options and method call options. Uses Response and
5
- Element wrapper classes for easy access to REST XML output.
3
+ Generic Product Advertising Ruby API using Hpricot. Uses Response and
4
+ Element wrapper classes for easy access to the REST API XML output.
6
5
 
7
6
  It is generic, so you can easily extend <tt>Amazon::Ecs</tt> to support
8
7
  other not implemented REST operations; and it is also generic because it just wraps around
9
8
  Hpricot element object, instead of providing one-to-one object/attributes to XML elements map.
10
9
 
11
- If in the future, there is a change in REST XML output structure,
12
- no changes will be required on <tt>amazon-ecs</tt> library,
13
- instead you just need to change the element path.
10
+ The idea is as the API evolves, there is a change in REST XML output structure,
11
+ no updates will be required on <tt>amazon-ecs</tt> gem,
12
+ instead you just need to update the element path.
14
13
 
15
14
  == INSTALLATION
16
15
 
@@ -18,22 +17,22 @@ instead you just need to change the element path.
18
17
 
19
18
  == EXAMPLE
20
19
 
21
- require 'amazon/ecs'
20
+ require "amazon/ecs"
22
21
 
23
22
  # set the default options; options will be camelized and converted to REST request parameters.
24
- Amazon::Ecs.options = {:aWS_access_key_id => [your access key]}
23
+ Amazon::Ecs.options = {:aWS_access_key_id => "[your access key]"}
25
24
 
26
25
  # to generate signed requests include your secret key:
27
- Amazon::Ecs.options = {:aWS_access_key_id => [your developer token], :aWS_secret_key => [your secret access key]}
26
+ Amazon::Ecs.options = {:aWS_access_key_id => "[your developer token]", :aWS_secret_key => "[your secret access key]"}
28
27
 
29
28
  # alternatively,
30
29
  Amazon::Ecs.configure do |options|
31
- options[:aWS_access_key_id] = [your access key]
32
- options[:aWS_secret_key] = [you secret key]
30
+ options[:aWS_access_key_id] = "[your access key]"
31
+ options[:aWS_secret_key] = "[you secret key]"
33
32
  end
34
33
 
35
34
  # options provided on method call will merge with the default options
36
- res = Amazon::Ecs.item_search('ruby', {:response_group => 'Medium', :sort => 'salesrank'})
35
+ res = Amazon::Ecs.item_search("ruby", {:response_group => "Medium", :sort => "salesrank"})
37
36
 
38
37
  # some common response object methods
39
38
  res.is_valid_request? # return true if request is valid
@@ -46,22 +45,31 @@ instead you just need to change the element path.
46
45
  # traverse through each item (Amazon::Element)
47
46
  res.items.each do |item|
48
47
  # retrieve string value using XML path
49
- item.get('asin')
50
- item.get('itemattributes/title')
48
+ item.get("asin")
49
+ item.get("itemattributes/title")
51
50
 
52
- # or return Amazon::Element instance
53
- atts = item.search_and_convert('itemattributes')
54
- atts.get('title')
51
+ # return Amazon::Element instance
52
+ item_attributes = item.get_element("itemattributes")
53
+ item_attributes.get("title")
55
54
 
56
55
  # return first author or a string array of authors
57
- atts.get('author') # 'Author 1'
58
- atts.get_array('author') # ['Author 1', 'Author 2', ...]
56
+ item_attributes.get("author") # "Author 1"
57
+ item_attributes.get_array("author") # ["Author 1", "Author 2", ...]
59
58
 
60
59
  # return an hash of children text values with the element names as the keys
61
- item.get_hash('smallimage') # {:url => ..., :width => ..., :height => ...}
60
+ item.get_hash("smallimage") # {:url => ..., :width => ..., :height => ...}
61
+
62
+ # return the first matching path as Amazon::Element
63
+ item_height = item.get_element("itemdimensions/height")
64
+
65
+ # retrieve attributes from Amazon::Element
66
+ item_height.attributes["units"] # "hundredths-inches"
67
+
68
+ # return an array of Amazon::Element
69
+ authors = item.get_elements("author")
62
70
 
63
- # note that '/' returns Hpricot::Elements array object, nil if not found
64
- reviews = item/'editorialreview'
71
+ # return Hpricot::Elements object or nil if not found
72
+ reviews = item/"editorialreview"
65
73
 
66
74
  # traverse through Hpricot elements
67
75
  reviews.each do |review|
@@ -69,33 +77,26 @@ instead you just need to change the element path.
69
77
  Amazon::Element.get_hash(review) # [:source => ..., :content ==> ...]
70
78
 
71
79
  # Or to get unescaped HTML values
72
- Amazon::Element.get_unescaped(review, 'source')
73
- Amazon::Element.get_unescaped(review, 'content')
80
+ Amazon::Element.get_unescaped(review, "source")
81
+ Amazon::Element.get_unescaped(review, "content")
74
82
 
75
83
  # Or this way
76
84
  el = Amazon::Element.new(review)
77
- el.get_unescaped('source')
78
- el.get_unescaped('content')
85
+ el.get_unescaped("source")
86
+ el.get_unescaped("content")
79
87
  end
80
-
81
- # returns Amazon::Element instead of string
82
- item.search_and_convert('itemattributes').
83
88
  end
84
89
 
85
- Refer to Amazon ECS documentation for more information on Amazon REST request parameters and XML output:
86
- http://docs.amazonwebservices.com/AWSEcommerceService/2006-09-13/
90
+ Refer to Amazon Product Advertising API documentation for more information
91
+ on Amazon REST request parameters and XML output:
92
+ https://affiliate-program.amazon.com/gp/advertising/api/detail/main.html
87
93
 
88
94
  To get a sample of Amazon REST response XML output, use AWSZone.com scratch pad:
89
95
  http://www.awszone.com/scratchpads/aws/ecs.us/index.aws
90
96
 
91
97
  == SOURCE CODES
92
98
 
93
- * http://github.com/jugend/amazon-ecs/tree/master
94
-
95
- == LINKS
96
-
97
99
  * http://github.com/jugend/amazon-ecs
98
- * http://www.pluitsolutions.com/amazon-ecs
99
100
 
100
101
  == CREDITS
101
102
 
@@ -282,14 +282,27 @@ module Amazon
282
282
  elements
283
283
  end
284
284
 
285
- # Find Hpricot::Elements matching the given path, and convert to Amazon::Element.
286
- # Returns an array Amazon::Elements if more than Hpricot::Elements size is greater than 1.
285
+ # Return an array of Amazon::Element matching the given path, or Amazon::Element if there
286
+ # is only one element found.
287
+ #
288
+ # <b>DEPRECATED:</b> Please use <tt>get_elements</tt> and <tt>get_element</tt> instead.
287
289
  def search_and_convert(path)
290
+ elements = self.get_elements(path)
291
+ return elements.first if elements and elements.size == 1
292
+ elements
293
+ end
294
+
295
+ # Return an array of Amazon::Element matching the given path
296
+ def get_elements(path)
288
297
  elements = self./(path)
289
298
  return unless elements
290
299
  elements = elements.map{|element| Element.new(element)}
291
- return elements.first if elements.size == 1
292
- elements
300
+ end
301
+
302
+ # Similar with search_and_convert but always return first element if more than one elements found
303
+ def get_element(path)
304
+ elements = get_elements(path)
305
+ elements[0] if elements
293
306
  end
294
307
 
295
308
  # Get the text value of the given path, leave empty to retrieve current element value.
@@ -311,7 +324,12 @@ module Amazon
311
324
  def get_hash(path='')
312
325
  Element.get_hash(@element, path)
313
326
  end
314
-
327
+
328
+ def attributes
329
+ return unless self.elem
330
+ self.elem.attributes
331
+ end
332
+
315
333
  # Similar to #get, except an element object must be passed-in.
316
334
  def self.get(element, path='')
317
335
  return unless element
@@ -122,5 +122,32 @@ class Amazon::EcsTest < Test::Unit::TestCase
122
122
  assert authors.is_a?(Array)
123
123
  assert 3, authors.size
124
124
  assert_equal "Dave Thomas", authors.first.get
125
- end
125
+ end
126
+
127
+ def test_get_elements
128
+ resp = Amazon::Ecs.item_lookup('0974514055')
129
+ item = resp.first_item
130
+
131
+ authors = item.get_elements("author")
132
+ assert authors.is_a?(Array)
133
+ assert 3, authors.size
134
+ assert authors.first.is_a?(Amazon::Element)
135
+ assert_equal "Dave Thomas", authors.first.get
136
+
137
+ asin = item.get_elements("asin")
138
+ assert asin.is_a?(Array)
139
+ assert 1, authors.size
140
+ end
141
+
142
+ def test_get_element_and_attributes
143
+ resp = Amazon::Ecs.item_lookup('0974514055')
144
+ item = resp.first_item
145
+
146
+ first_author = item.get_element("author")
147
+ assert_equal "Dave Thomas", first_author.get
148
+ assert_equal nil, first_author.attributes['unknown']
149
+
150
+ item_height = item.get_element("itemdimensions/height")
151
+ assert_equal "hundredths-inches", item_height.attributes['units']
152
+ end
126
153
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: amazon-ecs
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
+ - 1
8
9
  - 0
9
- - 0
10
- version: 1.0.0
10
+ version: 1.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Herryanto Siatono
@@ -15,7 +15,7 @@ autorequire: name
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-10 00:00:00 +08:00
18
+ date: 2010-11-11 00:00:00 +08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency