Class: AceClient::Query4

Inherits:
Base
  • Object
show all
Defined in:
lib/ace-client/query4.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#access_key_id, #endpoint, #http_method, #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) ⇒ Query4

Returns a new instance of Query4.



12
13
14
15
16
# File 'lib/ace-client/query4.rb', line 12

def initialize(options)
  super(options)
  @service = options[:service]
  @region = options[:region]
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



5
6
7
# File 'lib/ace-client/query4.rb', line 5

def body
  @body
end

#datetimeObject

Returns the value of attribute datetime.



7
8
9
# File 'lib/ace-client/query4.rb', line 7

def datetime
  @datetime
end

#headersObject

Returns the value of attribute headers.



3
4
5
# File 'lib/ace-client/query4.rb', line 3

def headers
  @headers
end

#queryObject

Returns the value of attribute query.



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

def query
  @query
end

#regionObject

Returns the value of attribute region.



4
5
6
# File 'lib/ace-client/query4.rb', line 4

def region
  @region
end

#serviceObject

Returns the value of attribute service.



6
7
8
# File 'lib/ace-client/query4.rb', line 6

def service
  @service
end

Instance Method Details

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



18
19
20
21
# File 'lib/ace-client/query4.rb', line 18

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

#add_authorization!Object



83
84
85
86
87
88
89
90
# File 'lib/ace-client/query4.rb', line 83

def add_authorization!
  headers['content-type'] ||= 'application/x-www-form-urlencoded'
  headers['host'] = endpoint
  headers['x-amz-date'] = datetime
  #headers['x-amz-security-token'] = credentials.session_token if credentials.session_token
  headers['x-amz-content-sha256'] ||= hexdigest(body || '')
  headers['authorization'] = authorization(datetime)
end

#execute(params) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/ace-client/query4.rb', line 23

def execute(params)
  @datetime = Time.now.utc.strftime("%Y%m%dT%H%M%SZ")
  if http_method == :get
    execute_get(params)
  else http_method == :post
    execute_post(params)
  end
end

#execute_get(params) ⇒ 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
# File 'lib/ace-client/query4.rb', line 32

def execute_get(params)
  @headers = {}
  @headers['host'] = @endpoint

  @params = params
  @params['Version'] = @version if @version
  @params.update(
    'X-Amz-Algorithm' => 'AWS4-HMAC-SHA256',
    'X-Amz-Credential' => access_key_id + '/' + credential_string(datetime),
    'X-Amz-Date' => datetime,
    'X-Amz-SignedHeaders' => signed_headers
  )
  options = self.class.default_options.dup
  options[:query] = @params
  request = HTTParty::Request.new(Net::HTTP::Get, endpoint_url + @path, options)
  @query = request.send(:normalize_query, options[:query])

  @params.update('X-Amz-Signature' => signature(datetime))

  @body = ''

  request = HTTParty::Request.new(Net::HTTP::Get, endpoint_url + @path, options)
  request.perform
end

#execute_post(params) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ace-client/query4.rb', line 57

def execute_post(params)
  @params = params
  options = {}
  options = self.class.default_options.dup
  options[:body] = @params
  request = HTTParty::Request.new(Net::HTTP::Post, endpoint_url + @path, options)
  @body = request.send(:normalize_query, options[:body])

  @headers = {}
  add_authorization!
  options[:headers] = headers

  request = HTTParty::Request.new(Net::HTTP::Post, endpoint_url + @path, options)
  request.perform
end

#querystringObject



73
74
75
76
77
78
79
80
81
# File 'lib/ace-client/query4.rb', line 73

def querystring
  if http_method == :post
    ''
  elsif http_method == :get
    @params.sort.collect { |param|
      "#{CGI::escape(param[0])}=#{CGI::escape(param[1])}"
    }.join("&").gsub('+', '%20').gsub('%7E', '~')
  end
end