Class: Services::Tumblr

Inherits:
Service show all
Defined in:
app/models/services/tumblr.rb

Constant Summary collapse

MAX_CHARACTERS =
1000

Instance Attribute Summary

Attributes inherited from Service

#access_level, #info

Instance Method Summary collapse

Methods inherited from Service

auth, first_from_omniauth, initialize_from_omniauth, options, #profile_photo_url, service_type, titles

Instance Method Details

#build_tumblr_post(post, url) ⇒ Object


35
36
37
# File 'app/models/services/tumblr.rb', line 35

def build_tumblr_post(post, url)
  { :type => 'text', :format => "markdown", :body => tumblr_template(post, url)  }
end

#consumer_keyObject


10
11
12
# File 'app/models/services/tumblr.rb', line 10

def consumer_key
  AppConfig.services.tumblr.key
end

#consumer_secretObject


14
15
16
# File 'app/models/services/tumblr.rb', line 14

def consumer_secret
  AppConfig.services.tumblr.secret
end

#delete_from_service(opts) ⇒ Object


51
52
53
54
55
56
57
# File 'app/models/services/tumblr.rb', line 51

def delete_from_service(opts)
  logger.debug "event=delete_from_service type=tumblr sender_id=#{user_id} tumblr_ids=#{opts[:tumblr_ids]}"
  tumblr_posts = JSON.parse(opts[:tumblr_ids])
  tumblr_posts.each do |blog_name, post_id|
    delete_from_tumblr(blog_name, post_id)
  end
end

#delete_from_tumblr(blog_name, service_post_id) ⇒ Object


59
60
61
# File 'app/models/services/tumblr.rb', line 59

def delete_from_tumblr(blog_name, service_post_id)
  client.post("/v2/blog/#{blog_name}/post/delete", "id" => service_post_id)
end

#post(post, url = '') ⇒ Object


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/services/tumblr.rb', line 18

def post(post, url='')
  body = build_tumblr_post(post, url)
   = JSON.parse(client.get("/v2/user/info").body)
  blogs = ["response"]["user"]["blogs"]
  primaryblog = blogs.find {|blog| blog["primary"] } || blogs[0]
  tumblr_ids = {}

  blogurl = URI.parse(primaryblog["url"])
  resp = client.post("/v2/blog/#{blogurl.host}/post", body)
  if resp.code == "201"
    tumblr_ids[blogurl.host.to_s] = JSON.parse(resp.body)["response"]["id"]
  end

  post.tumblr_ids = tumblr_ids.to_json
  post.save
end

#post_opts(post) ⇒ Object


47
48
49
# File 'app/models/services/tumblr.rb', line 47

def post_opts(post)
  {tumblr_ids: post.tumblr_ids} if post.tumblr_ids.present?
end

#providerObject


6
7
8
# File 'app/models/services/tumblr.rb', line 6

def provider
  "tumblr"
end

#tumblr_template(post, url) ⇒ Object


39
40
41
42
43
44
45
# File 'app/models/services/tumblr.rb', line 39

def tumblr_template(post, url)
  photo_html = post.photos.map {|photo|
    "![photo](#{photo.url(:scaled_full)})\n\n"
  }.join

  "#{photo_html}#{post.message.html(mentioned_people: [])}\n\n[original post](#{url})"
end