Class: SynapsePay::APIObject

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/synapse_pay/apibits/api_object.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json = nil) ⇒ APIObject

Returns a new instance of APIObject.



16
17
18
# File 'lib/synapse_pay/apibits/api_object.rb', line 16

def initialize(json=nil)
  refresh_from(json)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &blk) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/synapse_pay/apibits/api_object.rb', line 36

def method_missing(name, *args, &blk)
  if name.to_s.end_with?('=')
    attr = name.to_s[0...-1].to_sym
    @json[attr] = args[0]
  else
    if @json.respond_to?(name)
      @json.send(name, *args, &blk)
    elsif @json.has_key?(name.to_sym)
      return @json[name.to_sym]
    else
      super
    end
  end
end

Instance Attribute Details

#jsonObject (readonly)

Returns the value of attribute json.



4
5
6
# File 'lib/synapse_pay/apibits/api_object.rb', line 4

def json
  @json
end

Class Method Details

.construct(json) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/synapse_pay/apibits/api_object.rb', line 6

def self.construct(json)
  if json.is_a?(Array)
    return json.map{ |a| APIObject.construct(a) }
  elsif json.is_a?(Hash)
    return APIObject.new(json)
  else
    return json
  end
end

Instance Method Details

#inspectObject



28
29
30
# File 'lib/synapse_pay/apibits/api_object.rb', line 28

def inspect
  @json.inspect
end

#refresh_from(json = {}) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/synapse_pay/apibits/api_object.rb', line 20

def refresh_from(json={})
  @json = Util.sorta_deep_clone(json)
  @json.each do |k, v|
    @json[k] = APIObject.construct(v)
  end
  self
end

#to_json(*args) ⇒ Object



32
33
34
# File 'lib/synapse_pay/apibits/api_object.rb', line 32

def to_json(*args)
  JSON.generate(@json)
end