Skip to content

Instantly share code, notes, and snippets.

@loicginoux
Created December 15, 2014 12:46
Show Gist options
  • Save loicginoux/6ed431eb9abcdfcb10e0 to your computer and use it in GitHub Desktop.
Save loicginoux/6ed431eb9abcdfcb10e0 to your computer and use it in GitHub Desktop.
get AWIS - alexa rank from amazon api
BASE_URL = "http://awis.amazonaws.com/"
access_key_id = AppConfig.get_value('aws_access_key_id')
secret_access_key = AppConfig.get_value('aws_secret_access_key')
now = Time.now.utc.iso8601
url = "yahoo.com"
query = {'AWSAccessKeyId' =>access_key_id,
'Action' => "UrlInfo",
'ResponseGroup' => "Rank",
'SignatureMethod' => "HmacSHA1",
'SignatureVersion' => 2,
'Timestamp' => now,
'Url' => url
}
http_verb = "GET"
host_header = BASE_URL.gsub(/http(s)*:\/\//, "").gsub(/\//, "")
request_uri = "/"
string_to_sign = "#{http_verb}\n#{host_header}\n#{request_uri}\n#{query.to_query}"
signature_data = OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), secret_access_key, string_to_sign)
signature = Base64.encode64(signature_data).gsub(/\n|\r/, '')
signature = {'Signature' => signature}
query = query.merge(signature)
api_url = BASE_URL + '?' + query.to_query
parsed_url = URI.parse(api_url)
http = Net::HTTP.new(parsed_url.host, parsed_url.port)
request = Net::HTTP::Get.new(parsed_url.request_uri)
response = http.request(request)
response.body
JSON.parse(response.body)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment