Sha256: 85ed6cffeb2b446300cf9efe0dd6450758715bc0915109379ed62b72938eb869

Contents?: true

Size: 1.09 KB

Versions: 3

Compression:

Stored size: 1.09 KB

Contents

require 'aws-sdk'
require 'alephant/lookup/lookup_query'
require 'alephant/logger'

module Alephant
  module Lookup
    class LocationWrite
      include ::Alephant::Logger

      attr_reader :table_name
      attr_reader :lookups

      def initialize(lookup_table)
        @table_name = lookup_table.table_name
        @batch = AWS::DynamoDB::BatchWrite.new
        @lookups = []
        @processed = false
      end

      def <<(lookup)
        raise TypeError unless lookup.is_a? LookupQuery

        @lookups << lookup
      end

      def processed?
        @processed
      end

      def process!
        logger.info("LocationWrite#process! #{processed? ? "is" : "not"} running batch put on #{table_name}")
        processed? ? false : process_batch_put
      end

      private

      def process_batch_put
        logger.info("LocationWrite#process_batch_put to #{table_name} for #{@lookups.map { |lookup| lookup.to_h }}")

        @batch.put(
          table_name,
          @lookups.map { |lookup| lookup.to_h }
        )
        @batch.process!

        processed = true
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
alephant-lookup-0.1.1 lib/alephant/lookup/location_write.rb
alephant-lookup-0.1.0 lib/alephant/lookup/location_write.rb
alephant-lookup-0.0.4 lib/alephant/lookup/location_write.rb