Class: TempoIQ::Cursor
- Inherits:
-
Object
- Object
- TempoIQ::Cursor
- 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
- PAGE_LINK =
"next_page"- NEXT_QUERY =
"next_query"
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#remoter ⇒ Object
readonly
Returns the value of attribute remoter.
-
#route ⇒ Object
readonly
Returns the value of attribute route.
-
#segment_key ⇒ Object
readonly
Returns the value of attribute segment_key.
Instance Method Summary collapse
- #each ⇒ Object
-
#initialize(klass, remoter, route, query, headers = {}, segment_key = "data") ⇒ Cursor
constructor
A new instance of Cursor.
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
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
16 17 18 |
# File 'lib/tempoiq/models/cursor.rb', line 16 def headers @headers end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
16 17 18 |
# File 'lib/tempoiq/models/cursor.rb', line 16 def query @query end |
#remoter ⇒ Object (readonly)
Returns the value of attribute remoter.
16 17 18 |
# File 'lib/tempoiq/models/cursor.rb', line 16 def remoter @remoter end |
#route ⇒ Object (readonly)
Returns the value of attribute route.
16 17 18 |
# File 'lib/tempoiq/models/cursor.rb', line 16 def route @route end |
#segment_key ⇒ Object (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
#each ⇒ Object
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 |