Class: Awis::Connection
- Inherits:
-
Object
- Object
- Awis::Connection
- Defined in:
- lib/awis/connection.rb
Instance Attribute Summary collapse
-
#debug ⇒ Object
Returns the value of attribute debug.
- #params ⇒ Object
-
#protocol ⇒ Object
Returns the value of attribute protocol.
Instance Method Summary collapse
- #default_params ⇒ Object
- #get(params = {}) ⇒ Object
- #handle_response(response) ⇒ Object
- #host_with_port ⇒ Object
-
#initialize ⇒ Connection
constructor
A new instance of Connection.
- #original_params ⇒ Object
- #query_params ⇒ Object
- #request ⇒ Object
- #request_url ⇒ Object
- #setup_options! ⇒ Object
- #setup_params(params) ⇒ Object
- #sign ⇒ Object
- #signature ⇒ Object
- #timestamp ⇒ Object
- #url_params ⇒ Object
Constructor Details
#initialize ⇒ Connection
Returns a new instance of Connection.
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? end |
Instance Attribute Details
#debug ⇒ Object
Returns the value of attribute debug.
10 11 12 |
# File 'lib/awis/connection.rb', line 10 def debug @debug end |
#params ⇒ Object
26 27 28 |
# File 'lib/awis/connection.rb', line 26 def params @params ||= {} end |
#protocol ⇒ Object
Returns the value of attribute protocol.
10 11 12 |
# File 'lib/awis/connection.rb', line 10 def protocol @protocol end |
Instance Method Details
#default_params ⇒ Object
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" => , "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) = xml["Response"]["Errors"]["Error"]["Message"] raise ResponseError.new(, response) end else raise ResponseError.new("Unknown code: #{respnse.code}", response) end end |
#host_with_port ⇒ Object
73 74 75 |
# File 'lib/awis/connection.rb', line 73 def host_with_port protocol + '://' + Awis::API_HOST end |
#original_params ⇒ Object
111 112 113 |
# File 'lib/awis/connection.rb', line 111 def original_params query_params + "&Signature=" + CGI::escape(signature) end |
#query_params ⇒ Object
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 |
#request ⇒ Object
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..open_timeout = @timeout req..timeout = @open_timeout end end |
#request_url ⇒ Object
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 @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 |
#sign ⇒ Object
103 104 105 |
# File 'lib/awis/connection.rb', line 103 def sign "GET\n" + Awis::API_HOST + "\n/\n" + query_params end |
#signature ⇒ Object
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 |
#timestamp ⇒ Object
77 78 79 |
# File 'lib/awis/connection.rb', line 77 def ||= Time::now.utc.strftime("%Y-%m-%dT%H:%M:%S.000Z") end |
#url_params ⇒ Object
85 86 87 |
# File 'lib/awis/connection.rb', line 85 def url_params '?' + original_params end |