Module: RAWS::SDB::Adapter::Adapter20090415

Included in:
RAWS::SDB::Adapter
Defined in:
lib/raws/sdb/adapter.rb

Constant Summary collapse

URI =
'https://sdb.amazonaws.com/'
PARAMS =
{'Version' => '2009-04-15'}
KEYWORDS =
%w'
  or and not from where select like null is order by asc desc in between
  intersection limit every
'
REXP_NAME =
/^[a-zA-Z_$]/

Instance Method Summary collapse

Instance Method Details

#batch_put_attributes(domain_name, items = {}, replaces = {}) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/raws/sdb/adapter.rb', line 139

def batch_put_attributes(domain_name, items={}, replaces={})
  params = {
    'Action'     => 'BatchPutAttributes',
    'DomainName' => domain_name
  }
  
  i = 0
  items.each do |key, attrs|
    params["Item.#{i}.ItemName"] = key
    params.merge!(pack_attrs(attrs, replaces[key], "Item.#{i}."))
    i += 1
  end

  connect('GET', URI, PARAMS.merge(params))
end

#connect(method, base_uri, params, parser = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/raws/sdb/adapter.rb', line 58

def connect(method, base_uri, params, parser={})
  doc = nil

  RAWS::SDB.http.connect(
    "#{base_uri}?#{sign(method, base_uri, params)}"
  ) do |request|
    request.method = method
    response = request.send
    doc = response.parse(parser)
    response
  end

  doc
end

#create_domain(domain_name) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/raws/sdb/adapter.rb', line 73

def create_domain(domain_name)
  params = {
    'Action'     => 'CreateDomain',
    'DomainName' => domain_name
  }

  connect('GET', URI, PARAMS.merge(params))
end

#delete_attributes(domain_name, item_name, attrs = {}) ⇒ Object



155
156
157
158
159
160
161
162
163
164
# File 'lib/raws/sdb/adapter.rb', line 155

def delete_attributes(domain_name, item_name, attrs={})
  params = {
    'Action'     => 'DeleteAttributes',
    'DomainName' => domain_name,
    'ItemName'   => item_name
  }
  params.merge!(pack_attrs(attrs))

  connect('GET', URI, PARAMS.merge(params))
end

#delete_domain(domain_name) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/raws/sdb/adapter.rb', line 82

def delete_domain(domain_name)
  params = {
    'Action'     => 'DeleteDomain',
    'DomainName' => domain_name
  }

  connect('GET', URI, PARAMS.merge(params))
end

#domain_metadata(domain_name) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/raws/sdb/adapter.rb', line 91

def (domain_name)
  params = {
    'Action'     => 'DomainMetadata',
    'DomainName' => domain_name
  }

  connect('GET', URI, PARAMS.merge(params))
end

#get_attributes(domain_name, item_name, *attrs) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/raws/sdb/adapter.rb', line 106

def get_attributes(domain_name, item_name, *attrs)
  params = {
    'Action'     => 'GetAttributes',
    'DomainName' => domain_name,
    'ItemName'   => item_name
  }

  i = 0
  attrs.each do |name|
    params["AttributeName.#{i}"] = name
    i += 1
  end

  connect(
    'GET',
    URI,
    PARAMS.merge(params),
    :multiple => %w'Attribute',
    :unpack   => %w'Attribute'
  )
end

#list_domains(params = {}, &block) ⇒ Object



100
101
102
103
104
# File 'lib/raws/sdb/adapter.rb', line 100

def list_domains(params={}, &block)
  params.merge!('Action' => 'ListDomains')

  connect('GET', URI, PARAMS.merge(params), :multiple => %w'DomainName')
end

#pack_attrs(attrs, replaces = nil, prefix = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/raws/sdb/adapter.rb', line 11

def pack_attrs(attrs, replaces=nil, prefix=nil)
  params = {}

  i = 1
  attrs.each do |key, val|
    if !replaces.nil? && replaces.include?(key)
      params["#{prefix}Attribute.#{i}.Replace"] = 'true'
    end

    if val.is_a? Array
      val.each do |v|
        params["#{prefix}Attribute.#{i}.Name"]  = key
        params["#{prefix}Attribute.#{i}.Value"] = v
        i += 1
      end
    else
      params["#{prefix}Attribute.#{i}.Name"]  = key
      params["#{prefix}Attribute.#{i}.Value"] = val
      i += 1
    end
  end

  params
end

#put_attributes(domain_name, item_name, attrs = {}, *replaces) ⇒ Object



128
129
130
131
132
133
134
135
136
137
# File 'lib/raws/sdb/adapter.rb', line 128

def put_attributes(domain_name, item_name, attrs={}, *replaces)
  params = {
    'Action'     => 'PutAttributes',
    'DomainName' => domain_name,
    'ItemName'   => item_name
  }
  params.merge!(pack_attrs(attrs, replaces))

  connect('GET', URI, PARAMS.merge(params))
end

#query_expr(expr, *params) ⇒ Object



174
175
176
177
178
179
180
181
182
# File 'lib/raws/sdb/adapter.rb', line 174

def query_expr(expr, *params)
  expr.gsub(/(\\)?(\?)/) do
    if $1
      "?"
    else
      "'#{params.shift.to_s.gsub(/(['])/, '\1\1')}'"
    end
  end
end

#quote(val) ⇒ Object



166
167
168
169
170
171
172
# File 'lib/raws/sdb/adapter.rb', line 166

def quote(val)
  if !REXP_NAME.match(val) || KEYWORDS.include?(val)
    "'#{val}'"
  else
    val
  end
end

#select(expr, expr_params = [], next_token = nil) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/raws/sdb/adapter.rb', line 184

def select(expr, expr_params=[], next_token=nil)
  params = {
    'Action'           => 'Select',
    'SelectExpression' => query_expr(expr, *expr_params)
  }
  params['NextToken'] = next_token if next_token

  connect(
    'GET',
    URI,
    PARAMS.merge(params),
    :multiple => %w'Item Attribute',
    :unpack   => %w'Attribute'
  )
end

#sign(method, base_uri, params) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/raws/sdb/adapter.rb', line 36

def sign(method, base_uri, params)
  path = {
    'AWSAccessKeyId'   => RAWS.aws_access_key_id,
    'SignatureMethod'  => 'HmacSHA256',
    'SignatureVersion' => '2',
    'Timestamp'        => Time.now.utc.iso8601
  }.merge(params).map do |key, val|
    "#{RAWS.escape(key)}=#{RAWS.escape(val)}"
  end.sort.join('&')

  uri = ::URI.parse(base_uri)
  "#{path}&Signature=" << RAWS.escape(
    [
      ::OpenSSL::HMAC.digest(
        ::OpenSSL::Digest::SHA256.new,
        RAWS.aws_secret_access_key,
        "#{method.upcase}\n#{uri.host.downcase}\n#{uri.path}\n#{path}"
      )
    ].pack('m').strip
  )
end