Class: AceClient::Query2
- Inherits:
-
Base
- Object
- Base
- AceClient::Query2
show all
- Defined in:
- lib/ace-client/query2.rb
Instance Attribute Summary collapse
Attributes inherited from Base
#access_key_id, #endpoint, #http_proxy, #last_response, #last_response_time, #secret_access_key, #use_ssl, #user_agent
Instance Method Summary
collapse
Methods inherited from Base
#endpoint_url, #record_response, #set_http_proxy
Constructor Details
#initialize(options = {}) ⇒ Query2
14
15
16
17
18
19
20
|
# File 'lib/ace-client/query2.rb', line 14
def initialize(options={})
super(options)
@signature_method = options[:signature_method] || 'HmacSHA256'
@sampler = options[:sampler]
@before_signature = options[:before_signature]
@before_request = options[:before_request]
end
|
Instance Attribute Details
#http_method ⇒ Object
Returns the value of attribute http_method.
7
8
9
|
# File 'lib/ace-client/query2.rb', line 7
def http_method
@http_method
end
|
#sampler ⇒ Object
Returns the value of attribute sampler.
9
10
11
|
# File 'lib/ace-client/query2.rb', line 9
def sampler
@sampler
end
|
#signature_method ⇒ Object
TODO: HMAC-SHA256 or HMAC-SHA1
8
9
10
|
# File 'lib/ace-client/query2.rb', line 8
def signature_method
@signature_method
end
|
Instance Method Details
#action(action, params = {}) ⇒ Object
22
23
24
25
|
# File 'lib/ace-client/query2.rb', line 22
def action(action, params={})
params.update('Action' => action)
execute(params)
end
|
#canonical_query_string ⇒ Object
110
111
112
113
114
|
# File 'lib/ace-client/query2.rb', line 110
def canonical_query_string
@params.sort.collect { |param|
"#{CGI::escape(param[0])}=#{CGI::escape(param[1])}"
}.join("&").gsub('+', '%20').gsub('%7E', '~')
end
|
#create_signature ⇒ Object
100
101
102
103
|
# File 'lib/ace-client/query2.rb', line 100
def create_signature
digest = OpenSSL::Digest::Digest.new(@signature_method.downcase.gsub(/hmac/, ''))
Base64.encode64(OpenSSL::HMAC.digest(digest, secret_access_key, string_to_sign)).strip
end
|
#dryrun(action, params = {}) ⇒ Object
27
28
29
30
|
# File 'lib/ace-client/query2.rb', line 27
def dryrun(action, params={})
params.update('Action' => action)
execute(params, true)
end
|
#execute(params = {}, dryrun = false) ⇒ Object
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
|
# File 'lib/ace-client/query2.rb', line 32
def execute(params={}, dryrun=false)
@params = params
@params.update(
'SignatureVersion' => '2',
'SignatureMethod' => @signature_method,
@access_key_id_key => @access_key_id,
'Timestamp' => Time.now.getutc.iso8601.sub(/Z/, sprintf(".%03dZ",(Time.now.getutc.usec/1000)))
)
@params['Version'] = @version if @version
@before_signature.call(@params) if @before_signature
@params['Signature'] = create_signature
options = self.class.default_options.dup
options[:headers] = {}
options[:headers]['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
options[:headers]['User-Agent'] = @user_agent if @user_agent
if http_method == :get
options[:query] = @params
http_method_class = Net::HTTP::Get
elsif http_method == :post
options[:body] = @params
http_method_class = Net::HTTP::Post
end
@before_request.call(@params) if @before_request
request = HTTParty::Request.new(http_method_class, endpoint_url + @path, options)
if dryrun
request
else
sample_request(request) if @sampler
response = record_response { request.perform }
sample_response(response) if @sampler
response
end
end
|
#sample_request(request) ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/ace-client/query2.rb', line 72
def sample_request(request)
query = (request.options[:query] || request.options[:body]).dup
variable_keys = %W(Version SignatureVersion SignatureMethod Timestamp #{access_key_id_key} Signature)
variables = {}
variable_keys.each do |key|
variables[key] = query.delete(key)
end
action = query.delete('Action')
@sampler[:output].puts "# #{action}"
@sampler[:output].puts "## request"
@sampler[:output].puts "#{request.path.to_s}"
@sampler[:output].puts " ?Action=#{action}"
query.each do |key, value|
@sampler[:output].puts " &#{key}=#{CGI.escape(value)}"
end
variable_keys.each do |key|
if variables[key]
value = @sampler[:echo][key] || CGI.escape(variables[key])
@sampler[:output].puts " &#{key}=#{value}"
end
end
end
|
#sample_response(response) ⇒ Object
95
96
97
98
|
# File 'lib/ace-client/query2.rb', line 95
def sample_response(response)
@sampler[:output].puts "## response"
@sampler[:output].puts Nokogiri::XML(response.body).to_xml(:indent => 4)
end
|
#string_to_sign ⇒ Object
105
106
107
108
|
# File 'lib/ace-client/query2.rb', line 105
def string_to_sign
host = @host_with_port ? @endpoint : @endpoint.gsub(/\:\d+$/, '')
[@http_method.to_s.upcase, @endpoint, @path, canonical_query_string].join("\n")
end
|