Skip to content
This repository has been archived by the owner on Mar 9, 2019. It is now read-only.

Commit

Permalink
Added GooglePlus::Cursor#each
Browse files Browse the repository at this point in the history
  • Loading branch information
seejohnrun committed Jan 4, 2012
1 parent da8ffa8 commit b0e784e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
18 changes: 8 additions & 10 deletions README.md
Expand Up @@ -56,12 +56,12 @@ And once you have an activity, you can move back to its person using `#person`.

### People do Things

Lastly, you can get a list of activities for a person, which is returned as a `GooglePlus::Cursor`. You use it like:
Lastly, you can get a list of activities for a person, which is returned as a `GooglePlus::Cursor`. You use it like (which auto-paginates):

person = GooglePlus::Person.new(123)
cursor = person.list_activities
while cursor.next_page
cursor.items.count # a batch of activities
cursor.each do |item|
item # an item
end

Or if you just want one page, you can have it:
Expand All @@ -74,7 +74,7 @@ You can also set the cursor size at any time using any of these variations:
# on the cursor
cursor = person.activities_list(:max_results => 10)
# or on the page
cursor.next_page(:max_results => 5)
items = cursor.next_page(:max_results => 5)

### Plusoners and Resharers

Expand All @@ -92,19 +92,17 @@ Getting comments for an activity is done just like getting activities for a pers

activity = GooglePlus::Activity.get(123)
cursor = activity.list_comments
while cursor.next_page
cursor.items.count # a bunch of comments
cursor.each do |item|
# a comment
end

## Searching

You can search for [people](https://developers.google.com/+/api/latest/people/search) or [activities](https://developers.google.com/+/api/latest/activities/search) using the respective `search` methods, which also yield `GooglePlus::Cursor` objects. Here's an example:

search = GooglePlus::Person.search('john crepezzi')
while search.next_page
search.each do |p|
puts p.display_name
end
search.each do |p|
puts p.display_name
end

## Setting options
Expand Down
16 changes: 15 additions & 1 deletion lib/google_plus/cursor.rb
Expand Up @@ -6,6 +6,19 @@ class Cursor

extend GooglePlus::Resource

# Go through each item
# @yieldparam [GooglePlus::Entity] an individual item
# @yieldreturn [GooglePlus::Cursor] self
def each
while items = next_page
break if items.empty?
items.each do |item|
yield item
end
end
self
end

# Get the current page of results
# @return [Array] the current page of results, or nil if the page is blank
def items(params = {})
Expand Down Expand Up @@ -51,7 +64,8 @@ def load_page(params)
data = JSON::parse(json)
@next_page_token = data['nextPageToken']
if items = data['items']
return data['items'].map { |d| @resource_klass.send(:new, d) }
return nil if items.empty?
return items.map { |d| @resource_klass.send(:new, d) }
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/examples/comment_spec.rb
Expand Up @@ -14,7 +14,7 @@
cursor.items.each { |c| c.should be_a(GooglePlus::Comment) }
end

it 'should get an empty array when there are no comments' do
it 'should get nil when there are no comments' do
cursor = GooglePlus::Comment.for_activity('z12dh5o4hzzjujpt423wcvtq2k2igvsl0')
cursor.next_page.should be_nil
end
Expand Down
19 changes: 19 additions & 0 deletions spec/examples/cursor_spec.rb
@@ -0,0 +1,19 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe GooglePlus::Cursor do

describe :each do

it 'should go through multiple pages' do
cursor = GooglePlus::Cursor.new(Object, :get, '/nothing')
cursor.should_receive(:next_page).and_return(['123'])
cursor.should_receive(:next_page).and_return(['456'])
cursor.should_receive(:next_page).and_return(nil)
items = []
cursor.each { |c| items << c }
items.should == ['123', '456']
end

end

end

0 comments on commit b0e784e

Please sign in to comment.