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

Inherits:
Object
  • Object
show all
Includes:
Collections
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.



36
37
38
39
# File 'lib/fog/aws/simpledb.rb', line 36

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



22
23
24
25
26
27
28
# File 'lib/fog/aws/simpledb.rb', line 22

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

.reset_data(keys = data.keys) ⇒ Object



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

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, options = {}) ⇒ 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
67
68
69
70
71
72
73
# File 'lib/fog/aws/requests/simpledb/put_attributes.rb', line 42

def put_attributes(domain_name, item_name, attributes, options = {})
  options[:expect] = {} unless options[:expect]
  options[:replace] = [] unless options[:replace]
  response = Excon::Response.new
  if @data[:domains][domain_name]
    options[:expect].each do |ck, cv|
      if @data[:domains][domain_name][item_name][ck] != [cv]
        response.status = 409
        raise(Excon::Errors.status_error({:expects => 200}, response))
      end
    end
    attributes.each do |key, value|
      @data[:domains][domain_name][item_name] ||= {}
      @data[:domains][domain_name][item_name][key.to_s] = [] unless @data[:domains][domain_name][item_name][key.to_s]
      if options[:replace].include?(key.to_s)
        @data[:domains][domain_name][item_name][key.to_s] = [value.to_s]
      else
        @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

#select(select_expression, next_token = nil) ⇒ Object



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

def select(select_expression, next_token = nil)
  Fog::Mock.not_implemented
end