Class: SimplyDB::Interface

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Interface

Returns a new instance of Interface.



6
7
8
9
# File 'lib/simplydb/interface.rb', line 6

def initialize(options = {})
  @options = {}.merge(options)
  @client = SimplyDB::Client.new(options)
end

Instance Attribute Details

#box_usageObject

Returns the value of attribute box_usage.



5
6
7
# File 'lib/simplydb/interface.rb', line 5

def box_usage
  @box_usage
end

#clientObject

Returns the value of attribute client.



5
6
7
# File 'lib/simplydb/interface.rb', line 5

def client
  @client
end

#next_tokenObject

Returns the value of attribute next_token.



5
6
7
# File 'lib/simplydb/interface.rb', line 5

def next_token
  @next_token
end

#on_errorObject

Returns the value of attribute on_error.



5
6
7
# File 'lib/simplydb/interface.rb', line 5

def on_error
  @on_error
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/simplydb/interface.rb', line 5

def options
  @options
end

#request_idObject

Returns the value of attribute request_id.



5
6
7
# File 'lib/simplydb/interface.rb', line 5

def request_id
  @request_id
end

Instance Method Details

#batch_put_attributes(name, items = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/simplydb/interface.rb', line 50

def batch_put_attributes(name, items = {})
  params = {'DomainName' => name, 'ActionName' => 'BatchPutAttributes'}
  items.keys.each_with_index do |key, i|
    params["Item.#{i}.ItemName"] = key
    items[key].inject(0) do |j, (name,value)|
      value = [value] unless value.is_a?(Array)
      value.each do |v|
        params["Item.#{i}.Attribute.#{j}.Name"] = name
        params["Item.#{i}.Attribute.#{j}.Value"] = value
        j+=1
      end
      j
    end
  end
  call(params) do |doc|
    doc.css("BatchPutAttributesResponse").length > 0
  end
end

#create_domain(name) ⇒ Object



11
12
13
14
15
# File 'lib/simplydb/interface.rb', line 11

def create_domain(name)
  call({'Action' => 'CreateDomain', 'DomainName' => name}) do |doc|
    doc.css("CreateDomainResponse").length > 0
  end
end

#delete_attributes(name, id, attributes = {}, expected = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/simplydb/interface.rb', line 69

def delete_attributes(name, id, attributes = {}, expected = {})
  params = define_attributes(attributes, expected)
  params.merge!({
    'DomainName' => name,
    'ItemName' => id,
    'Action' => 'DeleteAttributes'
  })
  call(params) do |doc|
    doc.css("DeleteAttributesResponse").length > 0
  end
end

#delete_domain(name) ⇒ Object



17
18
19
20
21
# File 'lib/simplydb/interface.rb', line 17

def delete_domain(name)
  call({'Action' => 'DeleteDomain', 'DomainName' => name}) do |doc|
    doc.css("DeleteDomainResponse").length > 0
  end
end

#domain_metadata(name) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/simplydb/interface.rb', line 29

def (name)
  call({'Action' => 'DomainMetadata','DomainName' => name}) do |doc|
    doc.css("DomainMetadataResult").first.children.inject({}) do |memo, child|
      memo[child.name] = child.text
      memo
    end
  end
end

#get_attributes(name, id, wanted_attributes = [], consistent_read = false) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/simplydb/interface.rb', line 81

def get_attributes(name, id, wanted_attributes = [], consistent_read = false)
  params = {
    'Action' => 'GetAttributes',
    'DomainName' => name,
    'ItemName' => id,
    'ConsistentRead' => consistent_read.to_s,
  }
  wanted_attributes.each_with_index {|name, index| params["AttributeName.#{index}"] = name}

  call(params) do |doc|
    doc.css("Attribute").inject({}) do |memo, attribute|
      name = attribute.css("Name").first.text
      value = attribute.css("Value").first.text
      if memo.has_key?(name)
        memo[name] = [memo[name]] unless memo[name].is_a?(Array)
        memo[name] << value
      else
        memo[name] = value
      end
      memo
    end
  end
end

#list_domainsObject



23
24
25
26
27
# File 'lib/simplydb/interface.rb', line 23

def list_domains
  call({'Action' => 'ListDomains'}) do |doc|
    doc.css('DomainName').collect{|d| d.text}
  end
end

#put_attributes(name, id, attributes = {}, expected = {}, replace = false) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/simplydb/interface.rb', line 38

def put_attributes(name, id, attributes = {}, expected = {}, replace = false)
  params = define_attributes(attributes, expected, replace)
  params.merge!({
    'DomainName' => name,
    'ItemName' => id,
    'Action' => 'PutAttributes'
  })
  call(params) do |doc|
    doc.css("PutAttributesResponse").length > 0
  end
end

#select(expression, consistent_read = false, next_token = nil) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/simplydb/interface.rb', line 105

def select(expression, consistent_read = false, next_token = nil)
  params = {
    'Action' => 'Select',
    'SelectExpression' => expression,
    'ConsistentRead' => consistent_read.to_s,
  }
  params['NextToken'] = next_token unless next_token.nil?
  call(params) do |doc|
    doc.css("SelectResponse SelectResult Item").collect do |element|
      item_name = element.css("Name").first.text
      item = element.css("Attribute").inject({}) do |attributes, attribute|
        attribute_name = attribute.css("Name").first.text
        attribute_value = attribute.css("Value").first.text
        if attributes.has_key?(attribute_name)
          attributes[attribute_name] = [attributes[attribute_name]] unless attributes[attribute_name].is_a?(Array)
          attributes[attribute_name] << attribute_value
        else
          attributes[attribute_name] = attribute_value
        end
        attributes
      end
      {item_name => item}
    end
  end
end