Class: Mintsoft::Base

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/mintsoft/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Base

Returns a new instance of Base.



11
12
13
14
# File 'lib/mintsoft/base.rb', line 11

def initialize(attributes)
  @original_response = deep_freeze_object(attributes)
  super(to_ostruct(attributes))
end

Instance Attribute Details

#original_responseObject (readonly)

Returns the value of attribute original_response.



9
10
11
# File 'lib/mintsoft/base.rb', line 9

def original_response
  @original_response
end

Instance Method Details

#rawObject

Get the raw original response data



32
33
34
# File 'lib/mintsoft/base.rb', line 32

def raw
  @original_response
end

#to_hashObject

Convert back to hash representation



27
28
29
# File 'lib/mintsoft/base.rb', line 27

def to_hash
  ostruct_to_hash(self)
end

#to_ostruct(obj) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/mintsoft/base.rb', line 16

def to_ostruct(obj)
  if obj.is_a?(Hash)
    OpenStruct.new(obj.transform_keys { |key| key.to_s.underscore }.transform_values { |val| to_ostruct(val) })
  elsif obj.is_a?(Array)
    obj.map { |o| to_ostruct(o) }
  else # Assumed to be a primitive value
    obj
  end
end