kernow-ruby-aaws 0.5.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,102 @@
1
+ # $Id: locale.rb,v 1.3 2008/06/07 17:28:51 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[ 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 be 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,139 @@
1
+ # $Id: amazon.rb,v 1.25 2008/10/03 09:35:37 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
+ @@config = {}
12
+
13
+ # Prints debugging messages and works like printf, except that it prints
14
+ # only when Ruby is run with the -d switch.
15
+ #
16
+ def Amazon.dprintf(format='', *args)
17
+ $stderr.printf( format + "\n", *args ) if $DEBUG
18
+ end
19
+
20
+
21
+ # Encode a string, such that it is suitable for HTTP transmission.
22
+ #
23
+ def Amazon.url_encode(string)
24
+
25
+ # Shamelessly plagiarised from Wakou Aoyama's cgi.rb.
26
+ #
27
+ string.gsub( /([^ a-zA-Z0-9_.-]+)/n ) do
28
+ '%' + $1.unpack( 'H2' * $1.size ).join( '%' ).upcase
29
+ end.tr( ' ', '+' )
30
+ end
31
+
32
+
33
+ # Convert a string from CamelCase to ruby_case.
34
+ #
35
+ def Amazon.uncamelise(str)
36
+ # Avoid modifying by reference.
37
+ #
38
+ str = str.dup
39
+
40
+ # Don't mess with string if all caps.
41
+ #
42
+ str.gsub!( /(.+?)(([A-Z][a-z]|[A-Z]+$))/, "\\1_\\2" ) if str =~ /[a-z]/
43
+
44
+ # Convert to lower case.
45
+ #
46
+ str.downcase
47
+ end
48
+
49
+
50
+ # A Class for dealing with configuration files, such as
51
+ # <tt>/etc/amazonrc</tt> and <tt>~/.amazonrc</tt>.
52
+ #
53
+ class Config < Hash
54
+
55
+ require 'stringio'
56
+
57
+ # Exception class for configuration file errors.
58
+ #
59
+ class ConfigError < AmazonError; end
60
+
61
+ # A configuration may be passed in as a string. Otherwise, the files
62
+ # <tt>/etc/amazonrc</tt> and <tt>~/.amazonrc</tt> are read if they exist
63
+ # and are readable.
64
+ #
65
+ def initialize(config_str=nil)
66
+
67
+ if config_str
68
+
69
+ # We have been passed a config file as a string.
70
+ #
71
+ config_files = [ config_str ]
72
+ config_class = StringIO
73
+
74
+ else
75
+
76
+ # Perform the usual search for the system and user config files.
77
+ #
78
+ config_files = [ File.join( '', 'etc', 'amazonrc' ) ]
79
+
80
+ # Figure out where home is. The locations after HOME are for Windows.
81
+ # [ruby-core:12347]
82
+ #
83
+ home = ENV['AMAZONRCDIR'] ||
84
+ ENV['HOME'] || ENV['HOMEDRIVE'] + ENV['HOMEPATH'] ||
85
+ ENV['USERPROFILE']
86
+ user_rcfile = ENV['AMAZONRCFILE'] || '.amazonrc'
87
+
88
+ if home
89
+ config_files << File.expand_path( File.join( home, user_rcfile ) )
90
+ end
91
+
92
+ config_class = File
93
+ end
94
+
95
+ config_files.each do |cf|
96
+
97
+ if config_class == StringIO
98
+ readable = true
99
+ else
100
+ # We must determine whether the file is readable.
101
+ #
102
+ readable = File.exists?( cf ) && File.readable?( cf )
103
+ end
104
+
105
+ if readable
106
+
107
+ Amazon.dprintf( 'Opening %s ...', cf ) if config_class == File
108
+
109
+ config_class.open( cf ) { |f| lines = f.readlines }.each do |line|
110
+ line.chomp!
111
+
112
+ # Skip comments and blank lines.
113
+ #
114
+ next if line =~ /^(#|$)/
115
+
116
+ Amazon.dprintf( 'Read: %s', line )
117
+
118
+ # Store these, because we'll probably find a use for these later.
119
+ #
120
+ begin
121
+ match = line.match( /^\s*(\S+)\s*=\s*(['"]?)([^'"]+)(['"]?)/ )
122
+ key, begin_quote, val, end_quote = match[1, 4]
123
+ raise ConfigError if begin_quote != end_quote
124
+
125
+ rescue NoMethodError, ConfigError
126
+ raise ConfigError, "bad config line: #{line}"
127
+ end
128
+
129
+ self[key] = val
130
+
131
+ end
132
+ end
133
+
134
+ end
135
+
136
+ end
137
+ end
138
+
139
+ end
data/ruby-aws.gemspec ADDED
@@ -0,0 +1,57 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "ruby-aaws"
3
+ s.version = "0.5.4"
4
+ s.date = "2008-12-04"
5
+ s.summary = "Interface for Amazon's Associates Web Services (AWS)."
6
+ s.email = "jamie@kernowsoul.com"
7
+ s.homepage = "http://github.com/kernowsoul/ruby-aws"
8
+ s.description = "Ruby/AWS is a Ruby language library that makes it relatively easy for the programmer to retrieve information from the popular Amazon Web site via Amazon's Associates Web Services (AWS)."
9
+ s.has_rdoc = true
10
+ s.authors = ["Ian Macdonald", "Jamie Dyer"]
11
+ s.files = ["setup.rb",
12
+ "ruby-aws.spec",
13
+ "ruby-aws.gemspec",
14
+ "README.rdoc",
15
+ "README",
16
+ "Rakefile",
17
+ "NEWS",
18
+ "lib/amazon.rb",
19
+ "lib/amazon/locale.rb",
20
+ "lib/amazon/aws.rb",
21
+ "lib/amazon/aws/shoppingcart.rb",
22
+ "lib/amazon/aws/search.rb",
23
+ "lib/amazon/aws/cache.rb",
24
+ "INSTALL",
25
+ "example/transaction_lookup1",
26
+ "example/tag_lookup1",
27
+ "example/similarity_lookup1",
28
+ "example/shopping_cart1",
29
+ "example/seller_lookup1",
30
+ "example/seller_listing_search1",
31
+ "example/seller_listing_lookup1",
32
+ "example/multiple_operation1",
33
+ "example/list_search1",
34
+ "example/list_lookup1",
35
+ "example/item_search3",
36
+ "example/item_search2",
37
+ "example/item_search1",
38
+ "example/item_lookup2",
39
+ "example/item_lookup1",
40
+ "example/help1",
41
+ "example/example1",
42
+ "example/customer_content_search1",
43
+ "example/customer_content_lookup1",
44
+ "example/browse_node_lookup1",
45
+ "COPYING"]
46
+ s.test_files = ["test/ts_aws.rb",
47
+ "test/tc_shopping_cart.rb",
48
+ "test/tc_serialisation.rb",
49
+ "test/tc_operation_request.rb",
50
+ "test/tc_multiple_operation.rb",
51
+ "test/tc_item_search.rb",
52
+ "test/tc_aws.rb",
53
+ "test/tc_amazon.rb",
54
+ "test/setup.rb"]
55
+ s.rdoc_options = ["--main", "README.txt"]
56
+ s.extra_rdoc_files = ["INSTALL", "README", "NEWS"]
57
+ end
data/ruby-aws.spec ADDED
@@ -0,0 +1,177 @@
1
+ # $Id: ruby-aws.spec,v 1.16 2008/10/03 12:00:57 ianmacd Exp $
2
+
3
+ %{!?ruby_sitelib: %define ruby_sitelib %(ruby -rrbconfig -e "puts Config::CONFIG['sitelibdir']")}
4
+ %{!?ruby_rdoc_sitepath: %define ruby_rdoc_sitepath %(ruby -rrdoc/ri/ri_paths -e "puts RI::Paths::PATH[1]")}
5
+ %define rubyabi 1.8
6
+
7
+ Name: ruby-aws
8
+ Version: 0.4.4
9
+ Release: 1%{?dist}
10
+ Summary: Ruby library interface to Amazon Associates Web Services
11
+ Group: Development/Languages
12
+
13
+ License: GPL+
14
+ URL: http://www.caliban.org/ruby/ruby-aws/
15
+ Source0: http://www.caliban.org/files/ruby/%{name}-%{version}.tar.gz
16
+ BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
17
+
18
+ BuildArch: noarch
19
+ BuildRequires: ruby, ruby-rdoc
20
+ #BuildRequires: ruby-devel
21
+ Requires: ruby >= 1.8.6
22
+ Provides: ruby(aws) = %{version}-%{release}
23
+ # Obsoletes but not Provides, because Ruby/Amazon API is obsolete.
24
+ Obsoletes: ruby-amazon
25
+
26
+ %description
27
+ Ruby/AWS is a Ruby language library that allows the programmer to retrieve
28
+ information from Amazon via the Associate Web Service. In addition to the
29
+ original amazon.com site, amazon.co.uk, amazon.de, amazon.fr, amazon.ca and
30
+ amazon.co.jp are also supported.
31
+
32
+ In addition to wrapping the AWS API, Ruby/AWS provides geolocation of clients,
33
+ transparent fetching of all hits for a given search (not just the first 10)
34
+ and a host of other features.
35
+
36
+ Ruby/AWS supersedes Ruby/Amazon.
37
+
38
+
39
+ %package doc
40
+ Summary: Documentation for %{name}
41
+ Group: Documentation
42
+
43
+ %description doc
44
+ This package contains documentation for %{name}.
45
+
46
+
47
+ %prep
48
+ %setup -q
49
+
50
+ %build
51
+ ruby setup.rb config \
52
+ --prefix=%{_prefix} \
53
+ --site-ruby=%{ruby_sitelib}
54
+ ruby setup.rb setup
55
+
56
+ %install
57
+ %{__rm} -rf $RPM_BUILD_ROOT
58
+
59
+ ruby setup.rb install \
60
+ --prefix=$RPM_BUILD_ROOT
61
+
62
+ %{__chmod} ugo-x example/*
63
+
64
+ rdoc -r -o $RPM_BUILD_ROOT%{ruby_rdoc_sitepath} -x CVS lib
65
+ %{__rm} $RPM_BUILD_ROOT%{ruby_rdoc_sitepath}/created.rid
66
+
67
+ %clean
68
+ %{__rm} -rf $RPM_BUILD_ROOT
69
+
70
+ %files
71
+ %defattr(-,root,root,-)
72
+ %doc COPYING
73
+ %doc NEWS
74
+ %doc README*
75
+
76
+ %{ruby_sitelib}/amazon.rb
77
+ %{ruby_sitelib}/amazon/
78
+
79
+ %files doc
80
+ %defattr(-,root,root,-)
81
+ %doc example/
82
+ %doc test/
83
+ %doc %{ruby_rdoc_sitepath}/
84
+
85
+ %changelog
86
+ * Fri Oct 3 2008 Ian Macdonald <ian@caliban.org> 0.4.4-1
87
+ - 0.4.4
88
+ - $AMAZONRCFILE may now be defined with an alternative to .amazonrc.
89
+ - $AMAZONRCDIR and typical Windows paths were not being used as alternatives
90
+ to $HOME.
91
+
92
+ * Wed Sep 11 2008 Ian Macdonald <ian@caliban.org> 0.4.2-1
93
+ - 0.4.2
94
+ - AWS API revision 2008-08-19 now used for all calls.
95
+ - Exception class Amazon::Config::ConfigError was not defined.
96
+ - Config file lines may now contain leading whitespace.
97
+ - ALL_PAGES now takes into account the type of search operation being
98
+ performed.
99
+
100
+ * Mon Aug 18 2008 Ian Macdonald <ian@caliban.org> 0.4.1-1
101
+ - 0.4.1
102
+ - Exception class Amazon::AWS::HTTPError was not defined.
103
+ - Scan extra locations besides $HOME for .amazonrc (for Windows users).
104
+
105
+ * Sat Jul 5 2008 Ian Macdonald <ian@caliban.org> 0.4.0-1
106
+ - 0.4.0
107
+ - AWS API revision 2008-04-07 now used for all calls.
108
+ - Remote shopping-cart operation, CartGet, is now implemented.
109
+ - A bug in Cart#modify was fixed.
110
+
111
+ * Mon Jun 23 2008 Ian Macdonald <ian@caliban.org> 0.3.3-1
112
+ - 0.3.3
113
+ - Minor code clean-up.
114
+ - Rakefile added for packaging Ruby/AWS as a RubyGems gem.
115
+
116
+ * Tue Jun 17 2008 Ian Macdonald <ian@caliban.org> 0.3.2-1
117
+ - 0.3.2
118
+ - Solved Marshal and YAML deserialisation issues, which occur because objects
119
+ dumped from dynamically defined classes can't be reinstantiated at
120
+ load-time, when those classes no longer exist.
121
+
122
+ * Tue Jun 10 2008 Ian Macdonald <ian@caliban.org> 0.3.1-1
123
+ - 0.3.1
124
+ - The 'Save For Later' area of remote shopping-carts is now implemented. See
125
+ Cart#cart_modify and the @saved_for_later_items attribute of Cart objects.
126
+ - New methods, Cart#active? and Cart#saved_for_later?
127
+ - Cart#include? now also takes the Save For Later area of the cart into
128
+ account.
129
+ - Numerous bug fixes.
130
+
131
+ * Mon May 19 2008 Ian Macdonald <ian@caliban.org> 0.3.0-1
132
+ - 0.3.0
133
+ - Remote shopping-carts are now implemented. Newly supported operations are
134
+ CartCreate, CartAdd, CartModify and CartClear.
135
+ - New iterator method, AWSObject#each, yields each |property, value| of the
136
+ AWSObject.
137
+ - AWS API revision 2008-04-07 now used for all calls.
138
+ - Error-checking improved.
139
+ - Minor bug fixes.
140
+ - RDoc documentation for ri is now part of the doc subpackage.
141
+ - test/ added to doc subpackage.
142
+ - BuildRequires ruby-rdoc.
143
+
144
+ * Mon Apr 28 2008 Ian Macdonald <ian@caliban.org> 0.2.0-1
145
+ - 0.2.0
146
+ - Removed BuildRequires and Requires for ruby(abi).
147
+ - New operations supported: CustomerContentLookup, CustomerContentSearch,
148
+ Help, ListLookup, SellerListingSearch, SellerLookup, SimilarityLookup,
149
+ TagLookup, TransactionLookup.
150
+ - Symbols can now be used instead of Strings as parameters when instantiating
151
+ operation and response group objects.
152
+ - Image objects can now retrieve their images and optionally overlay them with
153
+ percentage discount icons.
154
+ - Compatibility fixes for Ruby 1.9.
155
+ - Dozens of other fixes and minor improvements.
156
+
157
+ * Sat Apr 11 2008 Ian Macdonald <ian@caliban.org> 0.1.0-1
158
+ - 0.1.0
159
+ - Completely rewritten XML parser.
160
+ - Multiple operations are now implemented.
161
+ - Numerous fixes and improvements.
162
+ - Large scale code clean-up.
163
+ - Much more documentation added.
164
+ - Use Fedora spec file by Mamoru Tasaka <mtasaka@ioa.s.u-tokyo.ac.jp>.
165
+
166
+ * Fri Mar 28 2008 Ian Macdonald <ian@caliban.org> 0.0.2-1
167
+ - 0.0.2
168
+ - Allow multiple response groups to be passed to ResponseGroup.new.
169
+ - Minor bug fixes.
170
+
171
+ * Mon Mar 24 2008 Ian Macdonald <ian@caliban.org> 0.0.1-2
172
+ - 0.0.1
173
+ - First public (alpha) release.
174
+
175
+ * Sun Mar 23 2008 Ian Macdonald <ian@caliban.org> 0.0.1-1
176
+ - Private test release only.
177
+