Class: AwsSdb::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/aws_sdb/service.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Service

Returns a new instance of Service.



14
15
16
17
18
19
# File 'lib/aws_sdb/service.rb', line 14

def initialize(options={})
  @access_key_id = options[:access_key_id] || ENV['AMAZON_ACCESS_KEY_ID']
  @secret_access_key = options[:secret_access_key] || ENV['AMAZON_SECRET_ACCESS_KEY']
  @base_url = options[:url] || 'http://sdb.amazonaws.com'
  @logger = options[:logger] || Logger.new("aws_sdb.log")
end

Instance Method Details

#create_domain(domain) ⇒ Object



35
36
37
38
# File 'lib/aws_sdb/service.rb', line 35

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

#delete_attributes(domain, item) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/aws_sdb/service.rb', line 103

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

#delete_domain(domain) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/aws_sdb/service.rb', line 40

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

#get_attributes(domain, item) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/aws_sdb/service.rb', line 85

def get_attributes(domain, item)
  doc = call(
    :get,
    {
      'Action' => 'GetAttributes',
      'DomainName' => domain.to_s,
      'ItemName' => item.to_s
    }
  )
  attributes = {}
  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
  attributes
end

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



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/aws_sdb/service.rb', line 21

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
end

#put_attributes(domain, item, attributes, replace = true) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/aws_sdb/service.rb', line 66

def put_attributes(domain, item, attributes, replace = true)
  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)
  nil
end

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/aws_sdb/service.rb', line 48

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 = []
  REXML::XPath.each(doc, '//ItemName/text()') do |item|
    results << item.to_s
  end
  return results, REXML::XPath.first(doc, '//NextToken/text()').to_s
end