Class: Mollie::List

Inherits:
Base
  • Object
show all
Includes:
Enumerable
Defined in:
lib/mollie/list.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#attributes

Instance Method Summary collapse

Methods inherited from Base

all, #assign_attributes, cancel, create, #delete, delete, get, id_param, parent_id, request, resource_name, update, #update

Constructor Details

#initialize(list_attributes, klass) ⇒ List

Returns a new instance of List.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/mollie/list.rb', line 8

def initialize(list_attributes, klass)
  @klass = klass

  list_attributes['items'] ||= if list_attributes['_embedded']
                                 list_attributes['_embedded'].fetch(klass.embedded_resource_name, [])
                               else
                                 []
                               end
  super list_attributes

  @items = items.map do |attributes|
    klass.new attributes
  end
end

Instance Attribute Details

Returns the value of attribute _links.



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

def _links
  @_links
end

#itemsObject

Returns the value of attribute items.



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

def items
  @items
end

#klassObject

Returns the value of attribute klass.



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

def klass
  @klass
end

Instance Method Details

#[](index) ⇒ Object



23
24
25
# File 'lib/mollie/list.rb', line 23

def [](index)
  @items[index]
end

#each(&block) ⇒ Object



31
32
33
# File 'lib/mollie/list.rb', line 31

def each(&block)
  @items.each(&block)
end

#next(options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/mollie/list.rb', line 35

def next(options = {})
  return self.class.new({}, klass) if links['next'].nil?

  href = URI.parse(links['next']['href'])
  query = URI.decode_www_form(href.query).to_h

  response = Mollie::Client.instance.perform_http_call('GET', links['next']['href'], nil, {}, options.merge(query))
  self.class.new(response, klass)
end

#previous(options = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/mollie/list.rb', line 45

def previous(options = {})
  return self.class.new({}, klass) if links['previous'].nil?

  href = URI.parse(links['previous']['href'])
  query = URI.decode_www_form(href.query).to_h

  response = Mollie::Client.instance.perform_http_call('GET', links['previous']['href'], nil, {}, options.merge(query))
  self.class.new(response, klass)
end

#sizeObject



27
28
29
# File 'lib/mollie/list.rb', line 27

def size
  @items.size
end