Class: AnPostReturn::Base

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

Direct Known Subclasses

ReturnLabel

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/an_post_return/objects/base.rb', line 12

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

Instance Attribute Details

#original_responseObject (readonly)

Returns the value of attribute original_response.



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

def original_response
  @original_response
end

Instance Method Details

#==(other) ⇒ Object

Override comparison to handle hash comparison



38
39
40
41
42
43
44
45
# File 'lib/an_post_return/objects/base.rb', line 38

def ==(other)
  case other
  when Hash
    to_hash == other
  else
    super
  end
end

#eql?(other) ⇒ Boolean

Override eql? to be consistent with ==

Returns:

  • (Boolean)


48
49
50
# File 'lib/an_post_return/objects/base.rb', line 48

def eql?(other)
  self == other
end

#responseObject

Return the original response with camelCase keys preserved



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

def response
  @original_response
end

#to_hashObject

Convert back to hash without table key, including nested structures



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

def to_hash
  ostruct_to_hash(self)
end

#to_ostruct(obj) ⇒ Object



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

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