Class: Usps::Support::Models::DynamoDb

Inherits:
Object
  • Object
show all
Defined in:
lib/usps/support/models/dynamo_db.rb

Overview

DynamoDB API

Constant Summary collapse

REGION =
'us-east-1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table) ⇒ DynamoDb

Returns a new instance of DynamoDb.



11
12
13
# File 'lib/usps/support/models/dynamo_db.rb', line 11

def initialize(table)
  @table = table
end

Instance Attribute Details

#tableObject

Returns the value of attribute table.



9
10
11
# File 'lib/usps/support/models/dynamo_db.rb', line 9

def table
  @table
end

Instance Method Details

#query(options) ⇒ Object

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/usps/support/models/dynamo_db.rb', line 30

def query(options)
  raise ArgumentError, 'Required: {key}: "value"' unless options.keys.size == 1

  key = options.keys.first
  value = options[key]

  client.query(
    table_name: table,
    key_condition_expression: "#{key} = :key",
    expression_attribute_values: { ':key' => value },
    scan_index_forward: true
  ).items
end

#scanObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/usps/support/models/dynamo_db.rb', line 15

def scan
  [].tap do |results|
    last_evaluated_key = nil
    loop do
      response = client.scan(table_name: table, exclusive_start_key: last_evaluated_key)
      results.concat(response.items)
      break unless (last_evaluated_key = response.last_evaluated_key)
    end

    results.sort_by! do |result|
      sort_keys.map { result[it] }
    end
  end
end

#sort_keysObject



48
49
50
# File 'lib/usps/support/models/dynamo_db.rb', line 48

def sort_keys
  .key_schema.select { it.key_type == 'RANGE' }.map(&:attribute_name)
end

#table_metadataObject



44
45
46
# File 'lib/usps/support/models/dynamo_db.rb', line 44

def 
  client.describe_table(table_name: table).table
end