Sha256: 2a096a82c2e205677d7c45df1f95ce8373b840fb323e24716d01c778afd19a19

Contents?: true

Size: 1.92 KB

Versions: 4

Compression:

Stored size: 1.92 KB

Contents

require "crimp"
require "alephant/lookup/lookup_location"
require "alephant/logger"

module Alephant
  module Lookup
    class LookupQuery
      include Logger
      attr_reader :table_name, :lookup_location

      def initialize(table_name, component_id, opts, batch_version)
        @client          = AWS::DynamoDB::Client::V20120810.new
        @table_name      = table_name
        @lookup_location = LookupLocation.new(component_id, opts, batch_version)

        logger.info(
          "event"        => "LookupQueryInitialized",
          "tableName"    => table_name,
          "componentId"  => component_id,
          "location"     => lookup_location,
          "batchVersion" => batch_version,
          "method"       => "#{self.class}#initialize"
        )
      end

      def run!
        lookup_location.tap do |l|
          l.location = s3_location_from(
            @client.query(to_q)
          ).tap do |loc|
            logger.info(
              "event"    => "S3LocationRetrieved",
              "location" => loc,
              "method"   => "#{self.class}#run!"
            )
          end
        end
      end

      private

      def s3_location_from(result)
        result[:count] == 1 ? result[:member].first["location"][:s] : nil
      end

      def to_q
        {
          :table_name => table_name,
          :consistent_read => true,
          :select => "SPECIFIC_ATTRIBUTES",
          :attributes_to_get => ["location"],
          :key_conditions => {
            "component_key" => {
              :comparison_operator => "EQ",
              :attribute_value_list => [
                { "s" => @lookup_location.component_key }
              ],
            },
            "batch_version" => {
              :comparison_operator => "EQ",
              :attribute_value_list => [
                { "n" => @lookup_location.batch_version.to_s }
              ]
            }
          }
        }
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
alephant-lookup-2.0.2 lib/alephant/lookup/lookup_query.rb
alephant-lookup-2.0.1 lib/alephant/lookup/lookup_query.rb
alephant-lookup-2.0.0 lib/alephant/lookup/lookup_query.rb
alephant-lookup-1.0.0 lib/alephant/lookup/lookup_query.rb