class Tumblr::Post

Constants

PHOTO_URL_KEY_PREFIX

Attributes

body[RW]
caption[RW]
created_at[RW]
photo_url[RW]
tags[RW]
title[RW]
type[RW]
video_player[RW]

Public Class Methods

new(post_hash) click to toggle source
# File lib/tumblr_to_dayone/tumblr/post.rb, line 8
def initialize(post_hash)
  self.type = post_hash["type"]
  self.created_at = Time.at(post_hash["unix-timestamp"] || Time.now.to_i)
  self.title = post_hash["regular-title"]
  self.body = convert_to_markdown(post_hash["regular-body"]) || link_markdown(post_hash["link-text"], post_hash["link-url"])
  self.photo_url = largest_photo_url(post_hash)
  self.video_player = post_hash["video-player"]
  self.caption = convert_to_markdown(post_hash["photo-caption"] || post_hash["video-caption"])
  self.tags = post_hash["tags"] || []
    end

Public Instance Methods

add_to_dayone!(starred = false, dayone_journal = nil) click to toggle source
# File lib/tumblr_to_dayone.rb, line 55
def add_to_dayone!(starred = false, dayone_journal = nil)

  post_created = false

  photo = nil

  begin

    if self.photo_url
      photo = Tempfile.new(["#{self.photo_url.hash}", File.extname(self.photo_url)])
      photo.open
      photo.write open(self.photo_url).read
      photo.close
    end

    post_created = Dayone.create_post(self.full_body,
      :date => self.created_at,
      :starred => starred,
      :photo_path => photo ? photo.path : nil,
      :journal_path => dayone_journal
    )

  ensure
    if photo
      photo.close
      photo.unlink
    end
  end

  post_created
end
full_body() click to toggle source
# File lib/tumblr_to_dayone/tumblr/post.rb, line 19
def full_body
  [
    self.title ? "# #{self.title}" : nil,
    self.video_player ? video_player : nil,
    self.body ? self.body : nil,
    self.caption ? self.caption : nil,
    self.tags && !self.tags.empty? ? self.tags.map {|tag| "\\##{tag}"}.join(" ") : nil
  ].compact.join("\n\n")
end
to_s() click to toggle source
# File lib/tumblr_to_dayone/tumblr/post.rb, line 29
def to_s
  "Tumblr post created on #{self.created_at}\ntitle: #{self.title ? self.title : "<no title>"}\nphoto: #{!!self.photo_url}\nbody:\n#{self.full_body}\n"
end

Private Instance Methods

convert_to_markdown(content) click to toggle source
# File lib/tumblr_to_dayone/tumblr/post.rb, line 43
def convert_to_markdown(content)
  return nil unless content

  ReverseMarkdown.convert(CGI.unescapeHTML(content))
end
largest_photo_url(post_hash) click to toggle source
# File lib/tumblr_to_dayone/tumblr/post.rb, line 37
def largest_photo_url(post_hash)
  largest_width = post_hash.keys.map{ |key| key.match(PHOTO_URL_KEY_PREFIX) ? key.gsub(PHOTO_URL_KEY_PREFIX, "").to_i : nil }.compact.max

  largest_width ? post_hash["#{PHOTO_URL_KEY_PREFIX}#{largest_width}"] : nil
end