Class: Smartcoin::SmartcoinObject

Inherits:
Object
  • Object
show all
Defined in:
lib/smartcoin/smartcoin_object.rb

Direct Known Subclasses

Card, Charge, Customer, Fee, Installment, Plan, Refund, Subscription, Token

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSmartcoinObject

Returns a new instance of SmartcoinObject.



63
64
65
# File 'lib/smartcoin/smartcoin_object.rb', line 63

def initialize()
  @values = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/smartcoin/smartcoin_object.rb', line 67

def method_missing(name, *args)
  if name.to_s.end_with? '='
    name = name.to_s[0...-1].to_sym
  end

  if serialize_params.include? name
    @values[name] = args[0]
  else
    raise NoMemoryError.new ("Cannot set #{name} on this object")
  end
end

Class Method Details

.class_nameObject



55
56
57
# File 'lib/smartcoin/smartcoin_object.rb', line 55

def self.class_name
  self.name.split('::')[-1]
end

.create_from(params) ⇒ Object



8
9
10
11
# File 'lib/smartcoin/smartcoin_object.rb', line 8

def self.create_from(params)
  obj = self.new
  obj.reflesh_object(params)
end

.urlObject



47
48
49
# File 'lib/smartcoin/smartcoin_object.rb', line 47

def self.url
  "/v1/#{CGI.escape(class_name.downcase)}s"
end

Instance Method Details

#create_fields(params) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/smartcoin/smartcoin_object.rb', line 19

def create_fields(params)
  instance_eval do
    metaclass.instance_eval do
      params.keys.each do |key|
        define_method(key) { @values[key] }

        define_method("#{key}=") do |value|
          @values[key] = value
        end
      end
    end
  end
end

#metaclassObject



4
5
6
# File 'lib/smartcoin/smartcoin_object.rb', line 4

def metaclass
  class << self; self; end
end

#reflesh_object(params) ⇒ Object



13
14
15
16
17
# File 'lib/smartcoin/smartcoin_object.rb', line 13

def reflesh_object(params)
  create_fields(params)
  set_properties(params)
  self
end

#serialize_paramsObject



59
60
61
# File 'lib/smartcoin/smartcoin_object.rb', line 59

def serialize_params
  [:card]
end

#set_properties(params) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/smartcoin/smartcoin_object.rb', line 33

def set_properties(params)
  instance_eval do
    params.each do |key, value|
      if value.class == Hash
        @values[key] = Util.get_object_type(value['object']).create_from(value)
      elsif value.class == Array
        @values[key] = value.map{ |v| Util.get_object_type(v['object']).create_from(v) }
      else
        @values[key] = value
      end
    end
  end
end

#to_hashObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/smartcoin/smartcoin_object.rb', line 79

def to_hash
  @values.inject({}) do |result, (key,value)|
    if value.is_a? Array
      list = []
      value.each do |obj|
        list.push(obj.respond_to?(:to_hash) ? obj.to_hash : value)
      end
      result[key] = list
    else if value.respond_to?(:to_hash)
        result[key] = value.to_hash
      else
        result[key] =  value
      end
    end
    result
  end
end

#to_json(*a) ⇒ Object



97
98
99
# File 'lib/smartcoin/smartcoin_object.rb', line 97

def to_json(*a)
  JSON.generate(@values)
end

#to_sObject



101
102
103
# File 'lib/smartcoin/smartcoin_object.rb', line 101

def to_s
  JSON.generate(@values)
end

#urlObject



51
52
53
# File 'lib/smartcoin/smartcoin_object.rb', line 51

def url
  "#{self.class.url}/#{self.id}"
end