Class: Slack::Web::Api::Pagination::Cursor

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Cursor.



14
15
16
17
18
19
20
# File 'lib/slack/web/pagination/cursor.rb', line 14

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
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#max_retriesObject (readonly)

Returns the value of attribute max_retries.



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

def max_retries
  @max_retries
end

#paramsObject (readonly)

Returns the value of attribute params.



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

def params
  @params
end

#sleep_intervalObject (readonly)

Returns the value of attribute sleep_interval.



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

def sleep_interval
  @sleep_interval
end

#verbObject (readonly)

Returns the value of attribute verb.



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

def verb
  @verb
end

Instance Method Details

#eachObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/slack/web/pagination/cursor.rb', line 22

def each
  next_cursor = nil
  retry_count = 0
  loop do
    query = { limit: client.default_page_size }.merge(params).merge(cursor: next_cursor)
    begin
      response = client.send(verb, query)
    rescue Slack::Web::Api::Errors::TooManyRequestsError => e
      raise e if retry_count >= max_retries
      client.logger.debug("#{self.class}##{__method__}") { e.to_s }
      retry_count += 1
      sleep(e.retry_after.seconds)
      next
    end
    yield response
    break unless response.
    next_cursor = response..next_cursor
    break if next_cursor.blank?
    retry_count = 0
    sleep(sleep_interval) if sleep_interval
  end
end