Class: TempoIQ::Cursor

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

Overview

Cursor is an abstraction over a sequence / stream of objects. It uses lazy iteration to transparently fetch segments of data from the server.

It implements the Enumerable interface, which means convenience functions such as Enumerable#to_a are available if you know you’re working with a small enough segment of data that can reasonably fit in memory.

Constant Summary collapse

"next_page"
NEXT_QUERY =
"next_query"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, remoter, route, query, headers = {}, segment_key = "data") ⇒ Cursor

Returns a new instance of Cursor.



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

def initialize(klass, remoter, route, query, headers = {}, segment_key = "data")
  @klass = klass
  @remoter = remoter
  @route = route
  @query = query
  @headers = headers
  @segment_key = segment_key
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



16
17
18
# File 'lib/tempoiq/models/cursor.rb', line 16

def headers
  @headers
end

#queryObject (readonly)

Returns the value of attribute query.



16
17
18
# File 'lib/tempoiq/models/cursor.rb', line 16

def query
  @query
end

#remoterObject (readonly)

Returns the value of attribute remoter.



16
17
18
# File 'lib/tempoiq/models/cursor.rb', line 16

def remoter
  @remoter
end

#routeObject (readonly)

Returns the value of attribute route.



16
17
18
# File 'lib/tempoiq/models/cursor.rb', line 16

def route
  @route
end

#segment_keyObject (readonly)

Returns the value of attribute segment_key.



16
17
18
# File 'lib/tempoiq/models/cursor.rb', line 16

def segment_key
  @segment_key
end

Instance Method Details

#eachObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/tempoiq/models/cursor.rb', line 29

def each
  segment = nil
  until segment == nil && query == nil do
    json = get_segment(JSON.dump(query.to_hash))
    segment = json[segment_key]
    segment.each { |item| yield @klass.from_hash(item) }
    segment = nil
    @query = json.fetch(PAGE_LINK, {})[NEXT_QUERY]
  end
end