Class: TumblrPostPublisherJob

Inherits:
Struct
  • Object
show all
Includes:
Atreides::Extendable
Defined in:
app/models/tumblr_post_publisher_job.rb,
app/models/tumblr_post_publisher_job.rb

Overview

Class to aid in the publishing of posts to Tumblr. This is typically used with Delayed Job in a background process Eg:

Delayed::Job.enqueue(TumblrPostPublisherJob.new(post.id))

It requires that there is a ‘tumblr’ section in your settings.yml config file

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#post_idObject

Returns the value of attribute post_id

Returns:

  • (Object)

    the current value of post_id



9
10
11
# File 'app/models/tumblr_post_publisher_job.rb', line 9

def post_id
  @post_id
end

Instance Method Details

#performInt

Executes the job. This authorizes the client with Tumblr using settings.yml configuration It will update the post object with the posts tumblr_id on success

Returns:

  • (Int)

    the tumblr id of the newly created post



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/tumblr_post_publisher_job.rb', line 20

def perform

  return false unless Settings.has_key?('tumblr')

  # Find objects
  post = Atreides::Post.find(post_id)

  # Abort if already posted
  return if post.tumblr_id?

  # Common part
  tumblr = {
    :slug => post.slug
  }

  add_link_for_more = false
  part = posts.first

  case part.content_type
  when "text"
    tumblr.merge!({
      :type       => "regular",
      :title      => part.title,
      :body       => part.body
    })
  when "videos"
    tumblr.merge!({
      :type     => "video",
      :embed    => part.videos.first.url,
      :caption  => part.videos.first.caption
    })
    add_link_for_more = (part.videos.count > 1)
  when "photos"
    tumblr.merge!({
      :type     => "photo",
      :source   => part.photos.first.image.url,
      :caption  => part.photos.first.caption
    })
    add_link_for_more = (part.photos.count > 1)
  end

  link = post_url(post, :slug => post.slug)

  tumblr[:caption] += "<br />View more <a href='#{link}'>here</a>" if add_link_for_more

  # Authentication
  user = Tumblr::User.new(Settings.tumblr.email, Settings.tumblr.pass, false)

  tumblr_id = Tumblr::Post.create(user, tumblr)

  # Tumblr it!
  post.update_tumblr_id(tumblr_id)
end