Class: 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.



8
9
10
# File 'lib/hypertube-ruby-sdk/core/handler/retrieve_array_handler.rb', line 8

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.



26
27
28
29
30
31
32
33
34
35
# File 'lib/hypertube-ruby-sdk/core/handler/retrieve_array_handler.rb', line 26

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



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

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

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

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