Class: Seatsio::Pagination::Cursor

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/seatsio/pagination/cursor.rb

Overview

Enumerable for every Domain

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cls, endpoint, http_client, params = {}) ⇒ Cursor

Returns a new instance of Cursor.



11
12
13
14
15
16
17
18
19
20
# File 'lib/seatsio/pagination/cursor.rb', line 11

def initialize(cls, endpoint, http_client, params = {})
  @cls = cls
  @endpoint = endpoint
  @http_client = http_client
  @params = params
  @collection = []
  @next_page_starts_after = nil
  @previous_page_ends_before = nil
  @first_page = false
end

Instance Attribute Details

#next_page_starts_afterObject (readonly)

Returns the value of attribute next_page_starts_after.



9
10
11
# File 'lib/seatsio/pagination/cursor.rb', line 9

def next_page_starts_after
  @next_page_starts_after
end

#paramsObject (readonly)

Returns the value of attribute params.



9
10
11
# File 'lib/seatsio/pagination/cursor.rb', line 9

def params
  @params
end

#previous_page_ends_beforeObject (readonly)

Returns the value of attribute previous_page_ends_before.



9
10
11
# File 'lib/seatsio/pagination/cursor.rb', line 9

def previous_page_ends_before
  @previous_page_ends_before
end

Instance Method Details

#each(start = 0, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/seatsio/pagination/cursor.rb', line 22

def each(start = 0, &block)
  return to_enum(:each, start) unless block_given?

  Array(@collection[start..-1]).each do |element|
    yield(element)
  end
  return unless keep_running?
  return if last?

  start = [@collection.size, start].max
  fetch_next_page
  each(start, &block)
end

#first_page(limit = nil) ⇒ Object



40
41
42
43
44
# File 'lib/seatsio/pagination/cursor.rb', line 40

def first_page(limit = nil)
  @first_page = true
  set_query_param(:limit, limit) unless limit.nil?
  self
end

#page_after(after_id = nil, limit = nil) ⇒ Object



46
47
48
49
50
# File 'lib/seatsio/pagination/cursor.rb', line 46

def page_after(after_id = nil, limit = nil)
  set_query_param(:start_after_id, after_id) unless after_id.nil?
  set_query_param(:limit, limit) unless limit.nil?
  self
end

#page_before(before_id = nil, limit = nil) ⇒ Object



52
53
54
55
56
# File 'lib/seatsio/pagination/cursor.rb', line 52

def page_before(before_id = nil, limit = nil)
  set_query_param(:end_before_id, before_id) unless before_id.nil?
  set_query_param(:limit, limit) unless limit.nil?
  self
end

#set_query_param(key, value) ⇒ Object



36
37
38
# File 'lib/seatsio/pagination/cursor.rb', line 36

def set_query_param(key, value)
  @params[key] = value
end