Class: AceClient::Base4

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

Direct Known Subclasses

Json4, Query4

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) ⇒ Base4

Returns a new instance of Base4.



9
10
11
12
13
# File 'lib/ace-client/base4.rb', line 9

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/base4.rb', line 5

def body
  @body
end

#datetimeObject

Returns the value of attribute datetime.



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

def datetime
  @datetime
end

#headersObject

Returns the value of attribute headers.



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

def headers
  @headers
end

#regionObject

Returns the value of attribute region.



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

def region
  @region
end

#serviceObject

Returns the value of attribute service.



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

def service
  @service
end

Instance Method Details

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



15
16
17
18
# File 'lib/ace-client/base4.rb', line 15

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

#add_authorization!Object



80
81
82
83
84
85
86
87
# File 'lib/ace-client/base4.rb', line 80

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



20
21
22
23
24
25
26
27
# File 'lib/ace-client/base4.rb', line 20

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ace-client/base4.rb', line 29

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ace-client/base4.rb', line 54

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



70
71
72
73
74
75
76
77
78
# File 'lib/ace-client/base4.rb', line 70

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