Permalink
Browse files

Reader#get_all_posts does some recursive requests to get every post f…

…or a user
  • Loading branch information...
1 parent abf79f4 commit b8b65a3d34b1d8cd3198f90d120ce46415e2fff6 @mwunsch mwunsch committed Mar 13, 2010
Showing with 23 additions and 1 deletion.
  1. +15 −1 lib/tumblr/reader.rb
  2. +8 −0 test/test_tumblr.rb
View
@@ -15,7 +15,7 @@ def read(username, params={})
# http://www.tumblr.com/docs/en/api#authenticated_read
def authenticated_read(username, params={})
- raise 'Needs requirements badly' unless (params.include?(:email) && params.include?(:password)) || defaults
+ raise 'You must provide an email address and password' unless (params.include?(:email) && params.include?(:password)) || defaults
self.class.read username, :post, parameters(params)
end
@@ -26,6 +26,20 @@ def parameters(params)
params.reject {|key,value| !allowed.include? key }
end
+ # Transform ALL of the posts for user/group to Post objects.
+ # This could take a while...
+ def get_all_posts(username, start = 0, total = nil)
+ first_read = authenticated_read(username, {:num => 50,:start => start}).perform
+ raise %Q(Tumblr response was not successful, "#{first_read.code}: #{first_read.message}") if !first_read.success?
+ posts = self.class.get_posts(first_read)
+ offset = start + posts.count
+ post_total = total || first_read['tumblr']['posts']['total'].to_i
+ if post_total > offset
+ posts |= get_all_posts(username, offset, post_total)
+ end
+ posts
+ end
+
# Get the Posts as Post objects from a Read response.
# Pass an additional type parameter to only get Posts of a certain type.
def self.get_posts(response, type = nil)
View
@@ -232,6 +232,14 @@ class TestTumblr < Test::Unit::TestCase
assert_equal link.first['link_url'], link_post.url
assert_equal link.first['reblog_key'], link_post.reblog_key
end
+
+ test 'gets all the posts for a username' do
+ reader = Tumblr::Reader.new('test@testermcgee.com','dontrevealmysecrets')
+ posts = VCR.with_cassette('read/authenticated') do
+ reader.get_all_posts('mwunsch')
+ end
+ assert_equal 66, posts.count
+ end
end
describe 'Writer' do

0 comments on commit b8b65a3

Please sign in to comment.