Class: Veritable::Cursor
- Inherits:
-
Object
- Object
- Veritable::Cursor
- Includes:
- Enumerable, VeritableResource
- Defined in:
- lib/veritable/cursor.rb
Overview
Generic Cursor class for collections of API resources
Cursors may be initialized with ‘limit’, ‘start’, and ‘per_page’ options.
Users should call the #each and #next methods for access to the underlying resources.
Instance Method Summary collapse
-
#each ⇒ Object
Implements the Enumerable interface.
-
#initialize(opts = nil, doc = nil, &lazymap) ⇒ Cursor
constructor
Initializes a new Veritable::Cursor from an API collection.
-
#inspect ⇒ Object
String representation of the Cursor.
-
#to_s ⇒ Object
String representation of the Cursor.
Methods included from Connection
#delete, #get, #post, #put, #request
Constructor Details
#initialize(opts = nil, doc = nil, &lazymap) ⇒ Cursor
Initializes a new Veritable::Cursor from an API collection
Optionally pass a block in for postprocessing of resources.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/veritable/cursor.rb', line 18 def initialize(opts=nil, doc=nil, &lazymap) super(opts, doc) require_opts 'collection' default_opts({'per_page' => 100, 'extra_args' => {}}) collection_key = collection.split("/")[-1] @doc = get(collection, params={:count => per_page, :start => start}.update(@opts['extra_args'])) @doc.has_key?(collection_key) ? @opts['key'] = collection_key : @opts['key'] = 'data' @opts['lazymap'] = lazymap if lazymap end |
Instance Method Details
#each ⇒ Object
Implements the Enumerable interface
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/veritable/cursor.rb', line 31 def each i = limit if limit loop do if data.length > 0 or refresh > 0 if limit raise StopIteration if i == 0 i = i - 1 end if lazymap yield lazymap.call(data.shift) else yield data.shift end else raise StopIteration end end end |
#inspect ⇒ Object
String representation of the Cursor
51 |
# File 'lib/veritable/cursor.rb', line 51 def inspect; to_s; end |
#to_s ⇒ Object
String representation of the Cursor
54 |
# File 'lib/veritable/cursor.rb', line 54 def to_s; "#<Veritable::Cursor collection='" + collection + "'>"; end |