diff --git a/app/helpers/social_helper.rb b/app/helpers/social_helper.rb new file mode 100644 index 00000000..00447583 --- /dev/null +++ b/app/helpers/social_helper.rb @@ -0,0 +1,4 @@ +module SocialHelper + include SocialHelper::TwitterMethods + include SocialHelper::TumblrMethods +end \ No newline at end of file diff --git a/app/helpers/social_helper/twitter_methods.rb b/app/helpers/social_helper/twitter_methods.rb new file mode 100644 index 00000000..04579e28 --- /dev/null +++ b/app/helpers/social_helper/twitter_methods.rb @@ -0,0 +1,38 @@ +require 'cgi' + +module SocialHelper::TwitterMethods + include Rails.application.routes.url_helpers + include MarkdownHelper + + def prepare_tweet(answer) + question_content = twitter_markdown answer.question.content.gsub(/\@(\w+)/, '\1') + original_question_length = question_content.length + answer_content = twitter_markdown answer.content + original_answer_length = answer_content.length + answer_url = show_user_answer_url( + id: answer.id, + username: answer.user.screen_name, + host: APP_CONFIG['hostname'], + protocol: (APP_CONFIG['https'] ? :https : :http) + ) + + parsed_tweet = { :valid => false } + tweet_text = "" + + until parsed_tweet[:valid] + tweet_text = "#{question_content[0..122]}#{'…' if original_question_length > [123, question_content.length].min}" \ + " — #{answer_content[0..123]}#{'…' if original_answer_length > [124, answer_content.length].min} #{answer_url}" + + parsed_tweet = Twitter::TwitterText::Validation::parse_tweet(tweet_text) + + question_content = question_content[0..-2] + answer_content = answer_content[0..-2] + end + + tweet_text + end + + def twitter_share_url(answer) + "https://twitter.com/intent/tweet?text=#{CGI.escape(prepare_tweet(answer))}" + end +end \ No newline at end of file diff --git a/app/models/services/twitter.rb b/app/models/services/twitter.rb index 0e090bcd..c605bc93 100644 --- a/app/models/services/twitter.rb +++ b/app/models/services/twitter.rb @@ -1,6 +1,5 @@ class Services::Twitter < Service - include Rails.application.routes.url_helpers - include MarkdownHelper + include SocialHelper::TwitterMethods def provider "twitter" @@ -25,32 +24,4 @@ class Services::Twitter < Service def post_tweet(answer) client.update! prepare_tweet(answer) end - - def prepare_tweet(answer) - question_content = twitter_markdown answer.question.content.gsub(/\@(\w+)/, '\1') - original_question_length = question_content.length - answer_content = twitter_markdown answer.content - original_answer_length = answer_content.length - answer_url = show_user_answer_url( - id: answer.id, - username: answer.user.screen_name, - host: APP_CONFIG['hostname'], - protocol: (APP_CONFIG['https'] ? :https : :http) - ) - - parsed_tweet = { :valid => false } - tweet_text = "" - - until parsed_tweet[:valid] - tweet_text = "#{question_content[0..122]}#{'…' if original_question_length > [123, question_content.length].min}" \ - " — #{answer_content[0..123]}#{'…' if original_answer_length > [124, answer_content.length].min} #{answer_url}" - - parsed_tweet = Twitter::TwitterText::Validation::parse_tweet(tweet_text) - - question_content = question_content[0..-2] - answer_content = answer_content[0..-2] - end - - tweet_text - end end