Class: Ksql::Handlers::TypedList

Inherits:
Object
  • Object
show all
Defined in:
lib/ksql/handlers/typed_list.rb

Class Method Summary collapse

Class Method Details

.handle(response) ⇒ Ksql::@type

Define and instanciate an OpenStruct to fit the typed request response

Parameters:

Returns:

  • (Ksql::@type)

    OpenStruct instance



13
14
15
16
17
18
19
20
21
# File 'lib/ksql/handlers/typed_list.rb', line 13

def self.handle(response)
  return Ksql::Error.new(response.body) if response.error?
  return response.body unless response.body.present?

  parsed_body = response.body.first
  row_type = parsed_body.delete('@type').camelize
  row_class = Ksql.const_defined?(row_type) ? "Ksql::#{row_type}".constantize : Ksql.const_set(row_type, Class.new(OpenStruct))
  row_class.new(parsed_body)
end