Class: Heirloom::AWS::SimpleDB

Inherits:
Object
  • Object
show all
Defined in:
lib/heirloom/aws/simpledb.rb

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ SimpleDB

Returns a new instance of SimpleDB.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/heirloom/aws/simpledb.rb', line 7

def initialize(args)
  @config = args[:config]

  fog_args = { :region => @config. }

  if @config.use_iam_profile
    fog_args[:use_iam_profile] = true
  else
    fog_args[:aws_access_key_id]     = @config.access_key
    fog_args[:aws_secret_access_key] = @config.secret_key
  end

  @sdb = Fog::AWS::SimpleDB.new fog_args
end

Instance Method Details

#count(domain) ⇒ Object



77
78
79
80
# File 'lib/heirloom/aws/simpledb.rb', line 77

def count(domain)
  body = @sdb.select("SELECT count(*) FROM `#{domain}`").body
  body['Items']['Domain']['Count'].first.to_i
end

#create_domain(domain) ⇒ Object



30
31
32
# File 'lib/heirloom/aws/simpledb.rb', line 30

def create_domain(domain)
  @sdb.create_domain(domain) unless domain_exists?(domain)
end

#delete(domain, key) ⇒ Object



73
74
75
# File 'lib/heirloom/aws/simpledb.rb', line 73

def delete(domain, key)
  @sdb.delete_attributes domain, key
end

#delete_domain(domain) ⇒ Object



34
35
36
# File 'lib/heirloom/aws/simpledb.rb', line 34

def delete_domain(domain)
  @sdb.delete_domain(domain)
end

#domain_empty?(domain) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/heirloom/aws/simpledb.rb', line 38

def domain_empty?(domain)
  count(domain).zero?
end

#domain_exists?(domain) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/heirloom/aws/simpledb.rb', line 26

def domain_exists?(domain)
  domains.include? domain
end

#domainsObject



22
23
24
# File 'lib/heirloom/aws/simpledb.rb', line 22

def domains
  @sdb.list_domains.body['Domains']
end

#item_count(domain, item) ⇒ Object



82
83
84
85
# File 'lib/heirloom/aws/simpledb.rb', line 82

def item_count(domain, item)
  query = "SELECT count(*) FROM `#{domain}` WHERE itemName() = '#{item}'"
  @sdb.select(query).body['Items']['Domain']['Count'].first.to_i
end

#put_attributes(domain, key, attributes, options = {}) ⇒ Object



42
43
44
# File 'lib/heirloom/aws/simpledb.rb', line 42

def put_attributes(domain, key, attributes, options = {})
  @sdb.put_attributes domain, key, attributes, options
end

#select(query, opts = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/heirloom/aws/simpledb.rb', line 46

def select(query, opts = {})
  has_more = true
  next_token = nil
  results = {}

  if opts[:offset] && opts[:offset] > 0
    limit = @sdb.select("#{query} limit #{opts[:offset]}").body
    if limit['NextToken']
      next_token = limit['NextToken']
    else
      has_more = false
    end
  end
  while has_more
    more = @sdb.select(query, 'NextToken' => next_token).body
    more['Items'].each do |k, v|
      block_given? ? yield(k, v) : results[k] = v
    end
    if more['NextToken']
      next_token = more['NextToken']
    else
      has_more = false
    end
  end
  results unless block_given?
end