AWS services or capabilities described in AWS Documentation may vary by region/location. Click Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

Class: Aws::DynamoDBStreams::AttributeTranslator

Inherits:
Object
  • Object
show all
Defined in:
gems/aws-sdk-dynamodbstreams/lib/aws-sdk-dynamodbstreams/attribute_translator.rb

Overview

A utility class that translates DynamoDBStream events from Lambda functions into their simple attribute equivalents.

Class Method Summary collapse

Class Method Details

.from_event(event) ⇒ Object

Parse a DynamoDBStream event hash from Lambda that contains 1 or more records. When using the SDK to retrieve DynamoDB stream records, use the simple_attributes: true client option instead.

Examples:

Parse a DynamoDB stream event from Lambda

def lambda_handler(event:, context:)
  records = Aws::DynamoDBStreams::AttributeTranslator.from_event(event)
  records.each do |record|
    puts record.dynamodb.new_image
    # => { "size" => 123, "enabled" => true, ... }
  end
end

Parameters:

  • event (Hash)

    A DynamoDBStream event.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'gems/aws-sdk-dynamodbstreams/lib/aws-sdk-dynamodbstreams/attribute_translator.rb', line 20

def self.from_event(event)
  return unless event.is_a?(Hash)

  # TODO: This implementation is slow and inefficient. It would be
  # better to write a switch-case that has similar logic to AttributeValue
  # used in the ValueTranslator. This implementation however provides
  # consistency between both DynamoDB and DynamoDBStreams. The translator
  # only works with shapes and structs and not a regular Ruby hash.
  shape_ref = ClientApi::Shapes::ShapeRef.new(
    shape: ClientApi::GetRecordsOutput
  )
  translator = Plugins::SimpleAttributes::ValueTranslator.new(
    shape_ref, :unmarshal
  )
  parser = Aws::Json::Parser.new(shape_ref)
  input = parser.parse(Aws::Json.dump(event))
  translator.apply(input).records
end