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.



26
27
28
29
30
31
# File 'lib/aws_sdb/service.rb', line 26

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



47
48
49
50
51
# File 'lib/aws_sdb/service.rb', line 47

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

#delete_attributes(domain, item) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/aws_sdb/service.rb', line 146

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

#delete_domain(domain) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/aws_sdb/service.rb', line 53

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

#get_attributes(domain, item, consistent_read = 'true') ⇒ Object

updated to support consitent read



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/aws_sdb/service.rb', line 124

def get_attributes(domain, item, consistent_read = 'true')
  doc = call(
    :get,
    {
      'Action' => 'GetAttributes',
      'DomainName' => domain.to_s,
      'ItemName' => item.to_s,
      'ConsistentRead' =>  consistent_read
    }
  )
  attributes = {}
  REXML::XPath.each(doc, "//Attribute") do |attr|
    unesc_key = REXML::XPath.first(attr, './Name/text()').to_s
    unesc_value = REXML::XPath.first(attr, './Value/text()').to_s
    #unescape key and value to return to normal
    key = CGI.unescape(unesc_key)
    value = CGI.unescape(unesc_value)
    ( attributes[key] ||= [] ) << value
  end
  attributes
end

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



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/aws_sdb/service.rb', line 33

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



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/aws_sdb/service.rb', line 103

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

#select(query, max = nil, token = nil, consistent_read = 'true') ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/aws_sdb/service.rb', line 61

def select(query, max = nil, token = nil, consistent_read = 'true')
  query 
  params = {
    'Action' => 'Select',
    'SelectExpression' => query,
    'ConsistentRead' => consistent_read
  }
  params['NextToken'] =
    token unless token.nil? || token.empty?
  params['MaxNumberOfItems'] =
    max.to_s unless max.nil? || max.to_i == 0

  @logger.debug { "SELECT EXPRESSION BEFORE CALL: #{query.inspect}" } if @logger.debug?
  doc = call(:get, params)

  items = {}
  attributes = {}

  #build the result hash
  REXML::XPath.each(doc, '//Item') do |item_doc|
    item_name = item_doc.elements["Name"].text

    item_doc.each do |attrib_doc|
      att_name = attrib_doc.elements["Name"]
      att_value = attrib_doc.elements["Value"]
      
      next unless (att_name && att_value)
      
      attrib_name = CGI.unescape(att_name.text)
      attrib_value = CGI.unescape(att_value.text)

      add_value(attributes, attrib_name, attrib_value)
    end
    items[item_name] = attributes
    #reset attributes so we don't clobber next item
    attributes = {}
    items
  end
  results = items
  return results, REXML::XPath.first(doc, '//NextToken/text()').to_s
end