Class: Awis::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/awis/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConnection

Returns a new instance of Connection.

Raises:



13
14
15
16
17
# File 'lib/awis/connection.rb', line 13

def initialize
  raise CertificateError.new("Amazon access certificate is missing!") if Awis.config.access_key_id.nil? || Awis.config.secret_access_key.nil?

  setup_options!
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



10
11
12
# File 'lib/awis/connection.rb', line 10

def debug
  @debug
end

#paramsObject



26
27
28
# File 'lib/awis/connection.rb', line 26

def params
  @params ||= {}
end

#protocolObject

Returns the value of attribute protocol.



10
11
12
# File 'lib/awis/connection.rb', line 10

def protocol
  @protocol
end

Instance Method Details

#default_paramsObject



93
94
95
96
97
98
99
100
101
# File 'lib/awis/connection.rb', line 93

def default_params
  {
    "AWSAccessKeyId"   => Awis.config.access_key_id,
    "SignatureMethod"  => "HmacSHA256",
    "SignatureVersion" => Awis::API_SIGNATURE_VERSION,
    "Timestamp"        => timestamp,
    "Version"          => Awis::API_VERSION
  }
end

#get(params = {}) ⇒ Object



34
35
36
37
38
# File 'lib/awis/connection.rb', line 34

def get(params = {})
  setup_params(params)

  handle_response(request).body.force_encoding(Encoding::UTF_8)
end

#handle_response(response) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/awis/connection.rb', line 40

def handle_response(response)
  case response.status.to_i
  when 200...300
    response
  when 300...600
    if response.body.nil?
      raise ResponseError.new(nil, response)
    else
      xml = MultiXml.parse(response.body)
      message = xml["Response"]["Errors"]["Error"]["Message"]
      raise ResponseError.new(message, response)
    end
  else
    raise ResponseError.new("Unknown code: #{respnse.code}", response)
  end
end

#host_with_portObject



73
74
75
# File 'lib/awis/connection.rb', line 73

def host_with_port
  protocol + '://' + Awis::API_HOST
end

#original_paramsObject



111
112
113
# File 'lib/awis/connection.rb', line 111

def original_params
  query_params + "&Signature=" + CGI::escape(signature)
end

#query_paramsObject



107
108
109
# File 'lib/awis/connection.rb', line 107

def query_params
  default_params.merge(params).map { |key, value| "#{key}=#{CGI::escape(value.to_s)}" }.sort.join("&")
end

#requestObject



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

def request
  connection = Faraday.new(url: host_with_port) do |faraday|
    faraday.request  :url_encoded             # form-encode POST params
    faraday.response :logger do |logger|
      logger.filter(/(AWSAccessKeyId=)(\w+)/, '\1[REMOVED]')
    end if Awis.config.logger
    faraday.adapter  :net_http
  end

  connection.get do |req|
    req.url url_params
    req.options.open_timeout = @timeout
    req.options.timeout = @open_timeout
  end
end

#request_urlObject



89
90
91
# File 'lib/awis/connection.rb', line 89

def request_url
  URI.parse(host_with_port + url_params)
end

#setup_options!Object



19
20
21
22
23
24
# File 'lib/awis/connection.rb', line 19

def setup_options!
  @debug        = Awis.config.debug || false
  @protocol     = Awis.config.protocol || 'https'
  @timeout      = Awis.config.timeout || 5
  @open_timeout = Awis.config.open_timeout || 2
end

#setup_params(params) ⇒ Object



30
31
32
# File 'lib/awis/connection.rb', line 30

def setup_params(params)
  self.params = params
end

#signObject



103
104
105
# File 'lib/awis/connection.rb', line 103

def sign
  "GET\n" + Awis::API_HOST + "\n/\n" + query_params
end

#signatureObject



81
82
83
# File 'lib/awis/connection.rb', line 81

def signature
  Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new("sha256"), Awis.config.secret_access_key, sign)).strip
end

#timestampObject



77
78
79
# File 'lib/awis/connection.rb', line 77

def timestamp
  @timestamp ||= Time::now.utc.strftime("%Y-%m-%dT%H:%M:%S.000Z")
end

#url_paramsObject



85
86
87
# File 'lib/awis/connection.rb', line 85

def url_params
  '?' + original_params
end