Class: Amazon

Inherits:
Object
  • Object
show all
Defined in:
lib/river/amazon.rb

Defined Under Namespace

Classes: AmazonResultError

Constant Summary collapse

DIGEST_SUPPORT =

Do we have support for the SHA-256 Secure Hash Algorithm? Note that Module#constants returns Strings in Ruby 1.8 and Symbols in 1.9.

OpenSSL::Digest.constants.include?('SHA256') || OpenSSL::Digest.constants.include?(:SHA256)
DIGEST =

Requests are authenticated using the SHA-256 Secure Hash Algorithm.

OpenSSL::Digest::Digest.new('sha256')
AMAZON_SITES =
{
  :ca => 'http://ecs.amazonaws.ca/onca/xml',
  :de => 'http://ecs.amazonaws.de/onca/xml',
  :fr => 'http://ecs.amazonaws.fr/onca/xml',
  :jp => 'http://ecs.amazonaws.jp/onca/xml',
  :uk => 'http://ecs.amazonaws.co.uk/onca/xml',
  :us => 'http://ecs.amazonaws.com/onca/xml'
}
AMAZON_XSLT_SITES =
{
  :ca => 'http://xml-ca.amznxslt.com/onca/xml',
  :de => 'http://xml-de.amznxslt.com/onca/xml',
  :fr => 'http://xml-fr.amznxslt.com/onca/xml',
  :jp => 'http://xml-jp.amznxslt.com/onca/xml',
  :uk => 'http://xml-uk.amznxslt.com/onca/xml',
  :us => 'http://xml-us.amznxslt.com/onca/xml'
}

Class Method Summary collapse

Class Method Details

.sign_query(uri, query, amazon_secret_access_key) ⇒ Object

Sign an amazon query Based on ruby-aaws and documentation here www.caliban.org/ruby/ruby-aws/ docs.amazonwebservices.com/AWSECommerceService/latest/DG/index.html?RequestAuthenticationArticle.html Parameters query: The query to be signed



36
37
38
39
40
41
42
43
44
45
# File 'lib/river/amazon.rb', line 36

def self.sign_query(uri, query, amazon_secret_access_key)
  raise 'SHA-256 not available in this version of openssl.  Cannot sign Amazon requests.' unless DIGEST_SUPPORT
  query << "&Timestamp=#{Time.now.utc.strftime('%Y-%m-%dT%H:%M:%SZ')}"
  new_query = query.split('&').collect{|param| "#{param.split('=')[0]}=#{url_encode(param.split('=')[1])}"}.sort.join('&')
  to_sign = "GET\n%s\n%s\n%s" % [uri.host, uri.path, new_query]
  hmac = OpenSSL::HMAC.digest(DIGEST, amazon_secret_access_key, to_sign)
  base64_hmac = [hmac].pack('m').chomp
  signature = url_encode(base64_hmac)
  new_query << "&Signature=#{signature}"
end

.url_encode(string) ⇒ Object

Encode a string, such that it is suitable for HTTP transmission.



48
49
50
51
52
53
54
# File 'lib/river/amazon.rb', line 48

def self.url_encode(string)
  return '' if string.nil?
  # Shamelessly plagiarised from Wakou Aoyama's cgi.rb, but then altered slightly to please AWS.
  string.gsub( /([^a-zA-Z0-9_.~-]+)/ ) do
    '%' + $1.unpack( 'H2' * $1.bytesize ).join( '%' ).upcase
  end
end