dpickett-ruby_amazon_associates 0.5.4

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,26 @@
1
+ 0.5.4 2008-10-21
2
+ * include Chris Martin's patches
3
+ * rename Gem to match Amazon's new name for ECS - Amazon Associates API
4
+ * update to git gemspec format
5
+
6
+ 0.5.3 2007-09-12
7
+ ----------------
8
+ * send_request to use default options.
9
+
10
+ 0.5.2 2007-09-08
11
+ ----------------
12
+ * Fixed Amazon::Element.get_unescaped error when result returned for given element path is nil
13
+
14
+ 0.5.1 2007-02-08
15
+ ----------------
16
+ * Fixed Amazon Japan and France URL error
17
+ * Removed opts.delete(:search_index) from item_lookup, SearchIndex param is allowed
18
+ when looking for a book with IdType other than the ASIN.
19
+ * Check for defined? RAILS_DEFAULT_LOGGER to avoid exception for non-rails ruby app
20
+ * Added check for LOGGER constant if RAILS_DEFAULT_LOGGER is not defined
21
+ * Added Ecs.configure(&proc) method for easier configuration of default options
22
+ * Added Element#search_and_convert method
23
+
24
+ 0.5.0 2006-09-12
25
+ ----------------
26
+ Initial Release
data/README ADDED
@@ -0,0 +1,93 @@
1
+ == amazon-ecs
2
+
3
+ Generic 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. It supports ECS 4.0.
6
+
7
+ It is generic, so you can easily extend <tt>Amazon::Ecs</tt> to support
8
+ other not implemented REST operations; and it is also generic because it just wraps around
9
+ Hpricot element object, instead of providing one-to-one object/attributes to XML elements map.
10
+
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.
14
+
15
+ Version: 0.5.4
16
+
17
+ == INSTALLATION
18
+
19
+ $ gem install amazon-ecs
20
+
21
+ == EXAMPLE
22
+
23
+ require 'amazon/ecs'
24
+
25
+ # set the default options; options will be camelized and converted to REST request parameters.
26
+ Amazon::Ecs.options = {:aWS_access_key_id => [your developer token]}
27
+
28
+ # options provided on method call will merge with the default options
29
+ res = Amazon::Ecs.item_search('ruby', {:response_group => 'Medium', :sort => 'salesrank'})
30
+
31
+ # some common response object methods
32
+ res.is_valid_request? # return true if request is valid
33
+ res.has_error? # return true if there is an error
34
+ res.error # return error message if there is any
35
+ res.total_pages # return total pages
36
+ res.total_results # return total results
37
+ res.item_page # return current page no if :item_page option is provided
38
+
39
+ # traverse through each item (Amazon::Element)
40
+ res.items.each do |item|
41
+ # retrieve string value using XML path
42
+ item.get('asin')
43
+ item.get('itemattributes/title')
44
+
45
+ # or return Amazon::Element instance
46
+ atts = item.search_and_convert('itemattributes')
47
+ atts.get('title')
48
+
49
+ # return first author or a string array of authors
50
+ atts.get('author') # 'Author 1'
51
+ atts.get_array('author') # ['Author 1', 'Author 2', ...]
52
+
53
+ # return an hash of children text values with the element names as the keys
54
+ item.get_hash('smallimage') # {:url => ..., :width => ..., :height => ...}
55
+
56
+ # note that '/' returns Hpricot::Elements array object, nil if not found
57
+ reviews = item/'editorialreview'
58
+
59
+ # traverse through Hpricot elements
60
+ reviews.each do |review|
61
+ # Getting hash value out of Hpricot element
62
+ Amazon::Element.get_hash(review) # [:source => ..., :content ==> ...]
63
+
64
+ # Or to get unescaped HTML values
65
+ Amazon::Element.get_unescaped(review, 'source')
66
+ Amazon::Element.get_unescaped(review, 'content')
67
+
68
+ # Or this way
69
+ el = Amazon::Element.new(review)
70
+ el.get_unescaped('source')
71
+ el.get_unescaped('content')
72
+ end
73
+
74
+ # returns Amazon::Element instead of string
75
+ item.search_and_convert('itemattributes').
76
+ end
77
+
78
+ Refer to Amazon ECS documentation for more information on Amazon REST request parameters and XML output:
79
+ http://docs.amazonwebservices.com/AWSEcommerceService/2006-09-13/
80
+
81
+ To get a sample of Amazon REST response XML output, use AWSZone.com scratch pad:
82
+ http://www.awszone.com/scratchpads/aws/ecs.us/index.aws
83
+
84
+ == LINKS
85
+
86
+ * http://amazon-ecs.rubyforge.org
87
+ * http://www.pluitsolutions.com/amazon-ecs
88
+
89
+ == LICENSE
90
+
91
+ (The MIT License)
92
+
93
+ Copyright (c) 2006 Herryanto Siatono, Pluit Solutions
@@ -0,0 +1,38 @@
1
+ require File.dirname(__FILE__) + "/../test_helper"
2
+
3
+ class Amazon::BrowseNodeLookupTest < Test::Unit::TestCase
4
+
5
+ ## Test browse_node_lookup
6
+ def test_browse_node_lookup
7
+ resp = Amazon::Ecs.browse_node_lookup("5")
8
+ assert resp.is_valid_request?
9
+ browse_node_tags = resp.doc.get_elements_by_tag_name("browsenodeid")
10
+ browse_node_tags.each { |node| assert_equal("5", node.inner_text) }
11
+ assert_equal "TopSellers", resp.doc.get_elements_by_tag_name("responsegroup").inner_text
12
+ end
13
+
14
+ def test_browse_node_lookup_with_browse_node_info_response
15
+ resp = Amazon::Ecs.browse_node_lookup("5", :response_group => "BrowseNodeInfo")
16
+ assert resp.is_valid_request?
17
+ assert_equal "BrowseNodeInfo", resp.doc.get_elements_by_tag_name("responsegroup").inner_text
18
+ end
19
+
20
+ def test_browse_node_lookup_with_new_releases_response
21
+ resp = Amazon::Ecs.browse_node_lookup("5", :response_group => "NewReleases")
22
+ assert resp.is_valid_request?
23
+ assert_equal "NewReleases", resp.doc.get_elements_by_tag_name("responsegroup").inner_text
24
+ end
25
+
26
+ def test_browse_node_lookup_with_invalid_request
27
+ resp = Amazon::Ecs.browse_node_lookup(nil)
28
+ assert resp.has_error?
29
+ assert resp.error
30
+ end
31
+
32
+ def test_browse_node_lookup_with_no_result
33
+ resp = Amazon::Ecs.browse_node_lookup("abc")
34
+
35
+ assert resp.is_valid_request?
36
+ assert_match(/abc is not a valid value for BrowseNodeId/, resp.error)
37
+ end
38
+ end
@@ -0,0 +1,58 @@
1
+ require File.dirname(__FILE__) + "/../test_helper"
2
+
3
+ class Amazon::CartTest < Test::Unit::TestCase
4
+
5
+ # create a cart to store cart_id and hmac for add, get, modify, and clear tests
6
+ def setup
7
+ @asin = "0672328844"
8
+ resp = Amazon::Ecs.cart_create(@asin)
9
+ @cart_id = resp.doc.get_elements_by_tag_name("cartid").inner_text
10
+ @hmac = resp.doc.get_elements_by_tag_name("hmac").inner_text
11
+ item = resp.first_item
12
+ # run tests for cart_create with default quantity while we"re at it
13
+ assert resp.is_valid_request?
14
+ assert_equal @asin, item.get("asin")
15
+ assert_equal "1", item.get("quantity")
16
+ assert_not_nil @cart_id
17
+ assert_not_nil @hmac
18
+ end
19
+
20
+ # Test cart_get
21
+ def test_cart_get
22
+ resp = Amazon::Ecs.cart_get(@cart_id, @hmac)
23
+ assert resp.is_valid_request?
24
+ assert_not_nil resp.doc.get_elements_by_tag_name("purchaseurl").inner_text
25
+ end
26
+
27
+ # Test cart_modify
28
+ def test_cart_modify
29
+ resp = Amazon::Ecs.cart_get(@cart_id, @hmac)
30
+ cart_item_id = resp.doc.get_elements_by_tag_name("cartitemid").inner_text
31
+ resp = Amazon::Ecs.cart_modify(cart_item_id, @cart_id, @hmac, 2)
32
+ item = resp.first_item
33
+
34
+ assert resp.is_valid_request?
35
+ assert_equal "2", item.get("quantity")
36
+ assert_not_nil resp.doc.get_elements_by_tag_name("purchaseurl").inner_text
37
+ end
38
+
39
+ # Test cart_clear
40
+ def test_cart_clear
41
+ resp = Amazon::Ecs.cart_clear(@cart_id, @hmac)
42
+ assert resp.is_valid_request?
43
+ end
44
+
45
+ ## Test cart_create with a specified quantity
46
+ ## note this will create a separate cart
47
+ def test_cart_create_with_quantity
48
+ asin = "0672328844"
49
+ resp = Amazon::Ecs.cart_create(asin, :quantity => 2)
50
+ assert resp.is_valid_request?
51
+ item = resp.first_item
52
+ assert_equal asin, item.get("asin")
53
+ assert_equal "2", item.get("quantity")
54
+ assert_not_nil resp.doc.get_elements_by_tag_name("cartid").inner_text
55
+ assert_not_nil resp.doc.get_elements_by_tag_name("hmac").inner_text
56
+ end
57
+
58
+ end
@@ -0,0 +1,106 @@
1
+ require File.dirname(__FILE__) + "/../test_helper"
2
+
3
+ class Amazon::EcsTest < Test::Unit::TestCase
4
+ def setup
5
+ Amazon::Ecs.configure do |options|
6
+ options[:response_group] = "Large"
7
+
8
+ end
9
+ end
10
+ ## Test item_search
11
+ def test_item_search
12
+ resp = Amazon::Ecs.item_search("ruby")
13
+ assert(resp.is_valid_request?)
14
+ assert(resp.total_results >= 3600)
15
+ assert(resp.total_pages >= 360)
16
+ end
17
+
18
+ def test_item_search_with_paging
19
+ resp = Amazon::Ecs.item_search("ruby", :item_page => 2)
20
+ assert resp.is_valid_request?
21
+ assert 2, resp.item_page
22
+ end
23
+
24
+ def test_item_search_with_invalid_request
25
+ resp = Amazon::Ecs.item_search(nil)
26
+ assert !resp.is_valid_request?
27
+ end
28
+
29
+ def test_item_search_with_no_result
30
+ resp = Amazon::Ecs.item_search("afdsafds")
31
+
32
+ assert resp.is_valid_request?
33
+ assert_equal "We did not find any matches for your request.",
34
+ resp.error
35
+ end
36
+
37
+ def test_item_search_uk
38
+ resp = Amazon::Ecs.item_search("ruby", :country => :uk)
39
+ assert resp.is_valid_request?
40
+ end
41
+
42
+ def test_item_search_by_author
43
+ resp = Amazon::Ecs.item_search("dave", :type => :author)
44
+ assert resp.is_valid_request?
45
+ end
46
+
47
+ def test_item_get
48
+ resp = Amazon::Ecs.item_search("0974514055")
49
+ item = resp.first_item
50
+
51
+ # test get
52
+ assert_equal "Programming Ruby: The Pragmatic Programmers' Guide, Second Edition",
53
+ item.get("itemattributes/title")
54
+
55
+ # test get_array
56
+ assert_equal ["Dave Thomas", "Chad Fowler", "Andy Hunt"],
57
+ item.get_array("author")
58
+
59
+ # test get_hash
60
+ small_image = item.get_hash("smallimage")
61
+
62
+ assert_equal 3, small_image.keys.size
63
+ assert small_image[:url] != nil
64
+ assert_equal "75", small_image[:height]
65
+ assert_equal "59", small_image[:width]
66
+
67
+ # test /
68
+ reviews = item/"editorialreview"
69
+ reviews.each do |review|
70
+ # returns unescaped HTML content, Hpricot escapes all text values
71
+ assert Amazon::Element.get_unescaped(review, "source")
72
+ assert Amazon::Element.get_unescaped(review, "content")
73
+ end
74
+ end
75
+
76
+ ## Test item_lookup
77
+ def test_item_lookup
78
+ resp = Amazon::Ecs.item_lookup("0974514055")
79
+ assert_equal "Programming Ruby: The Pragmatic Programmers' Guide, Second Edition",
80
+ resp.first_item.get("itemattributes/title")
81
+ end
82
+
83
+ def test_item_lookup_with_invalid_request
84
+ resp = Amazon::Ecs.item_lookup(nil)
85
+ assert resp.has_error?
86
+ assert resp.error
87
+ end
88
+
89
+ def test_item_lookup_with_no_result
90
+ resp = Amazon::Ecs.item_lookup("abc")
91
+
92
+ assert resp.is_valid_request?
93
+ assert_match(/ABC is not a valid value for ItemId/, resp.error)
94
+ end
95
+
96
+ def test_search_and_convert
97
+ resp = Amazon::Ecs.item_lookup("0974514055")
98
+ title = resp.first_item.get("itemattributes/title")
99
+ authors = resp.first_item.search_and_convert("author")
100
+
101
+ assert_equal "Programming Ruby: The Pragmatic Programmers' Guide, Second Edition", title
102
+ assert authors.is_a?(Array)
103
+ assert 3, authors.size
104
+ assert_equal "Dave Thomas", authors.first.get
105
+ end
106
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dpickett-ruby_amazon_associates
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.4
5
+ platform: ruby
6
+ authors:
7
+ - Herryanto Siatono
8
+ - Dan Pickett
9
+ autorequire: name
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2008-10-21 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: hpricot
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0.6"
24
+ version:
25
+ description:
26
+ email:
27
+ - herryanto@pluitsolutions.com, dpickett@enlightsolutions.com
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - README
34
+ - CHANGELOG
35
+ files:
36
+ - test/amazon/browse_node_lookup_test.rb
37
+ - test/amazon/cart_test.rb
38
+ - test/amazon/ecs_test.rb
39
+ - README
40
+ - CHANGELOG
41
+ has_rdoc: true
42
+ homepage: http://github.com/dpickett/ruby_amazon_associates/tree/master
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --line-numbers
46
+ - --inline-source
47
+ - --main
48
+ - README.textile
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.2.0
67
+ signing_key:
68
+ specification_version: 2
69
+ summary: Generic Amazon Associates Web Service (Formerly ECS) REST API. Supports ECS 4.0.
70
+ test_files:
71
+ - test/amazon/browse_node_lookup_test.rb
72
+ - test/amazon/cart_test.rb
73
+ - test/amazon/ecs_test.rb