treyconnell-ruby-aaws 0.8.1

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.
Files changed (59) hide show
  1. data/COPYING +340 -0
  2. data/INSTALL +260 -0
  3. data/NEWS +808 -0
  4. data/README +679 -0
  5. data/README.rdoc +140 -0
  6. data/Rakefile +17 -0
  7. data/VERSION.yml +5 -0
  8. data/example/batch_operation +28 -0
  9. data/example/browse_node_lookup1 +46 -0
  10. data/example/customer_content_lookup1 +27 -0
  11. data/example/customer_content_search1 +21 -0
  12. data/example/example1 +78 -0
  13. data/example/help1 +24 -0
  14. data/example/item_lookup1 +56 -0
  15. data/example/item_lookup2 +56 -0
  16. data/example/item_search1 +30 -0
  17. data/example/item_search2 +37 -0
  18. data/example/item_search3 +23 -0
  19. data/example/list_lookup1 +29 -0
  20. data/example/list_search1 +30 -0
  21. data/example/multiple_operation1 +69 -0
  22. data/example/seller_listing_lookup1 +30 -0
  23. data/example/seller_listing_search1 +29 -0
  24. data/example/seller_lookup1 +45 -0
  25. data/example/shopping_cart1 +42 -0
  26. data/example/similarity_lookup1 +48 -0
  27. data/example/tag_lookup1 +34 -0
  28. data/example/transaction_lookup1 +25 -0
  29. data/example/vehicle_search +22 -0
  30. data/lib/amazon/aws/cache.rb +141 -0
  31. data/lib/amazon/aws/search.rb +464 -0
  32. data/lib/amazon/aws/shoppingcart.rb +537 -0
  33. data/lib/amazon/aws.rb +1493 -0
  34. data/lib/amazon/locale.rb +102 -0
  35. data/lib/amazon.rb +165 -0
  36. data/pkg/treyconnell-ruby-aaws-0.8.1.gem +0 -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 +146 -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
