lib/tumblr/reader.rb in tumblr-rb-1.0.0 vs lib/tumblr/reader.rb in tumblr-rb-1.1.0

- old
+ new

@@ -13,21 +13,35 @@ self.class.read username, :get, parameters(params) end # 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 # Setup parameters for Reads def parameters(params) allowed = [:start,:num,:type,:id,:filter,:tagged,:search,:state,:email,:password] params.merge! defaults if defaults 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) tumblr_post = response['tumblr']['posts']['post'] posts = tumblr_post.respond_to?(:each_pair) ? [tumblr_post] : tumblr_post @@ -36,16 +50,17 @@ posts end # Build a Post object from Reader's Post XML def self.build_post(post) - tumblr_post = setup_post(post) - tumblr_post.date = post['date_gmt'] - tumblr_post.format = post['format'].to_sym if post['format'] - tumblr_post.slug = post['slug'] - tumblr_post.tags post['tag'] if post['tag'] - tumblr_post + setup_post(post) do |tumblr_post| + tumblr_post.date = post['date_gmt'] + tumblr_post.format = post['format'].to_sym if post['format'] + tumblr_post.slug = post['slug'] + tumblr_post.tags post['tag'] if post['tag'] + tumblr_post.reblog_key = post['reblog_key'] if post['reblog_key'] + end end # Helper method to facilitate standard GET Read and Authenticated Read def self.read(username, via = :get, params = {}) Weary.request("http://#{username}.tumblr.com/api/read/", via) do |req| @@ -81,11 +96,11 @@ private def self.setup_post(post) post_type = post['type'].to_sym - case post_type + tumblr_post = case post_type when :regular build_regular(post) when :photo build_photo(post) when :quote @@ -99,9 +114,11 @@ when :audio build_audio(post) else raise "#{post_type} is not a recognized Tumblr post type." end + yield tumblr_post if block_given? + tumblr_post end def self.build_regular(post) post_id = post['id'] regular = Tumblr::Post::Regular.new(post_id) \ No newline at end of file