Class: SimplyDB::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/simplydb/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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 = {})
  @options = {
    :protocol => 'https://',
    :host => 'sdb.amazonaws.com',
    :port => 443,
    :path => "/",
    :signature_version => '2',
    :version => '2009-04-15',
    :signature_method => 'HmacSHA256',
  }.merge(options)
end

Instance Attribute Details

#http_clientObject

Returns the value of attribute http_client.



8
9
10
# File 'lib/simplydb/client.rb', line 8

def http_client
  @http_client
end

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/simplydb/client.rb', line 8

def options
  @options
end

Instance Method Details

#base_urlObject



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#{options[:host]}\n/\n" + escape_hash(params)
end