Sha256: 4a55ffa1ba43640488063620c03a683c425ef5eaa252803c89ec56b866a2a7f9
Contents?: true
Size: 1.9 KB
Versions: 1
Compression:
Stored size: 1.9 KB
Contents
require "cgi" require "base64" require "openssl" require "digest/sha1" require "faraday" require "time" module Alexa class Connection attr_accessor :secret_access_key, :access_key_id attr_writer :params def initialize(credentials = {}) self.secret_access_key = credentials.fetch(:secret_access_key) self.access_key_id = credentials.fetch(:access_key_id) end def params @params ||= {} end def get(params = {}) self.params = params handle_response(request).body.force_encoding(Encoding::UTF_8) end 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 def request Faraday.get(uri) end def timestamp @timestamp ||= Time::now.utc.strftime("%Y-%m-%dT%H:%M:%S.000Z") end def signature Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new("sha256"), secret_access_key, sign)).strip end def uri URI.parse("http://#{Alexa::API_HOST}/?" + query + "&Signature=" + CGI::escape(signature)) end def default_params { "AWSAccessKeyId" => access_key_id, "SignatureMethod" => "HmacSHA256", "SignatureVersion" => "2", "Timestamp" => timestamp, "Version" => Alexa::API_VERSION } end def sign "GET\n" + Alexa::API_HOST + "\n/\n" + query end def query default_params.merge(params).map { |key, value| "#{key}=#{CGI::escape(value.to_s)}" }.sort.join("&") end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
alexa-0.6.0 | lib/alexa/connection.rb |