Class: SimplyDB::Client
- Inherits:
-
Object
- Object
- SimplyDB::Client
- Defined in:
- lib/simplydb/client.rb
Instance Attribute Summary collapse
-
#http_client ⇒ Object
Returns the value of attribute http_client.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #base_url ⇒ Object
- #call(method, params, &block) ⇒ Object
- #generate_signature(method, params) ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #params_with_signature(method, params) ⇒ Object
- #string_to_sign(method, params) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/simplydb/client.rb', line 10 def initialize( = {}) @options = { :protocol => 'https://', :host => 'sdb.amazonaws.com', :port => 443, :path => "/", :signature_version => '2', :version => '2009-04-15', :signature_method => 'HmacSHA256', }.merge() end |
Instance Attribute Details
#http_client ⇒ Object
Returns the value of attribute http_client.
8 9 10 |
# File 'lib/simplydb/client.rb', line 8 def http_client @http_client end |
#options ⇒ Object
Returns the value of attribute options.
8 9 10 |
# File 'lib/simplydb/client.rb', line 8 def @options end |
Instance Method Details
#base_url ⇒ Object
22 23 24 |
# File 'lib/simplydb/client.rb', line 22 def base_url "#{@options[:protocol]}#{@options[:host]}:#{@options[:port]}#{@options[:path]}" end |
#call(method, params, &block) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/simplydb/client.rb', line 52 def call(method, params, &block) params = params_with_signature(method, params) response = case method.to_sym # when :get # RestClient.get(base_url << "?#{escape_hash(params)}") when :post RestClient.post(base_url, params) else raise "Not support request method #{method}" end block.call(response.body) rescue RestClient::BadRequest => e block.call(e.response.body) end |
#generate_signature(method, params) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/simplydb/client.rb', line 30 def generate_signature(method, params) Base64.encode64( OpenSSL::HMAC.digest( OpenSSL::Digest::Digest.new('sha256'), @options[:secret_key], string_to_sign(method, params) ) ).chomp end |
#params_with_signature(method, params) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/simplydb/client.rb', line 40 def params_with_signature(method, params) params.merge!({ 'AWSAccessKeyId' => @options[:access_key], 'SignatureVersion' => @options[:signature_version], 'Timestamp' => Time.now.iso8601, 'Version' => @options[:version], 'SignatureMethod' => @options[:signature_method] }) params['Signature'] = generate_signature(method, params) params end |
#string_to_sign(method, params) ⇒ Object
26 27 28 |
# File 'lib/simplydb/client.rb', line 26 def string_to_sign(method, params) return "#{method.to_s.upcase}\n#{[:host]}\n/\n" + escape_hash(params) end |