Class: Mollie::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/mollie/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Base

Returns a new instance of Base.



5
6
7
8
# File 'lib/mollie/base.rb', line 5

def initialize(attributes)
  @attributes = attributes
  assign_attributes(attributes)
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



3
4
5
# File 'lib/mollie/base.rb', line 3

def attributes
  @attributes
end

Class Method Details

.all(options = {}) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/mollie/base.rb', line 23

def all(options = {})
  id      = nil
  data    = {}

  request('GET', id, data, options) do |response|
    Mollie::List.new(response, self)
  end
end

.cancelObject



47
48
49
# File 'lib/mollie/base.rb', line 47

def delete(id, options = {})
  request('DELETE', id, options)
end

.create(data = {}, options = {}) ⇒ Object



17
18
19
20
21
# File 'lib/mollie/base.rb', line 17

def create(data = {}, options = {})
  request('POST', nil, data, options) do |response|
    new(response)
  end
end

.delete(id, options = {}) ⇒ Object



44
45
46
# File 'lib/mollie/base.rb', line 44

def delete(id, options = {})
  request('DELETE', id, options)
end

.get(id, options = {}) ⇒ Object



32
33
34
35
36
# File 'lib/mollie/base.rb', line 32

def get(id, options = {})
  request('GET', id, {}, options) do |response|
    new(response)
  end
end

.id_paramObject



55
56
57
# File 'lib/mollie/base.rb', line 55

def id_param
  "#{name.downcase.split('::')[-1]}_id".to_sym
end

.parent_idObject



59
60
61
# File 'lib/mollie/base.rb', line 59

def parent_id
  "#{name.downcase.split('::')[-2]}_id".to_sym
end

.request(method, id = 0, data = {}, options = {}) {|response| ... } ⇒ Object

Yields:

  • (response)


49
50
51
52
53
# File 'lib/mollie/base.rb', line 49

def request(method, id = 0, data = {}, options = {})
  parent_id = options.delete(self.parent_id) || data.delete(self.parent_id)
  response  = Mollie::Client.instance.perform_http_call(method, resource_name(parent_id), id, data, options)
  yield(response) if block_given?
end

.resource_name(parent_id = nil) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/mollie/base.rb', line 63

def resource_name(parent_id = nil)
  path = name.downcase.split('::').slice(1..-1).map(&Util.method(:pluralize))

  if path.size == 2 && parent_id
    path.join("/#{parent_id}/")
  else
    path.last
  end
end

.update(id, data = {}) ⇒ Object



38
39
40
41
42
# File 'lib/mollie/base.rb', line 38

def update(id, data = {})
  request('PATCH', id, data) do |response|
    new(response)
  end
end

Instance Method Details

#assign_attributes(attributes) ⇒ Object



10
11
12
13
14
# File 'lib/mollie/base.rb', line 10

def assign_attributes(attributes)
  attributes.each do |key, value|
    public_send("#{key}=", value) if respond_to?("#{key}=")
  end
end

#delete(options = {}) ⇒ Object Also known as: cancel



86
87
88
89
90
91
# File 'lib/mollie/base.rb', line 86

def delete(options = {})
  if (parent_id = attributes[self.class.parent_id])
    options[self.class.parent_id] = parent_id
  end
  self.class.delete(id, options)
end

#update(data = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/mollie/base.rb', line 74

def update(data = {})
  if (parent_id = attributes[self.class.parent_id])
    data[self.class.parent_id] = parent_id
  end

  if (resource = self.class.update(id, data))
    !!assign_attributes(resource.attributes)
  else
    resource
  end
end