Class: Rudy::AWS::SDB

Inherits:
Object
  • Object
show all
Includes:
ObjectBase
Defined in:
lib/rudy/aws/sdb.rb,
lib/rudy/aws/sdb/error.rb

Defined Under Namespace

Classes: ConnectionError, Error, FeatureDeprecatedError, InvalidDomainNameError, InvalidNextTokenError, InvalidNumberPredicatesError, InvalidNumberValueTestsError, InvalidParameterValueError, InvalidQueryExpressionError, MissingParameterError, NoAccessKey, NoSecretKey, NoSuchDomainError, NumberDomainAttributesExceededError, NumberDomainBytesExceededError, NumberDomainsExceededError, NumberItemAttributesExceededError, RequestError, RequestTimeoutError

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Huxtable

change_environment, change_position, change_region, change_role, change_zone, #check_keys, #config_dirname, create_domain, #current_group_name, #current_machine_address, #current_machine_count, #current_machine_group, #current_machine_hostname, #current_machine_image, #current_machine_name, #current_machine_size, #current_user, #current_user_keypairpath, debug?, #debug?, domain, domain_exists?, #group_metadata, #has_keypair?, #has_keys?, #has_pem_keys?, #has_root_keypair?, keypair_path_to_name, #known_machine_group?, #root_keypairname, #root_keypairpath, #switch_user, update_config, update_global, update_logger, #user_keypairname, #user_keypairpath

Constructor Details

#initialize(access_key = nil, secret_key = nil, region = nil, debug = nil) ⇒ SDB

Returns a new instance of SDB.

Raises:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rudy/aws/sdb.rb', line 20

def initialize(access_key=nil, secret_key=nil, region=nil, debug=nil)
  raise NoAccessKey if access_key.nil? || access_key.empty?
  raise NoSecretKey if secret_key.nil? || secret_key.empty?
    
  url ||= 'http://sdb.amazonaws.com'
  # There is a bug with passing :server to EC2::Base.new so 
  # we'll use the environment variable for now. 
  #if region && Rudy::AWS.valid_region?(region)
  #  "#{region}.sdb.amazonaws.com"
  #end
  
  @access_key_id = access_key || ENV['AWS_ACCESS_KEY']
  @secret_access_key = secret_key || ENV['AWS_SECRET_KEY']
  @base_url = url
  @debug = debug || StringIO.new
end

Class Method Details

.generate_query(*args) ⇒ Object

Takes a zipped Array or Hash of criteria. Returns a string suitable for a SimpleDB Query



81
82
83
84
85
86
87
88
# File 'lib/rudy/aws/sdb.rb', line 81

def self.generate_query(*args)
  q = args.first.is_a?(Hash)? args.first : Hash[*args.flatten]
  query = []
  q.each do |n,v| 
    query << "['#{Rudy::AWS.escape n}'='#{Rudy::AWS.escape v}']"
  end
  query.join(" intersection ")
end

.generate_select(*args) ⇒ Object

Takes a zipped Array or Hash of criteria. Returns a string suitable for a SimpleDB Select



68
69
70
71
72
73
74
75
76
# File 'lib/rudy/aws/sdb.rb', line 68

def self.generate_select(*args)
  fields, domain, args = *args
  q = args.is_a?(Hash) ? args : Hash[*args.flatten]
  query = []
  q.each do |n,v| 
    query << "#{Rudy::AWS.escape n}='#{Rudy::AWS.escape v}'"
  end
  "select * from #{domain} where " << query.join(' and ')
end

Instance Method Details

#create_domain(domain) ⇒ Object



52
53
54
55
# File 'lib/rudy/aws/sdb.rb', line 52

def create_domain(domain)
  call(:post, { 'Action' => 'CreateDomain', 'DomainName'=> domain.to_s })
  true
end

#delete_attributes(domain, item) ⇒ Object Also known as: destroy



238
239
240
241
242
243
244
245
246
247
248
# File 'lib/rudy/aws/sdb.rb', line 238

def delete_attributes(domain, item)
  call(
    :delete,
    {
      'Action' => 'DeleteAttributes',
      'DomainName' => domain.to_s,
      'ItemName' => item.to_s
    }
  )
  true
end

#destroy_domain(domain) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/rudy/aws/sdb.rb', line 57

def destroy_domain(domain)
  call(
    :delete,
    { 'Action' => 'DeleteDomain', 'DomainName' => domain.to_s }
  )
  true
end

#get_attributes(domain, item) ⇒ Object Also known as: get



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/rudy/aws/sdb.rb', line 215

def get_attributes(domain, item)
  doc = call(
    :get,
    {
      'Action' => 'GetAttributes',
      'DomainName' => domain.to_s,
      'ItemName' => item.to_s
    }
  )
  attributes = {}
  if doc
    REXML::XPath.each(doc, "//Attribute") do |attr|
      key = REXML::XPath.first(attr, './Name/text()').to_s
      value = REXML::XPath.first(attr, './Value/text()').to_s
      ( attributes[key] ||= [] ) << value
    end
  end
  attributes = nil if attributes.empty?
  attributes
