Class: Fog::AWS::SimpleDB::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/aws/simpledb.rb,
lib/fog/aws/requests/simpledb/select.rb,
lib/fog/aws/requests/simpledb/list_domains.rb,
lib/fog/aws/requests/simpledb/create_domain.rb,
lib/fog/aws/requests/simpledb/delete_domain.rb,
lib/fog/aws/requests/simpledb/get_attributes.rb,
lib/fog/aws/requests/simpledb/put_attributes.rb,
lib/fog/aws/requests/simpledb/domain_metadata.rb,
lib/fog/aws/requests/simpledb/delete_attributes.rb,
lib/fog/aws/requests/simpledb/batch_put_attributes.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.


44
45
46
47
# File 'lib/fog/aws/simpledb.rb', line 44

def initialize(options={})
  @aws_access_key_id = options[:aws_access_key_id]
  @data = self.class.data[@aws_access_key_id]
end

Class Method Details

.dataObject


30
31
32
33
34
35
36
# File 'lib/fog/aws/simpledb.rb', line 30

def self.data
  @data ||= Hash.new do |hash, key|
    hash[key] = {
      :domains => {}
    }
  end
end

.reset_data(keys = data.keys) ⇒ Object


38
39
40
41
42
# File 'lib/fog/aws/simpledb.rb', line 38

def self.reset_data(keys=data.keys)
  for key in [*keys]
    data.delete(key)
  end
end

Instance Method Details

#batch_put_attributes(domain_name, items, replace_attributes = Hash.new([])) ⇒ Object


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/fog/aws/requests/simpledb/batch_put_attributes.rb', line 35

def batch_put_attributes(domain_name, items, replace_attributes = Hash.new([]))
  response = Excon::Response.new
  if @data[:domains][domain_name]
    for item_name, attributes in items do
      for key, value in attributes do
        @data[:domains][domain_name][item_name] ||= {}
        if replace_attributes[item_name] && replace_attributes[item_name].include?(key)
          @data[:domains][domain_name][item_name][key.to_s] = []
        else
          @data[:domains][domain_name][item_name][key.to_s] ||= []
        end
        @data[:domains][domain_name][item_name][key.to_s] << value.to_s
      end
    end
    response.status = 200
    response.body = {
      'BoxUsage'  => Fog::AWS::Mock.box_usage,
      'RequestId' => Fog::AWS::Mock.request_id
    }
  else
    response.status = 400
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end

#create_domain(domain_name) ⇒ Object


29
30
31
32
33
34
35
36
37
38
# File 'lib/fog/aws/requests/simpledb/create_domain.rb', line 29

def create_domain(domain_name)
  response = Excon::Response.new
  @data[:domains][domain_name] = {}
  response.status = 200
  response.body = {
    'BoxUsage'  => Fog::AWS::Mock.box_usage,
    'RequestId' => Fog::AWS::Mock.request_id
  }
  response
end

#delete_attributes(domain_name, item_name, attributes = nil) ⇒ Object


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fog/aws/requests/simpledb/delete_attributes.rb', line 38

def delete_attributes(domain_name, item_name, attributes = nil)
  response = Excon::Response.new
  if @data[:domains][domain_name]
    if attributes
      for key, value in attributes
        if @data[:domains][domain_name][key]
          @data[:domains][domain_name][key].delete('value')
        end
      end
    else
      @data[:domains].delete(domain_name)
    end
    response.status = 200
    response.body = {
      'BoxUsage'  => Fog::AWS::Mock.box_usage,
      'RequestId' => Fog::AWS::Mock.request_id
    }
  else
    response.status = 400
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end

#delete_domain(domain_name) ⇒ Object


29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fog/aws/requests/simpledb/delete_domain.rb', line 29

def delete_domain(domain_name)
  response = Excon::Response.new
  if @data[:domains].delete(domain_name)
    response.status = 200
    response.body = {
      'BoxUsage'  => Fog::AWS::Mock.box_usage,
      'RequestId' => Fog::AWS::Mock.request_id
    }
  end
  response
end

#domain_metadata(domain_name) ⇒ Object


38
39
40
41
42
43
44
45
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
# File 'lib/fog/aws/requests/simpledb/domain_metadata.rb', line 38

def (domain_name)
  response = Excon::Response.new
  if domain = @data[:domains][domain_name]
    response.status = 200
  
    attribute_names = []
    attribute_values = []
    for item in domain.values
      for key, values in item
        attribute_names << key
        for value in values
          attribute_values << value
        end
      end
    end
  
    response.body = {
      'AttributeNameCount'        => attribute_names.length,
      'AttributeNamesSizeBytes'   => attribute_names.join('').length,
      'AttributeValueCount'       => attribute_values.length,
      'AttributeValuesSizeBytes'  => attribute_values.join('').length,
      'BoxUsage'                  => Fog::AWS::Mock.box_usage,
      'ItemCount'                 => domain.keys.length,
      'ItemNamesSizeBytes'        => domain.keys.join('').length,
      'RequestId'                 => Fog::AWS::Mock.request_id,
      'Timestamp'                 => Time.now
    }
  else
    response.status = 400
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end

#get_attributes(domain_name, item_name, attributes = nil) ⇒ Object


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fog/aws/requests/simpledb/get_attributes.rb', line 42

def get_attributes(domain_name, item_name, attributes = nil)
  response = Excon::Response.new
  if @data[:domains][domain_name]
    object = {}
    if attributes
      for attribute in attributes
        if @data[:domains][domain_name][item_name] && @data[:domains][domain_name][item_name]
          object[attribute] = @data[:domains][domain_name][item_name][attribute]
        end
      end
    elsif @data[:domains][domain_name][item_name]
      object = @data[:domains][domain_name][item_name]
    end
    response.status = 200
    response.body = {
      'Attributes'  => object,
      'BoxUsage'    => Fog::AWS::Mock.box_usage,
      'RequestId'   => Fog::AWS::Mock.request_id
    }
  else
    response.status = 400
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end

#list_domains(options = {}) ⇒ Object


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fog/aws/requests/simpledb/list_domains.rb', line 34

def list_domains(options = {})
  response = Excon::Response.new
  keys = @data[:domains].keys
  max = options['MaxNumberOfDomains'] || keys.size
  offset = options['NextToken'] || 0
  domains = []
  for key, value in @data[:domains].keys[offset...max]
    domains << key
  end
  response.status = 200
  response.body = {
    'BoxUsage'  => Fog::AWS::Mock.box_usage,
    'Domains'   => domains,
    'RequestId' => Fog::AWS::Mock.request_id
  }
  if max < keys.size
    response.body['NextToken'] = max + 1
  end
  response
end

#put_attributes(domain_name, item_name, attributes, replace_attributes = []) ⇒ Object


36
37
38
39
40
41
42
# File 'lib/fog/aws/requests/simpledb/put_attributes.rb', line 36

def put_attributes(domain_name, item_name, attributes, replace_attributes = [])
  batch_put_attributes(
    domain_name,
    { item_name => attributes },
    { item_name => replace_attributes }
  )
end

#select(select_expression, next_token = nil) ⇒ Object

Raises:


35
36
37
# File 'lib/fog/aws/requests/simpledb/select.rb', line 35

def select(select_expression, next_token = nil)
  raise MockNotImplemented.new("Contributions welcome!")
end