From b0e784e79373365cfbd212f1c6bf26b5d110c524 Mon Sep 17 00:00:00 2001 From: John Crepezzi Date: Wed, 4 Jan 2012 09:51:25 -0500 Subject: [PATCH] Added GooglePlus::Cursor#each --- README.md | 18 ++++++++---------- lib/google_plus/cursor.rb | 16 +++++++++++++++- spec/examples/comment_spec.rb | 2 +- spec/examples/cursor_spec.rb | 19 +++++++++++++++++++ 4 files changed, 43 insertions(+), 12 deletions(-) create mode 100644 spec/examples/cursor_spec.rb diff --git a/README.md b/README.md index 71a216a..d2174dd 100644 --- a/README.md +++ b/README.md @@ -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,8 +92,8 @@ 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 @@ -101,10 +101,8 @@ Getting comments for an activity is done just like getting activities for a pers 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 diff --git a/lib/google_plus/cursor.rb b/lib/google_plus/cursor.rb index 994d940..4bffd8d 100644 --- a/lib/google_plus/cursor.rb +++ b/lib/google_plus/cursor.rb @@ -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 diff --git a/spec/examples/comment_spec.rb b/spec/examples/comment_spec.rb index 4333ee8..356ca47 100644 --- a/spec/examples/comment_spec.rb +++ b/spec/examples/comment_spec.rb @@ -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 diff --git a/spec/examples/cursor_spec.rb b/spec/examples/cursor_spec.rb new file mode 100644 index 0000000..0648b90 --- /dev/null +++ b/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