Skip to content

Commit

Permalink
support search pagination via the next_page value
Browse files Browse the repository at this point in the history
  • Loading branch information
cyu committed Feb 13, 2010
1 parent d84366e commit 767ddaa
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions lib/twitter/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ def fetch(force=false)
if @fetch.nil? || force
query = @query.dup
query[:q] = query[:q].join(' ')
response = self.class.get('http://search.twitter.com/search.json', :query => query, :format => :json, :headers => {'User-Agent' => user_agent})
@fetch = Hashie::Mash.new(response)
perform_get(query)
end

@fetch
Expand All @@ -126,5 +125,23 @@ def fetch(force=false)
def each
fetch()['results'].each { |r| yield r }
end

def next_page?
!!fetch()['next_page']
end

def fetch_next_page
if next_page?
s = Search.new(nil, :user_agent => user_agent)
s.perform_get(fetch()['next_page'][1..-1])
s
end
end

protected
def perform_get(query)
response = self.class.get('http://search.twitter.com/search.json', :query => query, :format => :json, :headers => {'User-Agent' => user_agent})
@fetch = Hashie::Mash.new(response)
end
end
end

0 comments on commit 767ddaa

Please sign in to comment.