Class: NzPostApi::Objects::Base

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Base

Returns a new instance of Base.



12
13
14
15
# File 'lib/nz_post_api/objects/base.rb', line 12

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

Instance Attribute Details

#original_response ⇒ Object (readonly)

Returns the value of attribute original_response.



10
11
12
# File 'lib/nz_post_api/objects/base.rb', line 10

def original_response
  @original_response
end

Instance Method Details

#raw ⇒ Object

Get the raw original response data



33
34
35
# File 'lib/nz_post_api/objects/base.rb', line 33

def raw
  @original_response
end

#to_hash ⇒ Object

Convert back to hash representation



28
29
30
# File 'lib/nz_post_api/objects/base.rb', line 28

def to_hash
  ostruct_to_hash(self)
end

#to_ostruct(obj) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/nz_post_api/objects/base.rb', line 17

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