Class: MultiSafePay::List

Inherits:
Base
  • Object
show all
Includes:
Enumerable
Defined in:
lib/multisafepay/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.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/multisafepay/list.rb', line 7

def initialize(list_attributes, klass)
  @klass = klass
  @pager = list_attributes['pager'] || {}

  list_attributes['items'] ||= list_attributes['data'] || []
  super list_attributes

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

Instance Attribute Details

#itemsObject

Returns the value of attribute items.



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

def items
  @items
end

#klassObject

Returns the value of attribute klass.



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

def klass
  @klass
end

#pagerObject

Returns the value of attribute pager.



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

def pager
  @pager
end

Instance Method Details

#[](index) ⇒ Object



19
20
21
# File 'lib/multisafepay/list.rb', line 19

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

#each(&block) ⇒ Object



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

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

#next(options = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/multisafepay/list.rb', line 39

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

  href = URI.parse(pager['after'])
  query = URI.decode_www_form(href.query).to_h

  response = MultiSafePay::Client.instance.perform_http_call('GET', pager['after'], nil, {}, options.merge(query))
  self.class.new(response, klass)
end

#next?Boolean

Returns:

  • (Boolean)


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

def next?
  !pager['after'].nil?
end

#previous(options = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/multisafepay/list.rb', line 49

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

  href = URI.parse(pager['before'])
  query = URI.decode_www_form(href.query).to_h

  response = MultiSafePay::Client.instance.perform_http_call('GET', pager['before'], nil, {}, options.merge(query))
  self.class.new(response, klass)
end

#previous?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/multisafepay/list.rb', line 35

def previous?
  !pager['before'].nil?
end

#sizeObject



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

def size
  @items.size
end