Skip to content

Instantly share code, notes, and snippets.

@JunKikuchi
Created July 16, 2009 11:40
Show Gist options
  • Save JunKikuchi/148372 to your computer and use it in GitHub Desktop.
Save JunKikuchi/148372 to your computer and use it in GitHub Desktop.
require 'uri'
require 'time'
require 'openssl'
require 'rubygems'
require 'typhoeus'
require 'nokogiri'
class JAWS
include Typhoeus
class << self
attr_accessor :aws_access_key_id
attr_accessor :aws_secret_access_key
end
def self.escape(val)
URI.escape(val, /([^a-zA-Z0-9\-_.~]+)/n)
end
def self.sign(http_verb, base_uri, params)
uri = URI.parse(base_uri)
path = {
'AWSAccessKeyId' => aws_access_key_id,
'SignatureMethod' => 'HmacSHA256',
'SignatureVersion' => '2',
'Timestamp' => Time.now.utc.iso8601
}.merge(params).map do |key, val|
"#{escape(key)}=#{escape(val)}"
end.sort.join('&')
"#{path}&Signature=" + escape(
[
::OpenSSL::HMAC.digest(
::OpenSSL::Digest::Digest.new("sha256"),
aws_secret_access_key,
"#{http_verb.upcase}\n#{uri.host.downcase}\n#{uri.path}\n#{path}"
)
].pack('m').strip
)
end
def self.send(http_verb, base_uri, params)
get\
"#{base_uri}?#{sign(http_verb, base_uri, params)}",
:on_success => lambda { |r|
Nokogiri::XML.parse(r.body)
}
end
class SDB
module Adapter
module Adapter20090415
URI = 'https://sdb.amazonaws.com/'
PARAMS = {'Version' => '2009-04-15'}
def list_domain(params={}, &block)
JAWS.send(
'GET',
URI,
PARAMS.merge('Action' => 'ListDomains').merge(params)
).xpath('//domainname').each do |val|
block.call val.content
end
end
end
extend Adapter20090415
end
def self.each(&block)
Adapter.list_domain(&block)
end
def self.[](key)
end
class Domain
attr_reader :name
def initialize(name)
@name = name
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment