Sha256: cb988dd7b17fa300146fd4865cb527142eb9b8b44cf2226a3e27da5e0830af10

Contents?: true

Size: 1.1 KB

Versions: 6

Compression:

Stored size: 1.1 KB

Contents

require 'aws-sdk-dynamodb'
require 'clarion/counters/base'

module Clarion
  module Counters
    class Dynamodb < Base
      def initialize(table_name:, region:)
        @table_name = table_name
        @region = region
      end

      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

      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

      def table
        @table ||= dynamodb.table(@table_name)
      end

      def dynamodb
        @dynamodb ||= Aws::DynamoDB::Resource.new(region: @region)
      end

    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
clarion-1.1.0 lib/clarion/counters/dynamodb.rb
clarion-1.0.0 lib/clarion/counters/dynamodb.rb
clarion-0.3.0 lib/clarion/counters/dynamodb.rb
clarion-0.2.1 lib/clarion/counters/dynamodb.rb
clarion-0.2.0 lib/clarion/counters/dynamodb.rb
clarion-0.1.0 lib/clarion/counters/dynamodb.rb