Permalink
Browse files

Added GooglePlus::Cursor#each

  • Loading branch information...
1 parent da8ffa8 commit b0e784e79373365cfbd212f1c6bf26b5d110c524 @seejohnrun committed Jan 4, 2012
Showing with 43 additions and 12 deletions.
  1. +8 −10 README.md
  2. +15 −1 lib/google_plus/cursor.rb
  3. +1 −1 spec/examples/comment_spec.rb
  4. +19 −0 spec/examples/cursor_spec.rb
View
@@ -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:
@@ -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
@@ -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
@@ -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 = {})
@@ -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
@@ -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
@@ -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.