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.



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

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.



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

def client
  @client
end

#max_retriesObject (readonly)

Returns the value of attribute max_retries.



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

def max_retries
  @max_retries
end

#paramsObject (readonly)

Returns the value of attribute params.



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

def params
  @params
end

#sleep_intervalObject (readonly)

Returns the value of attribute sleep_interval.



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

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



19
20
21
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 19

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)
      next
    end
    yield response
    break unless response.

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

    retry_count = 0
    sleep(sleep_interval) if sleep_interval
  end
end