Class: GooglePlus::Cursor

Inherits:
Object
  • Object
show all
Extended by:
Resource
Defined in:
lib/google_plus/cursor.rb

Overview

A class for easily paginating through lists of Google Plus results. Automatically handles details like nextPageToken

Constant Summary

Constants included from Resource

Resource::BASE_URI

Instance Method Summary collapse

Methods included from Resource

make_request

Constructor Details

#initialize(klass, method, resource, params = {}) ⇒ Cursor

Create a new cursor

Parameters:

  • klass (Class)
    • The class type to instantiate members of this cursor as

  • method (Symbol)
    • The HTTP method if this request

  • resource (String)
    • The path of this request relative to the API

  • params (Hash) (defaults to: {})
    • a set of parameters to be merged into the request



44
45
46
47
48
49
50
# File 'lib/google_plus/cursor.rb', line 44

def initialize(klass, method, resource, params = {})
  @first_page_loaded = false
  @resource_klass = klass
  @method = method
  @resource = resource
  @base_params = params
end

Instance Method Details

#each {|an| ... } ⇒ Object

Go through each item

Yield Parameters:

Yield Returns:



12
13
14
15
16
17
18
19
20
# File 'lib/google_plus/cursor.rb', line 12

def each
  while items = next_page
    break if items.empty?
    items.each do |item|
      yield item
    end
  end
  self
end

#items(params = {}) ⇒ Array

Get the current page of results

Returns:

  • (Array)

    the current page of results, or nil if the page is blank



24
25
26
27
28
29
30
31
# File 'lib/google_plus/cursor.rb', line 24

def items(params = {})
  if instance_variable_defined?(:@items)
    # TODO raise error if params are passed here, since they're meaningless
    @items
  else
    @items = load_page(params)
  end
end

#next_page(params = {}) ⇒ Array

Load the next page of results and load it as the current page

Returns:

  • (Array)

    the next page of results, or nil if the page is blank



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

def next_page(params = {})
  @items = load_page(params)
end