-
Notifications
You must be signed in to change notification settings - Fork 8
/
amazon.rb
executable file
·139 lines (102 loc) · 3.08 KB
/
amazon.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# $Id: amazon.rb,v 1.25 2008/10/03 09:35:37 ianmacd Exp $
#
module Amazon
# A top-level exception container class.
#
class AmazonError < StandardError; end
NAME = 'Ruby/Amazon'
@@config = {}
# Prints debugging messages and works like printf, except that it prints
# only when Ruby is run with the -d switch.
#
def Amazon.dprintf(format='', *args)
$stderr.printf( format + "\n", *args ) if $DEBUG
end
# Encode a string, such that it is suitable for HTTP transmission.
#
def Amazon.url_encode(string)
# Shamelessly plagiarised from Wakou Aoyama's cgi.rb.
#
string.gsub( /([^ a-zA-Z0-9_.-]+)/n ) do
'%' + $1.unpack( 'H2' * $1.size ).join( '%' ).upcase
end.tr( ' ', '+' )
end
# Convert a string from CamelCase to ruby_case.
#
def Amazon.uncamelise(str)
# Avoid modifying by reference.
#
str = str.dup
# Don't mess with string if all caps.
#
str.gsub!( /(.+?)(([A-Z][a-z]|[A-Z]+$))/, "\\1_\\2" ) if str =~ /[a-z]/
# Convert to lower case.
#
str.downcase
end
# A Class for dealing with configuration files, such as
# <tt>/etc/amazonrc</tt> and <tt>~/.amazonrc</tt>.
#
class Config < Hash
require 'stringio'
# Exception class for configuration file errors.
#
class ConfigError < AmazonError; end
# A configuration may be passed in as a string. Otherwise, the files
# <tt>/etc/amazonrc</tt> and <tt>~/.amazonrc</tt> are read if they exist
# and are readable.
#
def initialize(config_str=nil)
if config_str
# We have been passed a config file as a string.
#
config_files = [ config_str ]
config_class = StringIO
else
# Perform the usual search for the system and user config files.
#
config_files = [ File.join( '', 'etc', 'amazonrc' ) ]
# Figure out where home is. The locations after HOME are for Windows.
# [ruby-core:12347]
#
home = ENV['AMAZONRCDIR'] ||
ENV['HOME'] || ENV['HOMEDRIVE'] + ENV['HOMEPATH'] ||
ENV['USERPROFILE']
user_rcfile = ENV['AMAZONRCFILE'] || '.amazonrc'
if home
config_files << File.expand_path( File.join( home, user_rcfile ) )
end
config_class = File
end
config_files.each do |cf|
if config_class == StringIO
readable = true
else
# We must determine whether the file is readable.
#
readable = File.exists?( cf ) && File.readable?( cf )
end
if readable
Amazon.dprintf( 'Opening %s ...', cf ) if config_class == File
config_class.open( cf ) { |f| lines = f.readlines }.each do |line|
line.chomp!
# Skip comments and blank lines.
#
next if line =~ /^(#|$)/
Amazon.dprintf( 'Read: %s', line )
# Store these, because we'll probably find a use for these later.
#
begin
match = line.match( /^\s*(\S+)\s*=\s*(['"]?)([^'"]+)(['"]?)/ )
key, begin_quote, val, end_quote = match[1, 4]
raise ConfigError if begin_quote != end_quote
rescue NoMethodError, ConfigError
raise ConfigError, "bad config line: #{line}"
end
self[key] = val
end
end
end
end
end
end