Class: FreeAgent::Object

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/free_agent/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
# File 'lib/free_agent/object.rb', line 5

def initialize(attributes)
  super to_ostruct(attributes)

  # The FreeAgent API doesn't send an ID so generate it from the URL 
  if attributes["url"]
    number = attributes["url"].match(/\d{2,}/)
    self.id = number[0] unless number.nil?
  end
end

Instance Method Details

#to_ostruct(obj) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/free_agent/object.rb', line 15

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