This new class replaces all existing server side message rendering helpers and is the new global entry point for such needs. All models with relevant fields now expose an instance of MessageRenderer for those. MessageRenderer acts as gateway between the existing processing solutions for markdown, mentions and tags and provides a very flexible interface for all output needs. This makes the API to obtain a message in a certain format clear. As a result of centralizing the processing a lot of duplication is eliminated. Centralizing the message processing also makes it clear where to change its behaviour, add new representations and what options are already available.
64 lines
1.8 KiB
Ruby
64 lines
1.8 KiB
Ruby
class Services::Tumblr < Service
|
|
MAX_CHARACTERS = 1000
|
|
|
|
def provider
|
|
"tumblr"
|
|
end
|
|
|
|
def consumer_key
|
|
AppConfig.services.tumblr.key
|
|
end
|
|
|
|
def consumer_secret
|
|
AppConfig.services.tumblr.secret
|
|
end
|
|
|
|
def post(post, url='')
|
|
body = build_tumblr_post(post, url)
|
|
user_info = JSON.parse(client.get("/v2/user/info").body)
|
|
blogs = user_info["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
|
|
|
|
def build_tumblr_post(post, url)
|
|
{ :type => 'text', :format => "markdown", :body => tumblr_template(post, url) }
|
|
end
|
|
|
|
def tumblr_template(post, url)
|
|
html = ''
|
|
post.photos.each do |photo|
|
|
html << "})\n\n"
|
|
end
|
|
html << post.message.html(mentioned_people: [])
|
|
html << "\n\n[original post](#{url})"
|
|
end
|
|
|
|
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
|
|
|
|
def delete_from_tumblr(blog_name, service_post_id)
|
|
client.post("/v2/blog/#{blog_name}/post/delete", "id" => service_post_id)
|
|
end
|
|
|
|
private
|
|
def client
|
|
@consumer ||= OAuth::Consumer.new(consumer_key, consumer_secret, :site => 'http://api.tumblr.com')
|
|
@client ||= OAuth::AccessToken.new(@consumer, self.access_token, self.access_secret)
|
|
end
|
|
end
|
|
|