Class: Cursor

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/onelogin/api/cursor.rb

Overview

Cursor

Used for paginating requests to the OneLogin API

Returns an enumerable object

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) ⇒ Cursor

Create a new instance of the Cursor.

Parameters:

  • url (String)

    The url of the API endpoint

  • options (Hash) (defaults to: {})

    Configuation options



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/onelogin/api/cursor.rb', line 14

def initialize(url, options = {})
  @url = url
  @options = options

  @model = options[:model]
  @headers = options[:headers] || {}
  @params = options[:params] || {}
  @max_results = options[:max_results]

  @collection = []
  @after_cursor = options.fetch(:after_cursor, nil)
end

Instance Method Details

#each(start = 0) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/onelogin/api/cursor.rb', line 27

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

  Array(@collection[start..-1]).each do |item|
    if @model
      yield(@model.new(item))
    else
      yield(item)
    end
  end

  unless last?
    start = [@collection.size, start].max

    fetch_next_page

    each(start, &Proc.new)
  end
end