Class: Opener::Daemons::Mapper

Inherits:
Oni::Mapper
  • Object
show all
Defined in:
lib/opener/daemons/mapper.rb

Overview

Maps the input/output between the daemon and the worker in such a format that both ends can work with it easily.

Constant Summary collapse

SCHEMA_DIRECTORY =

The directory containing JSON schema files.

Returns:

  • (String)
File.expand_path('../../../../schema', __FILE__)
INPUT_SCHEMA =

Path to the schema file.

Returns:

  • (String)
File.join SCHEMA_DIRECTORY, 'sqs_input.json'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(component, component_options = {}) ⇒ Mapper

Returns a new instance of Mapper.

Parameters:

  • component (Class)
  • component_options (Hash) (defaults to: {})


34
35
36
37
# File 'lib/opener/daemons/mapper.rb', line 34

def initialize(component, component_options = {})
  @component         = component
  @component_options = component_options
end

Instance Attribute Details

#componentClass (readonly)

Returns:

  • (Class)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/opener/daemons/mapper.rb', line 13

class Mapper < Oni::Mapper
  attr_reader :component, :component_options

  ##
  # The directory containing JSON schema files.
  #
  # @return [String]
  #
  SCHEMA_DIRECTORY = File.expand_path('../../../../schema', __FILE__)

  ##
  # Path to the schema file.
  #
  # @return [String]
  #
  INPUT_SCHEMA = File.join SCHEMA_DIRECTORY, 'sqs_input.json'

  ##
  # @param [Class] component
  # @param [Hash] component_options
  #
  def initialize(component, component_options = {})
    @component         = component
    @component_options = component_options
  end

  ##
  # @param [AWS::SQS::ReceivedMessage] message
  # @return [Hash]
  #
  def map_input(message)
    decoded = JSON(message.body)

    validate_input!(decoded)

    return Configuration.new(component, component_options, decoded)
  end

  ##
  # Validates the given input Hash.
  #
  # @param [Hash] input
  # @raise [JSON::Schema::ValidationError]
  #
  def validate_input!(input)
    JSON::Validator.validate!(INPUT_SCHEMA, input)
  end
end

#component_optionsHash (readonly)

Returns:

  • (Hash)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/opener/daemons/mapper.rb', line 13

class Mapper < Oni::Mapper
  attr_reader :component, :component_options

  ##
  # The directory containing JSON schema files.
  #
  # @return [String]
  #
  SCHEMA_DIRECTORY = File.expand_path('../../../../schema', __FILE__)

  ##
  # Path to the schema file.
  #
  # @return [String]
  #
  INPUT_SCHEMA = File.join SCHEMA_DIRECTORY, 'sqs_input.json'

  ##
  # @param [Class] component
  # @param [Hash] component_options
  #
  def initialize(component, component_options = {})
    @component         = component
    @component_options = component_options
  end

  ##
  # @param [AWS::SQS::ReceivedMessage] message
  # @return [Hash]
  #
  def map_input(message)
    decoded = JSON(message.body)

    validate_input!(decoded)

    return Configuration.new(component, component_options, decoded)
  end

  ##
  # Validates the given input Hash.
  #
  # @param [Hash] input
  # @raise [JSON::Schema::ValidationError]
  #
  def validate_input!(input)
    JSON::Validator.validate!(INPUT_SCHEMA, input)
  end
end

Instance Method Details

#map_input(message) ⇒ Hash

Parameters:

  • message (AWS::SQS::ReceivedMessage)

Returns:

  • (Hash)


43
44
45
46
47
48
49
# File 'lib/opener/daemons/mapper.rb', line 43

def map_input(message)
  decoded = JSON(message.body)

  validate_input!(decoded)

  return Configuration.new(component, component_options, decoded)
end

#validate_input!(input) ⇒ Object

Validates the given input Hash.

Parameters:

  • input (Hash)

Raises:

  • (JSON::Schema::ValidationError)


57
58
59
# File 'lib/opener/daemons/mapper.rb', line 57

def validate_input!(input)
  JSON::Validator.validate!(INPUT_SCHEMA, input)
end