end

#list_domains(max = nil, token = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rudy/aws/sdb.rb', line 37

def list_domains(max = nil, token = nil)
  params = { 'Action' => 'ListDomains' }
  params['NextToken'] =
    token unless token.nil? || token.empty?
  params['MaxNumberOfDomains'] =
    max.to_s unless max.nil? || max.to_i == 0
  doc = call(:get, params)
  results = []
  REXML::XPath.each(doc, '//DomainName/text()') do |domain|
    results << domain.to_s
  end
  #return results, REXML::XPath.first(doc, '//NextToken/text()').to_s
  results
end

#put_attributes(domain, item, attributes, replace = true) ⇒ Object Also known as: put



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/rudy/aws/sdb.rb', line 191

def put_attributes(domain, item, attributes, replace = true)
  replace = true if replace == :replace
  params = {
    'Action' => 'PutAttributes',
    'DomainName' => domain.to_s,
    'ItemName' => item.to_s
  }
  count = 0
  
  attributes.each do | key, values |
    ([]<<values).flatten.each do |value|
      params["Attribute.#{count}.Name"] = key.to_s
      params["Attribute.#{count}.Value"] = value.to_s
      params["Attribute.#{count}.Replace"] = replace
      count += 1
    end
  end
  
  call(:put, params)
  
  true
end

#query(domain, query, max = nil, token = nil) ⇒ Object

<QueryResult><ItemName>in-c2ffrw</ItemName><ItemName>in-72yagt</ItemName><ItemName>in-52j8gj</ItemName>



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/rudy/aws/sdb.rb', line 125

def query(domain, query, max = nil, token = nil)
  params = {
    'Action' => 'Query',
    'QueryExpression' => query,
    'DomainName' => domain.to_s
  }
  params['NextToken'] =
    token unless token.nil? || token.empty?
  params['MaxNumberOfItems'] =
    max.to_s unless max.nil? || max.to_i == 0


  doc = call(:get, params)
  results = []
  if doc
    REXML::XPath.each(doc, '//ItemName/text()') do |item|
      results << item.to_s
    end
  end
  
  #return results, REXML::XPath.first(doc, '//NextToken/text()').to_s
  results.empty? ? nil : results
  
end

#query_with_attributes(domain, query, max = nil, token = nil) ⇒ Object

<QueryWithAttributesResult><Item><Name>in-c2ffrw</Name><Attribute><Name>code</Name><Value>in-c2ffrw</Value></Attribute><Attribute><Name>date_created</Name><Value>2008-10-31</Value></Attribute></Item><Item>



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/rudy/aws/sdb.rb', line 153

def query_with_attributes(domain, query, max = nil, token = nil)
  params = {
    'Action' => 'QueryWithAttributes',
    'QueryExpression' => query,
    'DomainName' => domain.to_s
  }
  params['NextToken'] =
    token unless token.nil? || token.empty?
  params['MaxNumberOfItems'] =
    max.to_s unless max.nil? || max.to_i == 0

  doc = call(:get, params)
  results = []
  if doc
    REXML::XPath.each(doc, "//Item") do |item|
      name = REXML::XPath.first(item, './Name/text()').to_s

      attributes = {'Name' => name}
      REXML::XPath.each(item, "./Attribute") do |attr|
        key = REXML::XPath.first(attr, './Name/text()').to_s
        value = REXML::XPath.first(attr, './Value/text()').to_s
        ( attributes[key] ||= [] ) << value
      end
      results << attributes
    end
    #return results, REXML::XPath.first(doc, '//NextToken/text()').to_s
  end
  
  hash_results = {}
  results.each do |item|
    hash_results[item.delete('Name')] = item
  end
  
  hash_results
end

#select(select, token = nil) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rudy/aws/sdb.rb', line 91

def select(select, token = nil)
  params = {
    'Action' => 'Select',
    'SelectExpression' => select,
  }
  params['NextToken'] =
    token unless token.nil? || token.empty?
    
  doc = call(:get, params)
  results = []
  if doc
    REXML::XPath.each(doc, "//Item") do |item|
      name = REXML::XPath.first(item, './Name/text()').to_s

      attributes = {'Name' => name}
      REXML::XPath.each(item, "./Attribute") do |attr|
        key = REXML::XPath.first(attr, './Name/text()').to_s
        value = REXML::XPath.first(attr, './Value/text()').to_s
        ( attributes[key] ||= [] ) << value
      end
      results << attributes
    end
    #return results, REXML::XPath.first(doc, '//NextToken/text()').to_s
  end
  
  hash_results = {}
  results.each do |item|
    hash_results[item.delete('Name')] = item
  end
  
  hash_results.empty? ? nil : hash_results
end