Class: Notion::Api::Pagination::Cursor

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, verb, params = {}) ⇒ Cursor

Returns a new instance of Cursor.



15
16
17
18
19
20
21
22
# File 'lib/notion/pagination/cursor.rb', line 15

def initialize(client, verb, params = {})
  @client = client
  @verb = verb
  @params = params.dup
  @sleep_interval = @params.delete(:sleep_interval)
  @max_retries = @params.delete(:max_retries) || client.default_max_retries
  @retry_after = @params.delete(:retry_after) || client.default_retry_after
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



8
9
10
# File 'lib/notion/pagination/cursor.rb', line 8

def client
  @client
end

#max_retriesObject (readonly)

Returns the value of attribute max_retries.



11
12
13
# File 'lib/notion/pagination/cursor.rb', line 11

def max_retries
  @max_retries
end

#paramsObject (readonly)

Returns the value of attribute params.



13
14
15
# File 'lib/notion/pagination/cursor.rb', line 13

def params
  @params
end

#retry_afterObject (readonly)

Returns the value of attribute retry_after.



12
13
14
# File 'lib/notion/pagination/cursor.rb', line 12

def retry_after
  @retry_after
end

#sleep_intervalObject (readonly)

Returns the value of attribute sleep_interval.



10
11
12
# File 'lib/notion/pagination/cursor.rb', line 10

def sleep_interval
  @sleep_interval
end

#verbObject (readonly)

Returns the value of attribute verb.



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

def verb
  @verb
end

Instance Method Details

#eachObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/notion/pagination/cursor.rb', line 24

def each
  next_cursor = nil
  retry_count = 0
  loop do
    query = { page_size: client.default_page_size }.merge(params)
    query = query.merge(start_cursor: next_cursor) unless next_cursor.nil?
    begin
      response = client.send(verb, query)
    rescue Notion::Api::Errors::TooManyRequests => e
      raise e if retry_count >= max_retries

      client.logger.debug("#{self.class}##{__method__}") { e.to_s }
      retry_count += 1
      sleep(retry_after)
      next
    end
    yield response
    break unless response.has_more

    next_cursor = response.next_cursor
    break if next_cursor.nil? || next_cursor == ''

    retry_count = 0
    sleep(sleep_interval) if sleep_interval
  end
end