Class: Remit::SignedQuery

Inherits:
Relax::Query
  • Object
show all
Defined in:
lib/remit/common.rb

Direct Known Subclasses

ApiQuery

Constant Summary collapse

UNSAFE =
/[^A-Za-z0-9_.~-]/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, secret_key, query = {}) ⇒ SignedQuery

Returns a new instance of SignedQuery.



77
78
79
80
81
82
# File 'lib/remit/common.rb', line 77

def initialize(uri, secret_key, query = {})
  super(query)
  @uri = URI.parse(uri.to_s)
  @secret_key = secret_key
  sign
end

Class Method Details

.escape_value(value) ⇒ Object

Amazon is very specific about what chars should be escaped, and which should not.



114
115
116
117
# File 'lib/remit/common.rb', line 114

def escape_value(value)
  # note that URI.escape(' ') => '%20', and CGI.escape(' ') => '+'
  URI.escape(value.to_s, UNSAFE)
end

.parse(uri, secret_key, query_string) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/remit/common.rb', line 103

def parse(uri, secret_key, query_string)
  query = self.new(uri, secret_key)
  query_string.split('&').each do |parameter|
    key, value = parameter.split('=', 2)
    query[key] = unescape_value(value)
  end
  query
end

Instance Method Details

#signObject



84
85
86
87
88
# File 'lib/remit/common.rb', line 84

def sign
  store(:signatureVersion, Remit::API::SIGNATURE_VERSION)
  store(:signatureMethod, Remit::API::SIGNATURE_METHOD)
  store(:signature, signature)
end

#signable_stringObject



94
95
96
97
98
99
100
# File 'lib/remit/common.rb', line 94

def signable_string
  [ 'GET',
    @uri.host,
    @uri.path,
    to_s # the query string, sans :signature (but with :signatureVersion and :signatureMethod)
  ].join("\n")
end

#signatureObject



90
91
92
# File 'lib/remit/common.rb', line 90

def signature
  Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::SHA256.new, @secret_key, signable_string)).strip
end