data/lib/amazon.rb ADDED
@@ -0,0 +1,165 @@
1
+ # $Id: amazon.rb,v 1.33 2010/03/19 17:20:46 ianmacd Exp $
2
+ #
3
+
4
+ module Amazon
5
+
6
+ # A top-level exception container class.
7
+ #
8
+ class AmazonError < StandardError; end
9
+
10
+ NAME = 'Ruby/Amazon'
11
+
12
+ @@config = {}
13
+
14
+ # We're going to have to use String#size if String#bytesize isn't available.
15
+ # This is for Ruby pre-1.8.7.
16
+ #
17
+ unless String.instance_methods.include? 'bytesize'
18
+ String.module_eval( 'alias :bytesize :size' )
19
+ end
20
+
21
+ # Prints debugging messages and works like printf, except that it prints
22
+ # only when Ruby is run with the -d switch.
23
+ #
24
+ def Amazon.dprintf(format='', *args)
25
+ $stderr.printf( format + "\n", *args ) if $DEBUG
26
+ end
27
+
28
+ # Encode a string, such that it is suitable for HTTP transmission.
29
+ #
30
+ def Amazon.url_encode(string)
31
+
32
+ # Shamelessly plagiarised from Wakou Aoyama's cgi.rb, but then altered
33
+ # slightly to please AWS.
34
+ #
35
+ string.gsub( /([^a-zA-Z0-9_.~-]+)/ ) do
36
+ '%' + $1.unpack( 'H2' * $1.bytesize ).join( '%' ).upcase
37
+ end
38
+ end
39
+
40
+ # Convert a string from CamelCase to ruby_case.
41
+ #
42
+ def Amazon.uncamelise(string)
43
+ # Avoid modifying by reference.
44
+ #
45
+ string = string.dup
46
+
47
+ # Don't mess with string if all caps.
48
+ #
49
+ if string =~ /[a-z]/
50
+ string.gsub!( /(.+?)(([A-Z][a-z]|[A-Z]+$))/, "\\1_\\2" )
51
+ end
52
+
53
+ # Convert to lower case.
54
+ #
55
+ string.downcase
56
+ end
57
+
58
+
59
+ # A Class for dealing with configuration files, such as
60
+ # <tt>/etc/amazonrc</tt> and <tt>~/.amazonrc</tt>.
61
+ #
62
+ class Config < Hash
63
+
64
+ require 'stringio'
65
+
66
+ # Exception class for configuration file errors.
67
+ #
68
+ class ConfigError < AmazonError; end
69
+
70
+ # A configuration may be passed in as a string. Otherwise, the files
71
+ # <tt>/etc/amazonrc</tt> and <tt>~/.amazonrc</tt> are read if they exist
72
+ # and are readable.
73
+ #
74
+ def initialize(config_str=nil)
75
+ locale = nil
76
+
77
+ if config_str
78
+
79
+ # We have been passed a config file as a string.
80
+ #
81
+ config_files = [ config_str ]
82
+ config_class = StringIO
83
+
84
+ else
85
+
86
+ # Perform the usual search for the system and user config files.
87
+ #
88
+ config_files = [ File.join( '', 'etc', 'amazonrc' ) ]
89
+
90
+ # Figure out where home is. The locations after HOME are for Windows.
91
+ # [ruby-core:12347]
92
+ #
93
+ hp = nil
94
+ if ENV.key?( 'HOMEDRIVE' ) && ENV.key?( 'HOMEPATH' )
95
+ hp = ENV['HOMEDRIVE'] + ENV['HOMEPATH']
96
+ end
97
+ home = ENV['AMAZONRCDIR'] || ENV['HOME'] || hp || ENV['USERPROFILE']
98
+
99
+ user_rcfile = File.join(Rails.root, 'config/.amazonrc')
100
+
101
+ if home
102
+ config_files << File.expand_path( File.join( home, user_rcfile ) )
103
+ end
104
+
105
+ config_class = File
106
+ end
107
+
108
+ config_files.each do |cf|
109
+
110
+ if config_class == StringIO
111
+ readable = true
112
+ else
113
+ # We must determine whether the file is readable.
114
+ #
115
+ readable = File.exists?( cf ) && File.readable?( cf )
116
+ end
117
+
118
+ if readable
119
+
120
+ Amazon.dprintf( 'Opening %s ...', cf ) if config_class == File
121
+
122
+ config_class.open( cf ) { |f| lines = f.readlines }.each do |line|
123
+ line.chomp!
124
+
125
+ # Skip comments and blank lines.
126
+ #
127
+ next if line =~ /^(#|$)/
128
+
129
+ Amazon.dprintf( 'Read: %s', line )
130
+
131
+ # Determine whether we're entering the subsection of a new locale.
132
+ #
133
+ if match = line.match( /^\[(\w+)\]$/ )
134
+ locale = match[1]
135
+ Amazon.dprintf( "Config locale is now '%s'.", locale )
136
+ next
137
+ end
138
+
139
+ # Store these, because we'll probably find a use for these later.
140
+ #
141
+ begin
142
+ match = line.match( /^\s*(\S+)\s*=\s*(['"]?)([^'"]+)(['"]?)/ )
143
+ key, begin_quote, val, end_quote = match[1, 4]
144
+ raise ConfigError if begin_quote != end_quote
145
+
146
+ rescue NoMethodError, ConfigError
147
+ raise ConfigError, "bad config line: #{line}"
148
+ end
149
+
150
+ if locale && locale != 'global'
151
+ self[locale] ||= {}
152
+ self[locale][key] = val
153
+ else
154
+ self[key] = val
155
+ end
156
+
157
+ end
158
+ end
159
+
160
+ end
161
+
162
+ end
163
+ end
164
+
165
+ end
Binary file
data/test/setup.rb ADDED
@@ -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
data/test/tc_amazon.rb ADDED
@@ -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
data/test/tc_aws.rb ADDED
@@ -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