Class: Alula::ListObject

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/alula/list_object.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(list_type) ⇒ ListObject

Returns a new instance of ListObject.



10
11
12
13
14
# File 'lib/alula/list_object.rb', line 10

def initialize list_type
  @type = list_type
  @items = []
  @meta = Meta.new({})
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



7
8
9
# File 'lib/alula/list_object.rb', line 7

def items
  @items
end

#metaObject (readonly)

Returns the value of attribute meta.



7
8
9
# File 'lib/alula/list_object.rb', line 7

def meta
  @meta
end

#rate_limitObject

Returns the value of attribute rate_limit.



8
9
10
# File 'lib/alula/list_object.rb', line 8

def rate_limit
  @rate_limit
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/alula/list_object.rb', line 7

def type
  @type
end

Class Method Details

.construct_from_array(klass, collection) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/alula/list_object.rb', line 75

def self.construct_from_array(klass, collection)
  list = ListObject.new(klass)
  collection.each do |item|
    list << klass.new(item['id']).construct_from(item)
  end
  list
end

.construct_from_response(klass, response, _opts) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/alula/list_object.rb', line 64

def self.construct_from_response(klass, response, _opts)
  list = ListObject.new(klass)
  response.data['data'].each do |item|
    list << klass.new(item['id']).construct_from(item)
  end
  list.set_meta Meta.new(response.data['meta'])
  list.rate_limit = response.rate_limit

  list
end

Instance Method Details

#<<(item) ⇒ Object



36
37
38
# File 'lib/alula/list_object.rb', line 36

def <<(item)
  @items << item
end

#[](index) ⇒ Object



24
25
26
# File 'lib/alula/list_object.rb', line 24

def[] index
  @items[index]
end

#eachObject



20
21
22
# File 'lib/alula/list_object.rb', line 20

def each
  items.each { |i| yield i }
end

#firstObject



28
29
30
# File 'lib/alula/list_object.rb', line 28

def first
  @items.first
end

#lastObject



32
33
34
# File 'lib/alula/list_object.rb', line 32

def last
  @items.last
end

#lengthObject



16
17
18
# File 'lib/alula/list_object.rb', line 16

def length
  items.length
end

#merge!(list) ⇒ Object

Merge another list into this list Use only for lists of the same type and same pagination settings Should be used only when we are limited by URL length and need to fetch more than 20 items in the same page



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/alula/list_object.rb', line 51

def merge!(list)
  @items += list.items
  new_meta = Meta.new(
    'page' => {
      'total' => list.meta.page.total,
      'number' => list.meta.page.number,
      'size' => list.meta.page.size
    }
  )
  set_meta(new_meta)
  self
end

#paginationObject



44
45
46
# File 'lib/alula/list_object.rb', line 44

def pagination
  meta.page
end

#set_meta(meta) ⇒ Object



40
41
42
# File 'lib/alula/list_object.rb', line 40

def set_meta(meta)
  @meta = meta
end