Class: Openf1::Object

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/openf1/object.rb

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Object

Returns a new instance of Object.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
# File 'lib/openf1/object.rb', line 7

def initialize(attributes)
  raise ArgumentError, "Response cannot be nil" if attributes.nil?

  # Response is always an array so nest under data:
  super(data: to_ostruct(attributes))
end

Instance Method Details

#to_ostruct(obj) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/openf1/object.rb', line 14

def to_ostruct(obj)
  if obj.is_a?(Hash)
    OpenStruct.new(obj.transform_values { |val| to_ostruct(val) })
  elsif obj.is_a?(Array)
    obj.map { |o| to_ostruct(o) }
  else
    obj
  end
end