Class: Paysio::Resource

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Dirty
Defined in:
lib/paysio/resource.rb

Direct Known Subclasses

Charge, Coupon, Customer, Event, List, Log, Payout, Reward, Wallet

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values = {}) ⇒ Resource

Returns a new instance of Resource.



8
9
10
11
12
13
# File 'lib/paysio/resource.rb', line 8

def initialize(values = {})
  @values = {}
  @id = values['id'] if values['id']
  refresh_from(values)
  self.class.define_attribute_methods(attributes.keys)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *opts) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/paysio/resource.rb', line 30

def method_missing(name, *opts)
  if name[-1] == '='
    name = name.to_s.gsub(/\=$/, '')
    send(:"#{name}_will_change!")
    @values[name.to_s] = opts.first if @values.has_key?(name.to_s)
  else
    @values[name.to_s] if @values.has_key?(name.to_s)
  end
end

Instance Attribute Details

#redirectObject

Returns the value of attribute redirect.



6
7
8
# File 'lib/paysio/resource.rb', line 6

def redirect
  @redirect
end

Class Method Details

.build_from(resp, redirect_url = nil) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/paysio/resource.rb', line 82

def build_from(resp, redirect_url = nil)
  case resp
  when Array
    resp.map { |values| build_from(values) }
  when Hash
    if @@registered_resources && @@registered_resources.include?(resp['object'])
      klass = "Paysio::#{resp['object'].classify}".constantize
    else
      klass = Paysio::Resource
    end
    resource = klass.new(resp)
    resource.redirect = redirect_url
    resource
  else
    resp
  end
end

.pathObject



78
79
80
# File 'lib/paysio/resource.rb', line 78

def path
  "/#{self.name.demodulize.tableize}"
end

.resource(resource_name) ⇒ Object



73
74
75
76
# File 'lib/paysio/resource.rb', line 73

def resource(resource_name)
  @@registered_resources ||= []
  @@registered_resources << resource_name.to_s
end

Instance Method Details

#action_path(action) ⇒ Object



68
69
70
# File 'lib/paysio/resource.rb', line 68

def action_path(action)
  "#{path}/#{action}"
end

#as_json(*opts) ⇒ Object



44
45
46
# File 'lib/paysio/resource.rb', line 44

def as_json(*opts)
  @values.as_json(*a)
end

#attribute(key) ⇒ Object



56
57
58
# File 'lib/paysio/resource.rb', line 56

def attribute(key)
  to_hash[key]
end

#attributesObject



52
53
54
# File 'lib/paysio/resource.rb', line 52

def attributes
  to_hash.reject{ |k, v| v.is_a? (Paysio::Resource)}
end

#each(&blk) ⇒ Object



60
61
62
# File 'lib/paysio/resource.rb', line 60

def each(&blk)
  @values.each(&blk)
end

#inspectObject



25
26
27
28
# File 'lib/paysio/resource.rb', line 25

def inspect
  id_string = !@id.nil? ? " id=#{@id}" : ""
  "#<#{self.class}:0x#{self.object_id.to_s(16)}#{id_string}> JSON: " + Paysio::JSON.encode(@values)
end

#pathObject



64
65
66
# File 'lib/paysio/resource.rb', line 64

def path
  "#{self.class.path}/#{@id}"
end

#refresh_from(values) ⇒ Object



15
16
17
18
19
# File 'lib/paysio/resource.rb', line 15

def refresh_from(values)
  values.each do |key, value|
    @values[key] = Resource.build_from(value)
  end
end

#to_hashObject



48
49
50
# File 'lib/paysio/resource.rb', line 48

def to_hash
  @values
end

#to_json(*opts) ⇒ Object



40
41
42
# File 'lib/paysio/resource.rb', line 40

def to_json(*opts)
  Paysio::JSON.encode(@values)
end

#to_s(*opts) ⇒ Object



21
22
23
# File 'lib/paysio/resource.rb', line 21

def to_s(*opts)
  Paysio::JSON.encode(@values)
end