papercavalier-ruby-aaws 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. data/.gitignore +3 -0
  2. data/COPYING +340 -0
  3. data/INSTALL +260 -0
  4. data/NEWS +808 -0
  5. data/README +679 -0
  6. data/README.rdoc +140 -0
  7. data/Rakefile +17 -0
  8. data/VERSION.yml +5 -0
  9. data/example/batch_operation +28 -0
  10. data/example/browse_node_lookup1 +46 -0
  11. data/example/customer_content_lookup1 +27 -0
  12. data/example/customer_content_search1 +21 -0
  13. data/example/example1 +78 -0
  14. data/example/help1 +24 -0
  15. data/example/item_lookup1 +56 -0
  16. data/example/item_lookup2 +56 -0
  17. data/example/item_search1 +30 -0
  18. data/example/item_search2 +37 -0
  19. data/example/item_search3 +23 -0
  20. data/example/list_lookup1 +29 -0
  21. data/example/list_search1 +30 -0
  22. data/example/multiple_operation1 +69 -0
  23. data/example/seller_listing_lookup1 +30 -0
  24. data/example/seller_listing_search1 +29 -0
  25. data/example/seller_lookup1 +45 -0
  26. data/example/shopping_cart1 +42 -0
  27. data/example/similarity_lookup1 +48 -0
  28. data/example/tag_lookup1 +34 -0
  29. data/example/transaction_lookup1 +25 -0
  30. data/example/vehicle_search +22 -0
  31. data/lib/amazon.rb +165 -0
  32. data/lib/amazon/aws.rb +1493 -0
  33. data/lib/amazon/aws/cache.rb +141 -0
  34. data/lib/amazon/aws/search.rb +464 -0
  35. data/lib/amazon/aws/shoppingcart.rb +537 -0
  36. data/lib/amazon/locale.rb +102 -0
  37. data/test/setup.rb +56 -0
  38. data/test/tc_amazon.rb +20 -0
  39. data/test/tc_aws.rb +160 -0
  40. data/test/tc_browse_node_lookup.rb +49 -0
  41. data/test/tc_customer_content_lookup.rb +49 -0
  42. data/test/tc_help.rb +44 -0
  43. data/test/tc_item_lookup.rb +47 -0
  44. data/test/tc_item_search.rb +105 -0
  45. data/test/tc_list_lookup.rb +60 -0
  46. data/test/tc_list_search.rb +44 -0
  47. data/test/tc_multiple_operation.rb +375 -0
  48. data/test/tc_operation_request.rb +64 -0
  49. data/test/tc_seller_listing_lookup.rb +47 -0
  50. data/test/tc_seller_listing_search.rb +55 -0
  51. data/test/tc_seller_lookup.rb +44 -0
  52. data/test/tc_serialisation.rb +107 -0
  53. data/test/tc_shopping_cart.rb +214 -0
  54. data/test/tc_similarity_lookup.rb +48 -0
  55. data/test/tc_tag_lookup.rb +24 -0
  56. data/test/tc_transaction_lookup.rb +24 -0
  57. data/test/tc_vehicle_operations.rb +118 -0
  58. data/test/ts_aws.rb +24 -0
  59. metadata +141 -0
