From ed64a0990e8a2a99a599c92e6161073cf37c6ad4 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sun, 26 Dec 2021 22:04:46 +0100 Subject: [PATCH] Move tumblr post formatting into a separate helper class --- app/helpers/social_helper/tumblr_methods.rb | 37 +++++++++++++++++++++ app/models/services/tumblr.rb | 18 ++-------- 2 files changed, 40 insertions(+), 15 deletions(-) create mode 100644 app/helpers/social_helper/tumblr_methods.rb diff --git a/app/helpers/social_helper/tumblr_methods.rb b/app/helpers/social_helper/tumblr_methods.rb new file mode 100644 index 00000000..b9c3cfc3 --- /dev/null +++ b/app/helpers/social_helper/tumblr_methods.rb @@ -0,0 +1,37 @@ +require 'cgi' + +module SocialHelper::TumblrMethods + include Rails.application.routes.url_helpers + + def tumblr_title(answer) + asker = if answer.question.author_is_anonymous? + APP_CONFIG['anonymous_name'] + else + answer.question.user.profile.display_name.blank? ? answer.question.user.screen_name : answer.question.user.profile.display_name + end + + "#{asker} asked: #{answer.question.content}" + end + + def tumblr_body(answer) + answer_url = show_user_answer_url( + id: answer.id, + username: answer.user.screen_name, + host: APP_CONFIG['hostname'], + protocol: (APP_CONFIG['https'] ? :https : :http) + ) + + "#{answer.content}\n\n[Smile or comment on the answer here](#{answer_url})" + end + + def tumblr_share_url(answer) + answer_url = show_user_answer_url( + id: answer.id, + username: answer.user.screen_name, + host: APP_CONFIG['hostname'], + protocol: (APP_CONFIG['https'] ? :https : :http) + ) + + "https://www.tumblr.com/widgets/share/tool?shareSource=legacy&posttype=text&title=#{CGI.escape(tumblr_title(answer))}&url=#{CGI.escape(answer_url)}&caption=&content=#{CGI.escape(tumblr_body(answer))}" + end +end \ No newline at end of file diff --git a/app/models/services/tumblr.rb b/app/models/services/tumblr.rb index 8af15e1d..e99d8d56 100644 --- a/app/models/services/tumblr.rb +++ b/app/models/services/tumblr.rb @@ -1,6 +1,5 @@ class Services::Tumblr < Service - include Rails.application.routes.url_helpers - include MarkdownHelper + include SocialHelper::TumblrMethods def provider "tumblr" @@ -23,21 +22,10 @@ class Services::Tumblr < Service end def create_post(answer) - answer_url = show_user_answer_url( - id: answer.id, - username: answer.user.screen_name, - host: APP_CONFIG['hostname'], - protocol: (APP_CONFIG['https'] ? :https : :http) - ) - asker = if answer.question.author_is_anonymous? - APP_CONFIG['anonymous_name'] - else - answer.question.user.profile.display_name.blank? ? answer.question.user.screen_name : answer.question.user.profile.display_name - end client.text( self.uid, - title: "#{asker} asked: #{answer.question.content}", - body: "#{answer.content}\n\n[Smile or comment on the answer here](#{answer_url})", + title: tumblr_title(answer), + body: tumblr_body(answer), format: 'markdown', tweet: 'off' )