Class: AceClient::Query3

Inherits:
Base
  • Object
show all
Defined in:
lib/ace-client/query3.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 = {}) ⇒ Query3

Returns a new instance of Query3.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ace-client/query3.rb', line 14

def initialize(options={})
  super(options)
  @signature_method = options[:signature_method] || 'HmacSHA256'
  @authorization_key = options[:authorization_key] || 'authorization'
  @date_key = options[:date_key] || 'x-date'
  @nonce_key = options[:nonce_key] || 'x-amz-nonce'
  @authorization_prefix = options[:authorization_prefix] || 'AWS3-HTTPS'
  @nonce = options[:nonce]

  @sampler = options[:sampler]
  @before_signature = options[:before_signature]
  @before_request = options[:before_request]
end

Instance Attribute Details

#http_methodObject

Returns the value of attribute http_method.



8
9
10
# File 'lib/ace-client/query3.rb', line 8

def http_method
  @http_method
end

#samplerObject

Returns the value of attribute sampler.



10
11
12
# File 'lib/ace-client/query3.rb', line 10

def sampler
  @sampler
end

#signature_methodObject

TODO: HMAC-SHA256 or HMAC-SHA1



9
10
11
# File 'lib/ace-client/query3.rb', line 9

def signature_method
  @signature_method
end

Instance Method Details

#action(action, params = {}) ⇒ Object



28
29
30
31
# File 'lib/ace-client/query3.rb', line 28

def action(action, params={})
  params.update('Action' => action)
  execute(params)
end

#create_signatureObject



72
73
74
75
# File 'lib/ace-client/query3.rb', line 72

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

#dateObject



81
82
83
# File 'lib/ace-client/query3.rb', line 81

def date
  @date ||= Time.now.utc.rfc822.gsub(/[\-\+]\d{4}$/, 'GMT')
end

#dryrun(action, params = {}) ⇒ Object



33
34
35
36
# File 'lib/ace-client/query3.rb', line 33

def dryrun(action, params={})
  params.update('Action' => action)
  execute(params, true)
end

#execute(params = {}, dryrun = false) ⇒ Object



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/query3.rb', line 38

def execute(params={}, dryrun=false)
  @params = params
  @params['Version'] = @version if @version

  @before_signature.call(@params) if @before_signature

  signature = create_signature

  options = self.class.default_options.dup
  options[:headers] = {}
  options[:headers]['Date'] = date
  options[:headers][@authorization_key] = "#{@authorization_prefix} #{@access_key_id_key}=#{access_key_id},Algorithm=#{signature_method},Signature=#{signature}"
  options[:headers]['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
  options[:headers]['User-Agent'] = @user_agent if @user_agent
  options[:headers][@nonce_key] = @nonce if @nonce

  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
    record_response { request.perform }
  end
end

#string_to_signObject



77
78
79
# File 'lib/ace-client/query3.rb', line 77

def string_to_sign
  @nonce ? date + @nonce : date
end