Class: Hypertube::Core::Handler::ContextMetadataHandler

Inherits:
AbstractHandler show all
Defined in:
lib/hypertube-ruby-sdk/core/handler/context_metadata_handler.rb

Instance Method Summary collapse

Methods inherited from AbstractHandler

#handle_command, #validate

Constructor Details

#initializeContextMetadataHandler

Returns a new instance of ContextMetadataHandler.



12
13
14
15
16
# File 'lib/hypertube-ruby-sdk/core/handler/context_metadata_handler.rb', line 12

def initialize
  @required_parameters_count = 1
  @get_type_handler = GetTypeHandler.new
  @array_handler = ArrayHandler.new
end

Instance Method Details

#process(command) ⇒ Object



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
# File 'lib/hypertube-ruby-sdk/core/handler/context_metadata_handler.rb', line 18

def process(command)
  validate(command, @required_parameters_count, self.class.name)

  get_type_command = Utils::Command.new(Utils::RuntimeNameHt::RUBY, Utils::CommandType::GET_TYPE, "Graftcode::Context::RequestContext")
  context_type = @get_type_handler.process(get_type_command)
  current_context = context_type.current

  raise "Context metadata payload must contain an array command." if command.payload.nil? || command.payload.empty?

  array_command = command.payload[0]
  if array_command.is_a?(Utils::Command) && array_command.command_type == Utils::CommandType::ARRAY
    values = @array_handler.process(array_command)

    if values.length % 2 != 0
      raise "Context metadata payload must contain key/value pairs."
    end

    headers = {}
    (0...values.length).step(2) do |i|
      key = values[i]
      if key.nil? || key.to_s.strip.empty?
        raise "Context metadata key cannot be null or empty."
      end

      headers[key] = values[i + 1]
    end

    current_context.set_headers(headers)
  else
    raise "Context metadata payload must be an array."
  end

  0
end