@@ -0,0 +1,102 @@
1
+ # $Id: locale.rb,v 1.4 2009/10/29 09:05:02 ianmacd Exp $
2
+ #
3
+
4
+ catch :done do
5
+
6
+ begin
7
+ require 'net/geoip'
8
+ rescue LoadError
9
+ throw :done
10
+ end
11
+
12
+ module Amazon
13
+
14
+ # Use of this module requires the use of the GeoIP library from
15
+ # MaxMind[http://www.maxmind.com/]. It also requires the
16
+ # net-geoip[http://rubyforge.org/scm/?group_id=947] Ruby module to
17
+ # interface with it.
18
+ #
19
+ # Load this library as follows:
20
+ #
21
+ # require 'amazon/locale'
22
+ #
23
+ module Locale
24
+
25
+ # These country lists are obviously not complete.
26
+
27
+ # ISO 3166[http://www.iso.ch/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/iso_3166-1_decoding_table.html]
28
+ # codes of countries likely to want to shop in the *CA* locale.
29
+ #
30
+ CA = %w[ ca ]
31
+
32
+ # ISO 3166 codes of countries likely to want to shop in the *DE* locale.
33
+ #
34
+ DE = %w[ at ch de ]
35
+
36
+ # ISO 3166 codes of countries likely to want to shop in the *FR* locale.
37
+ #
38
+ FR = %w[ be fr ]
39
+
40
+ # ISO 3166 codes of countries likely to want to shop in the *JP* locale.
41
+ #
42
+ JP = %w[ jp ]
43
+
44
+ # ISO 3166 codes of countries likely to want to shop in the *UK* locale.
45
+ #
46
+ UK = %w[ ad al ba cy cz dk ee es fi fo gg gi gr gl hu ie im is it je
47
+ li lt lu lv mk mt nl no pl pt ro se si sk sm uk ]
48
+
49
+ # ISO 3166 codes of countries likely to want to shop in the *US* locale.
50
+ # Any countries not explicitly listed above default to the *US* locale.
51
+ #
52
+ US = %w[ mx us ]
53
+
54
+ # Exception class for geolocation errors.
55
+ #
56
+ class GeoError < StandardError; end
57
+
58
+ def Locale.localise(code)
59
+ code.downcase!
60
+
61
+ return 'ca' if CA.include? code
62
+ return 'de' if DE.include? code
63
+ return 'fr' if FR.include? code
64
+ return 'jp' if JP.include? code
65
+ return 'uk' if UK.include? code
66
+
67
+ 'us'
68
+ end
69
+ private_class_method :localise
70
+
71
+
72
+ # This will attempt to return a reasonable locale (*ca*, *de*, *fr*,
73
+ # *jp*, *uk* or *us*) to use for _host_.
74
+ #
75
+ # Example:
76
+ #
77
+ # get_locale_by_name( 'xs1.xs4all.nl' ) => "uk"
78
+ #
79
+ def Locale.get_locale_by_name(host)
80
+ cc = Net::GeoIP.new.country_code_by_name( host )
81
+ raise GeoError, "invalid host: #{host}" unless cc
82
+ localise( cc )
83
+ end
84
+
85
+ # This will attempt to return a reasonable locale (*ca*, *de*, *fr*,
86
+ # *jp*, *uk* or *us*) to use for the IP address _address_.
87
+ #
88
+ # Example:
89
+ #
90
+ # get_locale_by_addr( '217.110.207.55' ) => "de"
91
+ #
92
+ def Locale.get_locale_by_addr(address)
93
+ cc = Net::GeoIP.new.country_code_by_addr( address )
94
+ raise GeoError, "invalid address: #{address}" unless cc
95
+ localise( cc )
96
+ end
97
+
98
+ end
99
+
100
+ end
101
+
102
+ end
@@ -0,0 +1,56 @@
1
+ # $Id: setup.rb,v 1.6 2009/09/16 22:31:07 ianmacd Exp $
2
+ #
3
+
4
+ # Attempt to load Ruby/AWS using RubyGems.
5
+ #
6
+ begin
7
+ require 'rubygems'
8
+ gem 'ruby-aws'
9
+ rescue LoadError
10
+ # Either we don't have RubyGems or we don't have a gem of Ruby/AWS.
11
+ end
12
+
13
+ # Require the essential library, be it via RubyGems or the default way.
14
+ #
15
+ require 'amazon/aws/search'
16
+
17
+ include Amazon::AWS
18
+ include Amazon::AWS::Search
19
+
20
+ class AWSTest < Test::Unit::TestCase
21
+
22
+ # Figure out where the user config file is.
23
+ #
24
+ home = ENV['AMAZONRCDIR'] ||
25
+ ENV['HOME'] || ENV['HOMEDRIVE'] + ENV['HOMEPATH'] ||
26
+ ENV['USERPROFILE']
27
+ user_rcfile = ENV['AMAZONRCFILE'] || '.amazonrc'
28
+ rc = File.expand_path( File.join( home, user_rcfile ) )
29
+
30
+ # Replace the locale with UK.
31
+ #
32
+ lines = File.open( rc ) { |f| f.readlines }
33
+ lines.each { |l| l.sub!( /(locale\s*=\s*['"])..(['"])/, "\\1uk\\2" ) }
34
+
35
+ # Write a new config file for the purpose of unit testing.
36
+ #
37
+ test_rc = File.join( Dir.pwd, '.amazonrc' )
38
+ File.open( test_rc, 'w' ) { |f| f.puts lines } unless File.exist?( test_rc )
39
+
40
+ # Make sure the unit tests use the new config file.
41
+ #
42
+ ENV['AMAZONRCDIR'] = Dir.pwd
43
+
44
+ def setup
45
+ @rg = ResponseGroup.new( :Small )
46
+ @req = Request.new
47
+ @req.locale = 'uk'
48
+ @req.cache = false
49
+ @req.encoding = 'utf-8'
50
+ end
51
+
52
+ # The default_test method needs to be removed before Ruby 1.9.0.
53
+ #
54
+ undef_method :default_test if method_defined? :default_test
55
+
56
+ end
@@ -0,0 +1,20 @@
1
+ # $Id: tc_amazon.rb,v 1.1 2008/05/19 10:17:26 ianmacd Exp $
2
+ #
3
+
4
+ require 'test/unit'
5
+ require './setup'
6
+
7
+ class TestAmazonBasics < AWSTest
8
+
9
+ def test_uncamelise
10
+ str = 'ALongStringWithACRONYM'
11
+ uncamel_str = 'a_long_string_with_acronym'
12
+
13
+ # Ensure uncamelisation of strings occurs properly.
14
+ #
15
+ assert_equal( uncamel_str, Amazon::uncamelise( str ) )
16
+ assert_equal( 'asin', Amazon::uncamelise( 'ASIN' ) )
17
+
18
+ end
19
+
20
+ end
@@ -0,0 +1,160 @@
1
+ # $Id: tc_aws.rb,v 1.15 2010/02/20 23:59:02 ianmacd Exp $
2
+ #
3
+
4
+ require 'test/unit'
5
+ require './setup'
6
+ require 'fileutils'
7
+ require 'tmpdir'
8
+ require 'amazon/locale'
9
+
10
+ class TestAWSBasics < AWSTest
11
+
12
+ CACHE_DIR = Amazon::AWS::Cache::DEFAULT_CACHE_DIR
13
+ CACHE_PATH = File.join( Dir.tmpdir, 'aws_cache' )
14
+
15
+ def test_version
16
+ v = '1.8.6'
17
+ assert( RUBY_VERSION >= v, "Ruby version is lower than #{v}." )
18
+ end
19
+
20
+ def test_cache
21
+
22
+ # Assure we can create a cache with a non-default path.
23
+ #
24
+ c = Cache.new( CACHE_PATH )
25
+ assert( CACHE_PATH, c.path )
26
+ assert( File.directory?( c.path ) )
27
+ FileUtils.rmdir( c.path )
28
+
29
+ c = Cache.new
30
+
31
+ # Ensure that default cache path has been chosen.
32
+ #
33
+ assert( CACHE_DIR, c.path )
34
+
35
+ # Ensure that cache directory has been created.
36
+ #
37
+ assert( File.directory?( c.path ) )
38
+
39
+ f = File.join( c.path, 'foobar' )
40
+
41
+ # Just over a day ago.
42
+ #
43
+ t = Time.now - 86460
44
+
45
+ FileUtils.touch f, { :mtime => t }
46
+ assert( File.exist?( f ) )
47
+
48
+ # Ensure expired file is properly flushed.
49
+ #
50
+ c.flush_expired
51
+ assert( ! File.exist?( f ) )
52
+
53
+ FileUtils.touch f
54
+ assert( File.exist?( f ) )
55
+
56
+ # Ensure unexpired file is properly retained.
57
+ #
58
+ c.flush_expired
59
+ assert( File.exist?( f ) )
60
+
61
+ # Ensure file is properly flushed.
62
+ #
63
+ c.flush_all
64
+ assert( ! File.exist?( f ) )
65
+
66
+ h = Help.new( :ResponseGroup, :Large )
67
+ h_rg = ResponseGroup.new( :Help )
68
+ h.response_group = h_rg
69
+
70
+ # Ensure that file is not cached when no cache is desired.
71
+ #
72
+ @req.cache = false
73
+ resp = @req.search( h )
74
+ assert_equal( 0, Dir.glob( File.join( c.path, '*' ) ).size )
75
+
76
+ # Ensure that file is cached when cache is desired.
77
+ #
78
+ @req.cache = c
79
+ resp = @req.search( h )
80
+ assert_equal( 1, Dir.glob( File.join( c.path, '*' ) ).size )
81
+
82
+ # Flush it away.
83
+ #
84
+ c.flush_all
85
+ assert_equal( 0, Dir.glob( File.join( c.path, '*' ) ).size )
86
+
87
+ FileUtils.rmdir( c.path )
88
+
89
+ # Turn off caching for future tests. This happens in the setup method,
90
+ # anyway, but it can't hurt to tidy up after ourselves.
91
+ #
92
+ @req.cache = false
93
+
94
+ end
95
+
96
+ def test_config
97
+ # Test bad quoting.
98
+ #
99
+ assert_raise( Amazon::Config::ConfigError ) do
100
+ cf = Amazon::Config.new( <<' EOF' )
101
+ bad_syntax = 'bad quotes"
102
+ EOF
103
+ end
104
+
105
+ # Test good quoting.
106
+ #
107
+ assert_nothing_raised do
108
+ cf = Amazon::Config.new( <<' EOF' )
109
+ good_syntax = 'good quotes'
110
+ EOF
111
+ end
112
+
113
+ # Test that config files are properly read from $AMAZONRC.
114
+ #
115
+ Dir.mktmpdir do |td|
116
+ # First save old locations, in case they're not the default.
117
+ #
118
+ old_rcdir = ENV['AMAZONRCDIR']
119
+ old_rc_file = ENV['AMAZONRCFILE']
120
+
121
+ ENV['AMAZONRCDIR'] = td
122
+ ENV['AMAZONRCFILE'] = '.user_defined_name'
123
+ File.open( File.join( td, '.user_defined_name' ), 'w' ) do |tf|
124
+ tf.puts( 'foo = bar' )
125
+ end
126
+
127
+ cf = Amazon::Config.new
128
+ assert_equal( 'bar', cf['foo'] )
129
+
130
+ # Restore old locations.
131
+ #
132
+ ENV['AMAZONRCDIR'] = old_rcdir
133
+ ENV['AMAZONRCFILE'] = old_rc_file
134
+ end
135
+ end
136
+
137
+ def test_exceptions
138
+ assert_raise( Amazon::AWS::HTTPError ) { raise Amazon::AWS::HTTPError }
139
+ end
140
+
141
+ include Amazon
142
+
143
+ def test_geo
144
+
145
+ require 'net/geoip'
146
+
147
+ assert_equal( 'de', Locale.get_locale_by_name( 'www.marxer.org' ) )
148
+ assert_equal( 'jp', Locale.get_locale_by_name( 'ruby-lang.org' ) )
149
+ assert_equal( 'uk', Locale.get_locale_by_name( 'xs1.xs4all.nl' ) )
150
+ assert_equal( 'uk', Locale.get_locale_by_name( 'caliban.org' ) )
151
+ assert_equal( 'us', Locale.get_locale_by_name( 'google.com' ) )
152
+
153
+ # Ensure non-existent hostname causes an Amazon::Locale::GeoError.
154
+ #
155
+ assert_raise( Amazon::Locale::GeoError ) do
156
+ Locale.get_locale_by_name( 'marxer.org' )
157
+ end
158
+ end
159
+
160
+ end
@@ -0,0 +1,49 @@
1
+ # $Id: tc_browse_node_lookup.rb,v 1.3 2010/02/20 17:15:17 ianmacd Exp $
2
+ #
3
+
4
+ require 'test/unit'
5
+ require './setup'
6
+
7
+ class TestBrowseNodeLookup < AWSTest
8
+
9
+ def test_browse_node_lookup
10
+
11
+ bnl = BrowseNodeLookup.new( 694212 )
12
+ rg = ResponseGroup.new( :BrowseNodeInfo )
13
+ bnl.response_group = rg
14
+
15
+ response = @req.search( bnl )
16
+
17
+ results = response.kernel
18
+
19
+ # Ensure we got some actual results back.
20
+ #
21
+ assert( results.size > 0 )
22
+
23
+ end
24
+
25
+ def test_browse_node_lookup_class_method
26
+
27
+ response = Amazon::AWS.browse_node_lookup( 694212 )
28
+
29
+ results = response.kernel
30
+
31
+ # Ensure we got some actual results back.
32
+ #
33
+ assert( results.size > 0 )
34
+
35
+ end
36
+
37
+ def test_browse_node_lookup_class_method_block
38
+
39
+ Amazon::AWS.browse_node_lookup( '694212' ) do |r|
40
+
41
+ results = r.kernel
42
+
43
+ # Ensure we got some actual results back.
44
+ #
45
+ assert( results.size > 0 )
46
+ end
47
+ end
48
+
49
+ end
@@ -0,0 +1,49 @@
1
+ # $Id: tc_customer_content_lookup.rb,v 1.2 2010/02/20 17:15:17 ianmacd Exp $
2
+ #
3
+
4
+ require 'test/unit'
5
+ require './setup'
6
+
7
+ class TestCustomerContentLookup < AWSTest
8
+
9
+ def test_customer_content_lookup
10
+
11
+ @req.locale = 'us'
12
+ ccl = CustomerContentLookup.new( 'AJDWXANG1SYZP' )
13
+ rg = ResponseGroup.new( :CustomerReviews )
14
+ ccl.response_group = rg
15
+
16
+ response = @req.search( ccl )
17
+
18
+ review = response.customer_content_lookup_response.customers.customer.
19
+ customer_reviews.review[0]
20
+
21
+ # Ensure we got the right review.
22
+ #
23
+ assert_equal( 3746, review.content[0].size )
24
+
25
+ end
26
+
27
+ def test_customer_content_lookup_class_method
28
+
29
+ response = Amazon::AWS.customer_content_lookup( 'AA5QZU6TJQXEN' )
30
+
31
+ nickname = response.customer_content_lookup_response.customers.customer.
32
+ nickname
33
+
34
+ assert_equal( 'jimwood43', nickname )
35
+
36
+ end
37
+
38
+ def test_item_search_class_method_block
39
+
40
+ Amazon::AWS.customer_content_lookup( 'AA5QZU6TJQXEN' ) do |r|
41
+
42
+ nickname = r.customer_content_lookup_response.customers.customer.nickname
43
+
44
+ assert_equal( 'jimwood43', nickname )
45
+
46
+ end
47
+ end
48
+
49
+ end
@@ -0,0 +1,44 @@
1
+ # $Id: tc_help.rb,v 1.3 2010/02/20 17:15:17 ianmacd Exp $
2
+ #
3
+
4
+ require 'test/unit'
5
+ require './setup'
6
+
7
+ class TestHelp < AWSTest
8
+
9
+ def test_help
10
+
11
+ h = Help.new( 'ResponseGroup', 'Large' )
12
+ rg = ResponseGroup.new( 'Help' )
13
+ h.response_group = rg
14
+ response = @req.search( h )
15
+
16
+ # Get a list of valid operations for the Large response group.
17
+ #
18
+ results = response.help_response[0].information.response_group_information.
19
+ valid_operations.operation
20
+
21
+ # Ensure we got some actual results back.
22
+ #
23
+ assert( results.size > 0 )
24
+
25
+ end
26
+
27
+ def test_help_class_method
28
+
29
+ response = Amazon::AWS.help( 'ResponseGroup', 'Large' )
30
+
31
+ # With no response group, Large will be tried. The resulting exception
32
+ # will be rescued and the text of the message returned by AWS will be used
33
+ # to determine a response group that will work.
34
+ #
35
+ results = response.help_response[0].information.response_group_information.
36
+ valid_operations.operation
37
+
38
+ # Ensure we got some actual results back.
39
+ #
40
+ assert( results.size > 0 )
41
+
42
+ end
43
+
44
+ end