Class: Boomerang::APICall

Inherits:
Object
  • Object
show all
Defined in:
lib/boomerang/api_call.rb

Constant Summary collapse

CA_PATH =
File.join(File.dirname(__FILE__), *%w[.. .. data ca-bundle.crt])

Instance Method Summary collapse

Constructor Details

#initialize(host, action, parameters) ⇒ APICall

Returns a new instance of APICall.



5
6
7
8
9
10
# File 'lib/boomerang/api_call.rb', line 5

def initialize(host, action, parameters)
  @host       = host
  @parameters = Utilities.CamelCase(parameters)
                         .merge( "Action"  => Utilities.CamelCase(action),
                                 "Version" => "2008-09-17" )
end

Instance Method Details

#responseObject



18
19
20
21
22
23
24
25
26
27
28
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/boomerang/api_call.rb', line 18

def response
  url               = "#{@host}/?#{Utilities.build_query(@parameters)}"
  uri               = URI.parse(url)
  http              = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl      = true
  http.ca_file      = CA_PATH
  http.verify_mode  = OpenSSL::SSL::VERIFY_PEER
  http.verify_depth = 5
  
  begin
    response = http.start { |session|
      get    = Net::HTTP::Get.new("#{uri.path}?#{uri.query}")
      if (response = session.request(get)).is_a? Net::HTTPSuccess
        begin
          response.body
        rescue StandardError => error
          fail wrap_error( "HTTP",
                           "#{error.message} (#{error.class.name})",
                           http_response:  response,
                           original_error: error )
        end
      else
        fail wrap_error( "HTTP",
                         "#{response.message} (#{response.class.name})",
                         http_response: response )
      end
    }
  rescue Errors::HTTPError
    fail  # pass through already wrapped errors
  rescue StandardError => error
    fail wrap_error( "Connection",
                     "#{error.message} (#{error.class.name})",
                     original_error: error )
  end
end

#sign(access_key_id, secret_access_key) ⇒ Object



12
13
14
15
16
# File 'lib/boomerang/api_call.rb', line 12

def sign(access_key_id, secret_access_key)
  signature = Signature.new("GET", @host, @parameters)
  signature.sign(access_key_id, secret_access_key)
  @parameters = signature.signed_fields
end