Class: Usps::Support::Models::DynamoDb
- Inherits:
-
Object
- Object
- Usps::Support::Models::DynamoDb
- Defined in:
- lib/usps/support/models/dynamo_db.rb
Overview
DynamoDB API
Constant Summary collapse
- REGION =
'us-east-1'
Instance Attribute Summary collapse
-
#table ⇒ Object
Returns the value of attribute table.
Instance Method Summary collapse
-
#initialize(table) ⇒ DynamoDb
constructor
A new instance of DynamoDb.
- #query(options) ⇒ Object
- #scan ⇒ Object
- #sort_keys ⇒ Object
- #table_metadata ⇒ Object
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
#table ⇒ Object
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
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() raise ArgumentError, 'Required: {key}: "value"' unless .keys.size == 1 key = .keys.first value = [key] client.query( table_name: table, key_condition_expression: "#{key} = :key", expression_attribute_values: { ':key' => value }, scan_index_forward: true ).items end |
#scan ⇒ Object
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_keys ⇒ Object
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_metadata ⇒ Object
44 45 46 |
# File 'lib/usps/support/models/dynamo_db.rb', line 44 def client.describe_table(table_name: table).table end |