Class: Amazon::WebServices::Util::ConvenienceWrapper
- Inherits:
-
Object
- Object
- Amazon::WebServices::Util::ConvenienceWrapper
show all
- Includes:
- Util::Logging
- Defined in:
- lib/amazon/webservices/util/convenience_wrapper.rb
Constant Summary
collapse
- REQUIRED_PARAMETERS =
[:ServiceClass]
- DEFAULT_UNIQUE_REQUEST_TOKEN_FACTORY =
proc do |params|
time_hash = Time.now.hash.to_s(36)
rand_hash = rand(1679615).to_s(36)
params_hash = params.hash.to_s(36)
"#{params_hash}#{time_hash}#{rand_hash}"
end
Class Method Summary
collapse
Instance Method Summary
collapse
deprecated, #log, #set_log
Constructor Details
Returns a new instance of ConvenienceWrapper.
15
16
17
18
19
|
# File 'lib/amazon/webservices/util/convenience_wrapper.rb', line 15
def initialize(args)
missing_parameters = REQUIRED_PARAMETERS - args.keys
raise "Missing paramters: #{missing_parameters.join(',')}" unless missing_parameters.empty?
@service = args[:ServiceClass].new( args )
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
25
26
27
28
29
30
31
|
# File 'lib/amazon/webservices/util/convenience_wrapper.rb', line 25
def method_missing( method, *args )
if @service.respond_to? method
callService( method, *args )
else
callService( ConvenienceWrapper.real_method( method ), *args )
end
end
|
Class Method Details
.idempotent(method, key = :UniqueRequestToken, factory = DEFAULT_UNIQUE_REQUEST_TOKEN_FACTORY) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/amazon/webservices/util/convenience_wrapper.rb', line 61
def self.idempotent( method, key=:UniqueRequestToken, factory = DEFAULT_UNIQUE_REQUEST_TOKEN_FACTORY )
method = method.to_s
method = ( method[0..0].downcase + method[1..-1] ).to_sym
old_method = instance_method(method)
define_method(method) do |*params|
userArgs = params[0] || {}
args = if userArgs.has_key?(key)
userArgs
else
{ key => factory.call(userArgs) }.merge(userArgs)
end
old_method.bind(self).(args)
end
end
|
.paginate(method, elementTag, pageSize = 25) ⇒ Object
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
|
# File 'lib/amazon/webservices/util/convenience_wrapper.rb', line 77
def self.paginate( method, elementTag, pageSize=25 )
method = method.to_s
all_name = ( method[0..0].downcase + method[1..-1] + "All" ).to_sym
iterator_name = ( method[0..0].downcase + method[1..-1] + "Iterator" ).to_sym
proactive_name = ( method[0..0].downcase + method[1..-1] + "AllProactive" ).to_sym
method = ( method[0..0].downcase + method[1..-1] ).to_sym
raise 'Stop redifining service methods!' if self.instance_methods.include? name.to_s
processors = { all_name => Amazon::Util::LazyResults,
iterator_name => Amazon::Util::PaginatedIterator,
proactive_name => Amazon::Util::ProactiveResults }
processors.each do |name,processor|
define_method( name ) do |*params|
userArgs = params[0] || {}
args = {:PageSize => pageSize}.merge( userArgs ) return processor.new do |pageNumber|
pageArgs = args.merge({:PageNumber => pageNumber}) res = self.send( method, pageArgs)[elementTag]
res.nil? ? nil : [res].flatten
end
end
end
end
|
.real_method(method) ⇒ Object
104
105
106
107
|
# File 'lib/amazon/webservices/util/convenience_wrapper.rb', line 104
def self.real_method( method )
method = method.to_s
method = ( method[0..0].upcase + method[1..-1] ).to_sym
end
|
.serviceCall(method, responseTag, defaultArgs = {}) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/amazon/webservices/util/convenience_wrapper.rb', line 33
def self.serviceCall( method, responseTag, defaultArgs={} )
method = method.to_s
name = ( method[0..0].downcase + method[1..-1] ).to_sym
rawName = ( name.to_s + "Raw" ).to_sym
method = real_method( method )
raise 'Stop redifining service methods!' if self.instance_methods.include? name.to_s
define_method( rawName ) do |args|
log "Sending service request '#{name}' with args: #{args.inspect}"
result = callService( method, args )
return result[responseTag]
end
define_method( name ) do |*params|
userArgs = params[0] || {}
args = defaultArgs.merge( userArgs )
self.send rawName, args
end
end
|
Instance Method Details
#callService(method, *args) ⇒ Object
21
22
23
|
# File 'lib/amazon/webservices/util/convenience_wrapper.rb', line 21
def callService( method, *args )
@service.send( method, *args )
end
|