Class: Services::Tumblr

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

Constant Summary collapse

MAX_CHARACTERS =
1000

Instance Method Summary collapse

Instance Method Details

#build_tumblr_post(post, url) ⇒ Object


31
32
33
# File 'app/models/services/tumblr.rb', line 31

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

#consumer_keyObject


8
9
10
# File 'app/models/services/tumblr.rb', line 8

def consumer_key
  AppConfig.services.tumblr.key
end

#consumer_secretObject


12
13
14
# File 'app/models/services/tumblr.rb', line 12

def consumer_secret
  AppConfig.services.tumblr.secret
end

#delete_from_tumblr(blog_name, service_post_id) ⇒ Object


54
55
56
# File 'app/models/services/tumblr.rb', line 54

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

#delete_post(post) ⇒ Object


44
45
46
47
48
49
50
51
52
# File 'app/models/services/tumblr.rb', line 44

def delete_post(post)
  if post.present? && post.tumblr_ids.present?
    Rails.logger.debug("event=delete_from_service type=tumblr sender_id=#{self.user_id}")
    tumblr_posts = JSON.parse(post.tumblr_ids)
    tumblr_posts.each do |blog_name,post_id|
      delete_from_tumblr(blog_name, post_id)
    end
  end
end

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


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

def post(post, url='')
  body = build_tumblr_post(post, url)
   = JSON.parse(client.get("/v2/user/info").body)
  blogs = ["response"]["user"]["blogs"].map { |blog| URI.parse(blog['url']) }
  tumblr_ids = {}
  blogs.each do |blog|
    resp = client.post("/v2/blog/#{blog.host}/post", body)
    if resp.code == "201"
      tumblr_ids[blog.host.to_s] = JSON.parse(resp.body)["response"]["id"]
    end
  post.tumblr_ids = tumblr_ids.to_json
  post.save
  end
end

#providerObject


4
5
6
# File 'app/models/services/tumblr.rb', line 4

def provider
  "tumblr"
end

#tumblr_template(post, url) ⇒ Object


35
36
37
38
39
40
41
42
# File 'app/models/services/tumblr.rb', line 35

def tumblr_template(post, url)
  html = ''
  post.photos.each do |photo|
    html << "![photo](#{photo.url(:scaled_full)})\n\n"
  end
  html << post.message.html(mentioned_people: [])
  html << "\n\n[original post](#{url})"
end