class Clarion::Counters::Dynamodb

Public Class Methods

new(table_name:, region:) click to toggle source
# File lib/clarion/counters/dynamodb.rb, line 7
def initialize(table_name:, region:)
  @table_name = table_name
  @region = region
end

Public Instance Methods

dynamodb() click to toggle source
# File lib/clarion/counters/dynamodb.rb, line 39
def dynamodb
  @dynamodb ||= Aws::DynamoDB::Resource.new(region: @region)
end
get(key) click to toggle source
# File lib/clarion/counters/dynamodb.rb, line 12
def get(key)
  item = table.query(
    limit: 1,
    select: 'ALL_ATTRIBUTES',
    key_condition_expression: 'handle = :handle',
    expression_attribute_values: {":handle" => key.handle},
  ).items.first

  item && item['key_counter']
end
store(key) click to toggle source
# File lib/clarion/counters/dynamodb.rb, line 23
def store(key)
  table.update_item(
    key: {
      'handle' => key.handle,
    },
    update_expression: 'SET key_counter = :new',
    condition_expression: 'attribute_not_exists(key_counter) OR key_counter < :new',
    expression_attribute_values: {':new' => key.counter},
  )
rescue Aws::DynamoDB::Errors::ConditionalCheckFailedException
end
table() click to toggle source
# File lib/clarion/counters/dynamodb.rb, line 35
def table
  @table ||= dynamodb.table(@table_name)
end