Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added GooglePlus::Cursor#each

  • Loading branch information...
commit b0e784e79373365cfbd212f1c6bf26b5d110c524 1 parent da8ffa8
John Crepezzi authored January 04, 2012
18  README.md
Source Rendered
@@ -56,12 +56,12 @@ And once you have an activity, you can move back to its person using `#person`.
56 56
 
57 57
 ### People do Things
58 58
 
59  
-Lastly, you can get a list of activities for a person, which is returned as a `GooglePlus::Cursor`.  You use it like:
  59
+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):
60 60
 
61 61
     person = GooglePlus::Person.new(123)
62 62
     cursor = person.list_activities
63  
-    while cursor.next_page
64  
-      cursor.items.count # a batch of activities
  63
+		cursor.each do |item|
  64
+		  item # an item
65 65
     end
66 66
 
67 67
 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:
74 74
     # on the cursor
75 75
     cursor = person.activities_list(:max_results => 10)
76 76
     # or on the page
77  
-    cursor.next_page(:max_results => 5)
  77
+    items = cursor.next_page(:max_results => 5)
78 78
 
79 79
 ### Plusoners and Resharers
80 80
 
@@ -92,8 +92,8 @@ Getting comments for an activity is done just like getting activities for a pers
92 92
 
93 93
     activity = GooglePlus::Activity.get(123)
94 94
     cursor = activity.list_comments
95  
-    while cursor.next_page
96  
-      cursor.items.count # a bunch of comments
  95
+		cursor.each do |item|
  96
+			# a comment
97 97
     end
98 98
 
99 99
 ## Searching
@@ -101,10 +101,8 @@ Getting comments for an activity is done just like getting activities for a pers
101 101
 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:
102 102
 
103 103
     search = GooglePlus::Person.search('john crepezzi')
104  
-    while search.next_page
105  
-      search.each do |p|
106  
-        puts p.display_name
107  
-      end
  104
+    search.each do |p|
  105
+			puts p.display_name
108 106
     end
109 107
 
110 108
 ## Setting options
16  lib/google_plus/cursor.rb
@@ -6,6 +6,19 @@ class Cursor
6 6
 
7 7
     extend GooglePlus::Resource
8 8
 
  9
+    # Go through each item
  10
+    # @yieldparam [GooglePlus::Entity] an individual item
  11
+    # @yieldreturn [GooglePlus::Cursor] self
  12
+    def each
  13
+      while items = next_page
  14
+        break if items.empty?
  15
+        items.each do |item|
  16
+          yield item
  17
+        end
  18
+      end
  19
+      self
  20
+    end
  21
+
9 22
     # Get the current page of results
10 23
     # @return [Array] the current page of results, or nil if the page is blank
11 24
     def items(params = {})
@@ -51,7 +64,8 @@ def load_page(params)
51 64
           data = JSON::parse(json)
52 65
           @next_page_token = data['nextPageToken']
53 66
           if items = data['items']
54  
-            return data['items'].map { |d| @resource_klass.send(:new, d) }
  67
+            return nil if items.empty?
  68
+            return items.map { |d| @resource_klass.send(:new, d) }
55 69
           end
56 70
         end
57 71
       end
2  spec/examples/comment_spec.rb
@@ -14,7 +14,7 @@
14 14
       cursor.items.each { |c| c.should be_a(GooglePlus::Comment) }
15 15
     end
16 16
 
17  
-    it 'should get an empty array when there are no comments' do
  17
+    it 'should get nil when there are no comments' do
18 18
       cursor = GooglePlus::Comment.for_activity('z12dh5o4hzzjujpt423wcvtq2k2igvsl0')
19 19
       cursor.next_page.should be_nil
20 20
     end
19  spec/examples/cursor_spec.rb
... ...
@@ -0,0 +1,19 @@
  1
+require File.dirname(__FILE__) + '/../spec_helper'
  2
+
  3
+describe GooglePlus::Cursor do
  4
+
  5
+  describe :each do
  6
+
  7
+    it 'should go through multiple pages' do
  8
+      cursor = GooglePlus::Cursor.new(Object, :get, '/nothing')
  9
+      cursor.should_receive(:next_page).and_return(['123'])
  10
+      cursor.should_receive(:next_page).and_return(['456'])
  11
+      cursor.should_receive(:next_page).and_return(nil)
  12
+      items = []
  13
+      cursor.each { |c| items << c }
  14
+      items.should == ['123', '456']
  15
+    end
  16
+
  17
+  end
  18
+
  19
+end

0 notes on commit b0e784e

Please sign in to comment.
Something went wrong with that request. Please try again.