Class: Delighted::ListResource

Inherits:
Object
  • Object
show all
Defined in:
lib/delighted/list_resource.rb

Instance Method Summary collapse

Constructor Details

#initialize(klass, path, opts, client) ⇒ ListResource

Returns a new instance of ListResource.



3
4
5
6
7
8
9
10
# File 'lib/delighted/list_resource.rb', line 3

def initialize(klass, path, opts, client)
  @class = klass
  @path = path
  @opts = opts
  @client = client
  @iteration_count = 0
  @done = false
end

Instance Method Details

#auto_paging_each(opts = {}) ⇒ Object

Raises:



12
13
14
15
16
17
18
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/delighted/list_resource.rb', line 12

def auto_paging_each(opts = {})
  raise PaginationError, "pagination completed" if @done

  auto_handle_rate_limits = opts.fetch(:auto_handle_rate_limits, false)
  loop do
    begin
      # Get next (or first) page
      if @iteration_count == 0
        data = @client.request_get(@path, { params: @opts })
      else
        data = @client.request_get(@next_link)
      end
    rescue Delighted::RateLimitedError => e
      if auto_handle_rate_limits
        sleep e.retry_after
        retry
      else
        raise
      end
    end

    @iteration_count += 1
    @next_link = data[:response].next_link

    data[:json].map do |attributes|
      yield @class.new(attributes)
    end

    break if @next_link.nil?
  end
  @done = true
end