Class: Polar::Cursor

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

Constant Summary collapse

ITEMS_PER_PAGE =
500

Instance Method Summary collapse

Constructor Details

#initialize(api_key, secret_key, session_key, domain_klass, params) ⇒ Cursor

Returns a new instance of Cursor.



6
7
8
9
10
# File 'lib/polar/cursor.rb', line 6

def initialize(api_key, secret_key, session_key, domain_klass, params)
  @api_key, @secret_key, @session_key, @domain_klass, @params = api_key, secret_key, session_key, domain_klass, params
  @current_page = 0
  @fetched_current_page = false
end

Instance Method Details

#[](index) ⇒ Object



35
36
37
38
# File 'lib/polar/cursor.rb', line 35

def [](index)
  fetch_current_page if !@fetched_current_page
  @items[index]
end

#each(&block) ⇒ Object

Each will return the first page of results



13
14
15
16
17
18
# File 'lib/polar/cursor.rb', line 13

def each(&block)
  @current_page += 1
  fetch_current_page if !@fetched_current_page
  @items.each { |i| block.call i }
  @fetched_current_page = false
end

#next_page?Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
# File 'lib/polar/cursor.rb', line 20

def next_page?
  if @items
    return @items.count >= ITEMS_PER_PAGE
  elsif !@items && @current_page == 0
    return true
  else
    raise("Items are nil and they should not be.")
  end
end

#total_itemsObject



30
31
32
33
# File 'lib/polar/cursor.rb', line 30

def total_items
  fetch_current_page if @total_items.nil?
  @total_items
end