Class: DefraRubyGovpay::Object

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

Overview

Generic Object. Takes the hash/Json returned from the Govpay API and gives us openstruct objects for easier reading/consuming

Direct Known Subclasses

Payment, Refund, Error

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Object

Returns a new instance of Object.



9
10
11
# File 'lib/defra_ruby_govpay/object.rb', line 9

def initialize(attributes)
  super to_ostruct(attributes)
end

Instance Method Details

#nil_value?(value) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
# File 'lib/defra_ruby_govpay/object.rb', line 35

def nil_value?(value)
  value.nil? ||
    %w[nil null].any?(
      value
    .to_s
    .strip
    .downcase
    )
end

#string_reader(value) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/defra_ruby_govpay/object.rb', line 26

def string_reader(value)
  return if nil_value?(value)

  value = value.to_s
  value = yield value if block_given?

  value
end

#to_ostruct(obj) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/defra_ruby_govpay/object.rb', line 13

def to_ostruct(obj)
  case obj
  when Hash
    OpenStruct.new(obj.transform_values { |v| to_ostruct(v) })
  when Array
    obj.map { |o| to_ostruct(o) }
  when String
    string_reader(obj)&.strip
  else # Likely a primative value
    obj
  end
end