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



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

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



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

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

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

Raises:



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

def get(id, options = {})
  raise Mollie::Exception, "Invalid resource ID: #{id.inspect}" if id.nil? || id.strip.empty?

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

.id_paramObject



57
58
59
# File 'lib/mollie/base.rb', line 57

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

.parent_idObject



61
62
63
# File 'lib/mollie/base.rb', line 61

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

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

Yields:

  • (response)


51
52
53
54
55
# File 'lib/mollie/base.rb', line 51

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 Also known as: embedded_resource_name



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

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



40
41
42
43
44
# File 'lib/mollie/base.rb', line 40

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



90
91
92
93
94
95
# File 'lib/mollie/base.rb', line 90

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



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/mollie/base.rb', line 78

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