ruby-paa 0.0.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.
- data/example/batch_operation +28 -0
- data/example/browse_node_lookup1 +46 -0
- data/example/customer_content_lookup1 +27 -0
- data/example/customer_content_search1 +21 -0
- data/example/example1 +78 -0
- data/example/help1 +24 -0
- data/example/item_lookup1 +54 -0
- data/example/item_lookup2 +56 -0
- data/example/item_search1 +30 -0
- data/example/item_search2 +37 -0
- data/example/item_search3 +23 -0
- data/example/list_lookup1 +29 -0
- data/example/list_search1 +31 -0
- data/example/multiple_operation1 +69 -0
- data/example/seller_listing_lookup1 +30 -0
- data/example/seller_listing_search1 +29 -0
- data/example/seller_lookup1 +45 -0
- data/example/shopping_cart1 +42 -0
- data/example/similarity_lookup1 +48 -0
- data/example/tag_lookup1 +34 -0
- data/example/transaction_lookup1 +25 -0
- data/example/vehicle_search +22 -0
- data/lib/ruby-paa.rb +165 -0
- data/lib/ruby-paa/aws.rb +1489 -0
- data/lib/ruby-paa/aws/cache.rb +141 -0
- data/lib/ruby-paa/aws/search.rb +463 -0
- data/lib/ruby-paa/aws/shoppingcart.rb +536 -0
- data/lib/ruby-paa/locale.rb +102 -0
- data/test/setup.rb +56 -0
- data/test/tc_amazon.rb +20 -0
- data/test/tc_aws.rb +160 -0
- data/test/tc_browse_node_lookup.rb +49 -0
- data/test/tc_customer_content_lookup.rb +49 -0
- data/test/tc_help.rb +44 -0
- data/test/tc_item_lookup.rb +47 -0
- data/test/tc_item_search.rb +105 -0
- data/test/tc_list_lookup.rb +60 -0
- data/test/tc_list_search.rb +44 -0
- data/test/tc_multiple_operation.rb +375 -0
- data/test/tc_operation_request.rb +64 -0
- data/test/tc_seller_listing_lookup.rb +47 -0
- data/test/tc_seller_listing_search.rb +55 -0
- data/test/tc_seller_lookup.rb +44 -0
- data/test/tc_serialisation.rb +107 -0
- data/test/tc_shopping_cart.rb +214 -0
- data/test/tc_similarity_lookup.rb +48 -0
- data/test/tc_tag_lookup.rb +24 -0
- data/test/tc_transaction_lookup.rb +24 -0
- data/test/tc_vehicle_operations.rb +118 -0
- data/test/ts_aws.rb +24 -0
- metadata +132 -0
@@ -0,0 +1,31 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
#
|
3
|
+
# $Id: list_search1,v 1.4 2010/02/20 16:49:13 ianmacd Exp $
|
4
|
+
require 'rubygems'
|
5
|
+
require 'openssl'
|
6
|
+
require 'ruby-paa/aws/search'
|
7
|
+
|
8
|
+
include Amazon::AWS
|
9
|
+
include Amazon::AWS::Search
|
10
|
+
|
11
|
+
person = 'Peter Duff'
|
12
|
+
|
13
|
+
ls = ListSearch.new( 'WishList', { 'Name' => person } )
|
14
|
+
ls.response_group = ResponseGroup.new( 'ListInfo' )
|
15
|
+
|
16
|
+
req = Request.new
|
17
|
+
req.locale = 'us'
|
18
|
+
|
19
|
+
resp = req.search( ls )
|
20
|
+
lists = resp.list_search_response[0].lists[0].list
|
21
|
+
|
22
|
+
puts '%s returns the following lists:' % [ person ]
|
23
|
+
|
24
|
+
lists.each do |list|
|
25
|
+
puts ' %s' % [ list.customer_name ]
|
26
|
+
puts ' List created: %s' % [ list.date_created ]
|
27
|
+
puts ' List ID: %s' % [ list.list_id ]
|
28
|
+
puts ' URL: %s' % [ list.list_url ]
|
29
|
+
puts ' %s items on list.' % [ list.total_items ]
|
30
|
+
puts
|
31
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
#
|
3
|
+
# $Id: multiple_operation1,v 1.3 2010/02/20 16:49:13 ianmacd Exp $
|
4
|
+
|
5
|
+
require 'amazon/aws/search'
|
6
|
+
|
7
|
+
include Amazon::AWS
|
8
|
+
include Amazon::AWS::Search
|
9
|
+
|
10
|
+
# Example of a batch operation, using the ASIN as the shared ID.
|
11
|
+
#
|
12
|
+
# The MerchantId restriction is to ensure that we retrieve only items that
|
13
|
+
# are for sale by Amazon. This is important when we later want to retrieve the
|
14
|
+
# availability status.
|
15
|
+
#
|
16
|
+
il = ItemLookup.new( 'ASIN', { 'ItemId' => 'B000AE4QEC',
|
17
|
+
'MerchantId' => 'Amazon' } )
|
18
|
+
il2 = ItemLookup.new( 'ASIN', { 'ItemId' => 'B000051WBE',
|
19
|
+
'MerchantId' => 'Amazon' } )
|
20
|
+
il.batch( il2 )
|
21
|
+
is = ItemSearch.new( 'Books', { 'Title' => 'Ruby' } )
|
22
|
+
|
23
|
+
mo = MultipleOperation.new( is, il )
|
24
|
+
|
25
|
+
# You can have multiple response groups and apply them to all encapsulated
|
26
|
+
# operations.
|
27
|
+
#
|
28
|
+
mo.response_group = ResponseGroup.new( 'Medium', 'Offers', 'Reviews' )
|
29
|
+
|
30
|
+
req = Request.new
|
31
|
+
req.locale = 'uk'
|
32
|
+
resp = req.search( mo )
|
33
|
+
|
34
|
+
# Items returned by the ItemSearch.
|
35
|
+
#
|
36
|
+
is_item_sets = resp.multi_operation_response.item_search_response[0].items
|
37
|
+
|
38
|
+
# Items returned by the ItemLookup.
|
39
|
+
#
|
40
|
+
il_item_sets = resp.multi_operation_response.item_lookup_response[0].items
|
41
|
+
|
42
|
+
item_sets = is_item_sets + il_item_sets
|
43
|
+
|
44
|
+
item_sets.each do |item_set|
|
45
|
+
item_set.item.each do |item|
|
46
|
+
attribs = item.item_attributes[0]
|
47
|
+
puts attribs.label
|
48
|
+
if attribs.list_price
|
49
|
+
puts attribs.title, attribs.list_price[0].formatted_price
|
50
|
+
end
|
51
|
+
|
52
|
+
# Availability has become a cumbersome thing to retrieve in AWSv4.
|
53
|
+
#
|
54
|
+
puts 'Availability: %s' %
|
55
|
+
[ item.offers[0].offer[0].offer_listing[0].availability ]
|
56
|
+
|
57
|
+
if item.customer_reviews
|
58
|
+
puts 'Average rating: %s' % [ item.customer_reviews[0].average_rating ]
|
59
|
+
puts 'Reviewed by %s customers.' %
|
60
|
+
[ item.customer_reviews[0].total_reviews ]
|
61
|
+
puts 'Customers said:'
|
62
|
+
item.customer_reviews[0].review.each do |review|
|
63
|
+
puts ' %s (%s votes)' % [ review.summary, review.total_votes ]
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
puts
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
#
|
3
|
+
# $Id: seller_listing_lookup1,v 1.2 2010/02/20 16:49:13 ianmacd Exp $
|
4
|
+
|
5
|
+
require 'amazon/aws/search'
|
6
|
+
|
7
|
+
include Amazon::AWS
|
8
|
+
include Amazon::AWS::Search
|
9
|
+
|
10
|
+
seller_id = 'AP8U6Y3PYQ9VO'
|
11
|
+
artist = 'Killing Joke'
|
12
|
+
sll = SellerListingLookup.new( seller_id, 'ASIN',
|
13
|
+
{ 'Id' => 'B0009RRRC8' } )
|
14
|
+
sll.response_group = ResponseGroup.new( 'SellerListing' )
|
15
|
+
|
16
|
+
req = Request.new
|
17
|
+
req.locale = 'uk'
|
18
|
+
|
19
|
+
resp = req.search( sll )
|
20
|
+
|
21
|
+
# Yawn. This is verbose.
|
22
|
+
#
|
23
|
+
|
24
|
+
seller_id = resp.seller_listing_lookup_response[0].seller_listings[0].
|
25
|
+
request[0].seller_listing_lookup_request[0].seller_id
|
26
|
+
item = resp.seller_listing_lookup_response[0].seller_listings[0].
|
27
|
+
seller_listing[0]
|
28
|
+
|
29
|
+
puts 'Seller %s is selling the following item by %s:' % [ seller_id, artist ]
|
30
|
+
puts item
|
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
#
|
3
|
+
# $Id: seller_listing_search1,v 1.5 2010/02/20 16:49:13 ianmacd Exp $
|
4
|
+
|
5
|
+
require 'amazon/aws/search'
|
6
|
+
|
7
|
+
include Amazon::AWS
|
8
|
+
include Amazon::AWS::Search
|
9
|
+
|
10
|
+
seller_id = 'AP8U6Y3PYQ9VO'
|
11
|
+
artist = 'Killing Joke'
|
12
|
+
|
13
|
+
sls = SellerListingSearch.new( seller_id, { 'Keywords' => artist } )
|
14
|
+
sls.response_group = ResponseGroup.new( 'SellerListing' )
|
15
|
+
|
16
|
+
req = Request.new
|
17
|
+
req.locale = 'uk'
|
18
|
+
|
19
|
+
resp = req.search( sls )
|
20
|
+
|
21
|
+
# Yawn. This is verbose.
|
22
|
+
#
|
23
|
+
seller_id = resp.seller_listing_search_response[0].seller_listings[0].
|
24
|
+
request[0].seller_listing_search_request[0].seller_id
|
25
|
+
items = resp.seller_listing_search_response[0].seller_listings[0].
|
26
|
+
seller_listing
|
27
|
+
|
28
|
+
puts 'Seller %s is selling the following items by %s:' % [ seller_id, artist ]
|
29
|
+
items.each { |item| puts item, '-' * 80 }
|
@@ -0,0 +1,45 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
#
|
3
|
+
# $Id: seller_lookup1,v 1.2 2010/02/20 16:49:13 ianmacd Exp $
|
4
|
+
|
5
|
+
require 'amazon/aws/search'
|
6
|
+
|
7
|
+
include Amazon::AWS
|
8
|
+
include Amazon::AWS::Search
|
9
|
+
|
10
|
+
def display_properties(root, indent=0)
|
11
|
+
if root[0].respond_to? :properties
|
12
|
+
printf( 'Property %s =', root[0] )
|
13
|
+
root[0].properties.each do |pr|
|
14
|
+
display_properties( pr, indent + 2 )
|
15
|
+
end
|
16
|
+
else
|
17
|
+
printf( "Property %s = %s.\n", root, root.to_h[root] )
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
sl = SellerLookup.new( 'A3QFR0K2KCB7EG' )
|
22
|
+
sl.response_group = ResponseGroup.new( 'Seller' )
|
23
|
+
|
24
|
+
req = Request.new
|
25
|
+
req.locale = 'us'
|
26
|
+
|
27
|
+
resp = req.search( sl ).seller_lookup_response
|
28
|
+
|
29
|
+
seller = resp.sellers.seller
|
30
|
+
|
31
|
+
seller.properties.each do |pr|
|
32
|
+
if seller[0][pr][0].properties.empty?
|
33
|
+
printf( "%s = %s.\n", pr, seller[0][pr] )
|
34
|
+
else
|
35
|
+
seller[0][pr][0].properties.each do |nest1|
|
36
|
+
if seller[0][pr][0][nest1][0].properties.empty?
|
37
|
+
printf( "%s = %s.\n", nest1, seller[0][pr][0][nest1][0] )
|
38
|
+
else
|
39
|
+
seller[0][pr][0][nest1][0].properties.each do |nest2|
|
40
|
+
printf( "%s = %s.\n", nest2, seller[0][pr][0][nest1][0][nest2] )
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
#
|
3
|
+
# $Id: shopping_cart1,v 1.3 2010/02/20 16:49:13 ianmacd Exp $
|
4
|
+
|
5
|
+
require 'amazon/aws/search'
|
6
|
+
require 'amazon/aws/shoppingcart'
|
7
|
+
|
8
|
+
include Amazon::AWS
|
9
|
+
#include Amazon::AWS::Search
|
10
|
+
include Amazon::AWS::ShoppingCart
|
11
|
+
|
12
|
+
cart = Cart.new
|
13
|
+
cart.locale = 'uk'
|
14
|
+
|
15
|
+
cart.cart_create( :ASIN, 'B00151HZA6', 3, false,
|
16
|
+
{ 'B000WC4AH0' => 2 ,
|
17
|
+
'B00004U9MS' => 8 } )
|
18
|
+
|
19
|
+
puts cart.cart_id
|
20
|
+
puts cart.hmac
|
21
|
+
puts cart.purchase_url
|
22
|
+
puts
|
23
|
+
|
24
|
+
cart.cart_add( :ASIN, 'B0014C2BL4', 1,
|
25
|
+
{ 'B0006L16N8' => 5 },
|
26
|
+
{ 'B000QRI5RM' => 4 } )
|
27
|
+
cart.cart_add( :ASIN, 'B0013F2M52', 3 )
|
28
|
+
cart.cart_add( :ASIN, 'B000HCPSR6', 5 )
|
29
|
+
cart.cart_modify( :ASIN, 'B00151HZA6', 2, true,
|
30
|
+
{ 'B0013F2M52' => 1 },
|
31
|
+
{ 'B000HCPSR6' => 3 } )
|
32
|
+
|
33
|
+
puts 'Cart contents:'
|
34
|
+
cart.each do |it|
|
35
|
+
puts "ASIN: %s, item Id: %-14s, quantity: %d" %
|
36
|
+
[ it.asin, it.cart_item_id, it.quantity ]
|
37
|
+
end
|
38
|
+
puts
|
39
|
+
|
40
|
+
puts cart.items
|
41
|
+
|
42
|
+
cart.cart_clear
|
@@ -0,0 +1,48 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
#
|
3
|
+
# $Id: similarity_lookup1,v 1.2 2010/02/20 16:49:14 ianmacd Exp $
|
4
|
+
|
5
|
+
require 'amazon/aws'
|
6
|
+
require 'amazon/aws/search'
|
7
|
+
|
8
|
+
include Amazon::AWS
|
9
|
+
include Amazon::AWS::Search
|
10
|
+
|
11
|
+
# Example of a batch operation, using the ASIN as the shared ID.
|
12
|
+
#
|
13
|
+
sl = SimilarityLookup.new( [ 'B000AE4QEC', 'B000051WBE' ] )
|
14
|
+
|
15
|
+
# You can have multiple response groups.
|
16
|
+
#
|
17
|
+
sl.response_group = ResponseGroup.new( 'Medium', 'Offers', 'Reviews' )
|
18
|
+
|
19
|
+
req = Request.new
|
20
|
+
req.locale = 'uk'
|
21
|
+
|
22
|
+
resp = req.search( sl )
|
23
|
+
item_sets = resp.similarity_lookup_response[0].items
|
24
|
+
|
25
|
+
item_sets.each do |item_set|
|
26
|
+
item_set.item.each do |item|
|
27
|
+
attribs = item.item_attributes[0]
|
28
|
+
puts attribs.label
|
29
|
+
if attribs.list_price
|
30
|
+
puts attribs.title, attribs.list_price[0].formatted_price
|
31
|
+
end
|
32
|
+
|
33
|
+
# Availability has become a cumbersome thing to retrieve in AWSv4.
|
34
|
+
#
|
35
|
+
puts 'Availability: %s' %
|
36
|
+
[ item.offers[0].offer[0].offer_listing[0].availability ]
|
37
|
+
puts 'Average rating: %s' % [ item.customer_reviews[0].average_rating ]
|
38
|
+
puts 'Reviewed by %s customers.' %
|
39
|
+
[ item.customer_reviews[0].total_reviews ]
|
40
|
+
|
41
|
+
puts 'Customers said:'
|
42
|
+
item.customer_reviews[0].review.each do |review|
|
43
|
+
puts ' %s (%s votes)' % [ review.summary, review.total_votes ]
|
44
|
+
end
|
45
|
+
|
46
|
+
puts
|
47
|
+
end
|
48
|
+
end
|
data/example/tag_lookup1
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
#
|
3
|
+
# $Id: tag_lookup1,v 1.2 2010/02/20 16:49:14 ianmacd Exp $
|
4
|
+
|
5
|
+
require 'amazon/aws'
|
6
|
+
require 'amazon/aws/search'
|
7
|
+
|
8
|
+
include Amazon::AWS
|
9
|
+
include Amazon::AWS::Search
|
10
|
+
|
11
|
+
tag_str = 'Awful'
|
12
|
+
tl = TagLookup.new( tag_str )
|
13
|
+
|
14
|
+
# You can have multiple response groups.
|
15
|
+
#
|
16
|
+
tl.response_group = ResponseGroup.new( 'Tags', 'TagsSummary' )
|
17
|
+
|
18
|
+
req = Request.new
|
19
|
+
req.locale = 'us'
|
20
|
+
|
21
|
+
resp = req.search( tl )
|
22
|
+
tag = resp.tag_lookup_response.tags.tag
|
23
|
+
|
24
|
+
printf( "Tag name '%s' has %d distinct items.\n", tag_str, tag.distinct_items )
|
25
|
+
printf( "Tag has %d distinct items.\n", tag.distinct_users )
|
26
|
+
printf( "Tag has %d total usages.\n", tag.total_usages )
|
27
|
+
printf( "Tagged for the first time in entity %s on %s\nby %s..\n",
|
28
|
+
tag.first_tagging.entity_id,
|
29
|
+
tag.first_tagging.time,
|
30
|
+
tag.first_tagging.user_id )
|
31
|
+
printf( "Tagged for the last time in entity %s on %s\nby %s..\n",
|
32
|
+
tag.last_tagging.entity_id,
|
33
|
+
tag.last_tagging.time,
|
34
|
+
tag.last_tagging.user_id )
|
@@ -0,0 +1,25 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
#
|
3
|
+
# $Id: transaction_lookup1,v 1.2 2010/02/20 16:49:14 ianmacd Exp $
|
4
|
+
|
5
|
+
require 'amazon/aws'
|
6
|
+
require 'amazon/aws/search'
|
7
|
+
|
8
|
+
include Amazon::AWS
|
9
|
+
include Amazon::AWS::Search
|
10
|
+
|
11
|
+
tl = TransactionLookup.new( '103-5663398-5028241' )
|
12
|
+
tl.response_group = ResponseGroup.new( 'TransactionDetails' )
|
13
|
+
|
14
|
+
req = Request.new
|
15
|
+
req.locale = 'us'
|
16
|
+
|
17
|
+
resp = req.search( tl )
|
18
|
+
trans = resp.transaction_lookup_response.transactions.transaction
|
19
|
+
|
20
|
+
printf( "Transaction date was %s.\n", trans.transaction_date )
|
21
|
+
printf( "It was in the amount of %s and the seller was %s.\n",
|
22
|
+
trans.totals.total.formatted_price, trans.seller_name )
|
23
|
+
printf( "The shipping charge was %s and the package was sent by %s.\n",
|
24
|
+
trans.totals.shipping_charge.formatted_price,
|
25
|
+
trans.shipments.shipment.delivery_method )
|
@@ -0,0 +1,22 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
#
|
3
|
+
# $Id: vehicle_search,v 1.2 2010/02/20 16:49:14 ianmacd Exp $
|
4
|
+
|
5
|
+
require 'amazon/aws/search'
|
6
|
+
|
7
|
+
include Amazon::AWS
|
8
|
+
include Amazon::AWS::Search
|
9
|
+
|
10
|
+
vs = VehicleSearch.new( { 'Year' => 2008 } )
|
11
|
+
vs.response_group = ResponseGroup.new( 'VehicleMakes' )
|
12
|
+
|
13
|
+
req = Request.new
|
14
|
+
req.locale = 'us'
|
15
|
+
|
16
|
+
resp = req.search( vs )
|
17
|
+
makes = resp.vehicle_search_response[0].vehicle_years[0].vehicle_year[0].
|
18
|
+
vehicle_makes[0].vehicle_make
|
19
|
+
|
20
|
+
printf( "Search returned %d makes of vehicle for 2008.\n\n", makes.size )
|
21
|
+
|
22
|
+
makes.each { |make| puts make, '' }
|
data/lib/ruby-paa.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 = ENV['AMAZONRCFILE'] || '.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
|