Class: LemonSqueezy::Object

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

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Object

Returns a new instance of Object.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/lemon_squeezy/object.rb', line 5

def initialize(attributes)
  # Remove relationships and links from responses as they are not required
  attributes.delete "relationships"
  attributes.delete "links"

  attrs = {}

  if attributes["attributes"]
    # Add ID from response to attributes
    attrs[:id] = attributes["id"] if attributes["id"]
    attrs.merge!(attributes["attributes"])
  else
    attrs.merge!(attributes)
  end

  super to_ostruct(attrs)
end

Instance Method Details

#to_ostruct(obj) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/lemon_squeezy/object.rb', line 23

def to_ostruct(obj)
  if obj.is_a?(Hash)
    OpenStruct.new(obj.map { |key, val| [key, 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