module TumblrToDayone

Constants

VERSION

Public Class Methods

add_tumblr_posts_to_dayone(blog, options={}) { |post| ... } click to toggle source
options:

password - Password for blog (if there is one) filter - Only get Tumblr posts based on type (text, quote, photo, link, chat, video, or audio) automatically_add_each_post - Automatically add each Tumblr post or ask before adding each one (defaults to false) journal_path - Location of Day One Journal file

# File lib/tumblr_to_dayone.rb, line 17
def self.add_tumblr_posts_to_dayone(blog, options={})
  begin
    post_index = 0
    no_more_posts = false

    while !no_more_posts
      Tumblr.posts(blog, password = options[:password], :start => post_index, :type => options[:filter]) do |posts, total_posts|
        post_index += posts.count
        no_more_posts = posts.empty? || post_index >= total_posts

        exited = false

        posts.each do |post|
          unless exited
            post_status = options[:automatically_add_each_post] ? :y : yield(post)
            
            if post_status == :y || post_status == :s
              post_created = post.add_to_dayone!(starred = post_status == :s, dayone_journal = options[:journal_path])

              puts "ERROR: There was a problem adding the post." unless post_created
            elsif post_status == :exit
              exited = true
              no_more_posts = true
            end
          end
        end
      end
    end
  rescue TumblrPostsAPIError => e
    puts e.message
  end
end