Class: Patreon::Object

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

Direct Known Subclasses

Address, Benefit, Campaign, Goal, Identity, Member, Tier, User

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Object

Returns a new instance of Object.



5
6
7
# File 'lib/patreon/object.rb', line 5

def initialize(attributes)
  super to_ostruct(attributes)
end

Instance Method Details

#to_ostruct(obj) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/patreon/object.rb', line 9

def to_ostruct(obj)
  if obj.is_a?(Hash)
    if obj["data"]
      # For when data is sent to the object, for instance if included is also added
      if obj["data"]["attributes"]
        OpenStruct.new(obj["data"]["attributes"].map { |key, val| [key, to_ostruct(val)] }.to_h)
      end
    elsif obj["attributes"]
      # Merge the attributes 
      OpenStruct.new(obj["attributes"].map { |key, val| [key, to_ostruct(val)] }.to_h)
    else
      OpenStruct.new(obj.map { |key, val| [key, to_ostruct(val)] }.to_h)
    end
  elsif obj.is_a?(Array)
    obj.map { |o| to_ostruct(o) }
  else # Assumed to be a primitive value
    obj
  end
end