Class: Hypertube::Core::Handler::RetrieveArrayHandler

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractHandler

#handle_command, #validate

Constructor Details

#initializeRetrieveArrayHandler

Returns a new instance of RetrieveArrayHandler.



11
12
13
# File 'lib/hypertube-ruby-sdk/core/handler/retrieve_array_handler.rb', line 11

def initialize
  @required_parameters_count = 1
end

Class Method Details

.normalize_array_like(response) ⇒ Object

Converts Array, Set, and other array-like values to a plain Array payload.



29
30
31
32
33
34
35
36
37
38
# File 'lib/hypertube-ruby-sdk/core/handler/retrieve_array_handler.rb', line 29

def self.normalize_array_like(response)
  case response
  when Array
    response
  else
    if defined?(Set) && response.is_a?(Set)
      response.to_a
    end
  end
end

Instance Method Details

#process(command) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/hypertube-ruby-sdk/core/handler/retrieve_array_handler.rb', line 15

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

  response = command.payload[0]
  normalized = self.class.normalize_array_like(response)

  if normalized
    Hypertube::Utils::Command.new(Hypertube::Utils::RuntimeNameHt::RUBY, Hypertube::Utils::CommandType::ARRAY, normalized)
  else
    raise ArgumentError, 'Invalid argument type for Hypertube::Core::Handler::RetrieveArrayHandler. Expected an array.'
  end
end