Class: SynapsePay::APIResource

Inherits:
Object
  • Object
show all
Defined in:
lib/synapse_pay/apibits/api_resource.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json = nil, api_method = nil, client = nil) ⇒ APIResource

Returns a new instance of APIResource.



7
8
9
# File 'lib/synapse_pay/apibits/api_resource.rb', line 7

def initialize(json=nil, api_method=nil, client=nil)
  refresh_from(json, api_method, client)
end

Instance Attribute Details

#api_methodObject (readonly)

Returns the value of attribute api_method.



3
4
5
# File 'lib/synapse_pay/apibits/api_resource.rb', line 3

def api_method
  @api_method
end

#clientObject (readonly)

Returns the value of attribute client.



5
6
7
# File 'lib/synapse_pay/apibits/api_resource.rb', line 5

def client
  @client
end

#jsonObject (readonly)

Returns the value of attribute json.



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

def json
  @json
end

Class Method Details

.api_attribute_namesObject



46
47
48
# File 'lib/synapse_pay/apibits/api_resource.rb', line 46

def self.api_attribute_names
  @api_attributes.map(&:first)
end

.api_subclass_fetch(name) ⇒ Object



108
109
110
111
112
113
# File 'lib/synapse_pay/apibits/api_resource.rb', line 108

def self.api_subclass_fetch(name)
  @api_subclasses_hash ||= {}
  if @api_subclasses_hash.has_key?(name)
    @api_subclasses_hash[name]
  end
end

.api_subclassesObject



104
105
106
# File 'lib/synapse_pay/apibits/api_resource.rb', line 104

def self.api_subclasses
  return @api_subclasses ||= Set.new
end

.determine_api_attribute_value(name, raw_value) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/synapse_pay/apibits/api_resource.rb', line 87

def self.determine_api_attribute_value(name, raw_value)
  if @api_attributes[name] && @api_attributes[name].has_key?(:constructor)
    klass = Util.constantize(@api_attributes[name][:constructor])
    if(klass.respond_to?(:construct))
      klass.construct(raw_value)
    else
      klass.new(raw_value)
    end
  else
    APIObject.construct(raw_value)
  end
end

.register_api_subclass(subclass, name = nil) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/synapse_pay/apibits/api_resource.rb', line 115

def self.register_api_subclass(subclass, name=nil)
  @api_subclasses ||= Set.new
  @api_subclasses << subclass

  unless name.nil?
    @api_subclasses_hash ||= {}
    @api_subclasses_hash[name] = subclass
  end
end

Instance Method Details

#api_attributesObject



50
51
52
53
54
55
56
# File 'lib/synapse_pay/apibits/api_resource.rb', line 50

def api_attributes
  ret = {}
  self.class.api_attribute_names.each do |attribute|
    ret[attribute] = self.send(attribute)
  end
  ret
end

#changed_api_attributesObject

TODO(joncalhoun): Make this work for nested class construction.



71
72
73
74
75
76
77
78
79
# File 'lib/synapse_pay/apibits/api_resource.rb', line 71

def changed_api_attributes
  ret = {}
  self.api_attributes.each do |name, value|
    if @json[name] != value
      ret[name] = value
    end
  end
  ret
end

#clear_api_attributesObject



81
82
83
84
85
# File 'lib/synapse_pay/apibits/api_resource.rb', line 81

def clear_api_attributes
  self.class.api_attribute_names.each do |name|
    instance_variable_set("@#{name}", nil)
  end
end

#determine_api_attribute_value(name, raw_value) ⇒ Object



99
100
101
# File 'lib/synapse_pay/apibits/api_resource.rb', line 99

def determine_api_attribute_value(name, raw_value)
  self.class.determine_api_attribute_value(name, raw_value)
end

#inspectObject



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

def inspect
  id_string = (self.respond_to?(:id) && !self.id.nil?) ? " id=#{self.id}" : ""
  "#<#{self.class}:0x#{self.object_id.to_s(16)}#{id_string}> Attributes: " + JSON.pretty_generate(inspect_api_attributes)
end

#inspect_api_attributesObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/synapse_pay/apibits/api_resource.rb', line 58

def inspect_api_attributes
  ret = {}
  api_attributes.each do |k, v|
    if v.is_a?(APIResource)
      ret[k] = v.inspect_nested
    else
      ret[k] = v
    end
  end
  ret
end

#inspect_nestedObject



37
38
39
40
# File 'lib/synapse_pay/apibits/api_resource.rb', line 37

def inspect_nested
  id_string = (self.respond_to?(:id) && !self.id.nil?) ? " id=#{self.id}" : ""
  "#<#{self.class}:0x#{self.object_id.to_s(16)}#{id_string}>"
end

#refresh_from(json = {}, api_method = nil, client = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/synapse_pay/apibits/api_resource.rb', line 11

def refresh_from(json={}, api_method=nil, client=nil)
  unless json.is_a?(Hash)
    json = { :id => json }
  end
  json = Util.symbolize_keys(json)

  # Clear or write over any old data
  clear_api_attributes
  @api_method = api_method
  @json = Util.sorta_deep_clone(json)
  @client = client

  # Use json (not the @json, the cloned copy)
  json.each do |k, v|
    if self.class.api_attribute_names.include?(k.to_sym)
      instance_variable_set("@#{k}", determine_api_attribute_value(k, v))
    end
  end
  self
end

#to_json(*args) ⇒ Object



42
43
44
# File 'lib/synapse_pay/apibits/api_resource.rb', line 42

def to_json(*args)
  JSON.generate(api_attributes)
end