2014-12-12 12:42:34 -08:00
|
|
|
class Services::Twitter < Service
|
|
|
|
include Rails.application.routes.url_helpers
|
2015-01-12 04:00:00 -08:00
|
|
|
include MarkdownHelper
|
2014-12-12 12:42:34 -08:00
|
|
|
|
|
|
|
def provider
|
|
|
|
"twitter"
|
|
|
|
end
|
|
|
|
|
|
|
|
def post(answer)
|
|
|
|
Rails.logger.debug "posting to Twitter {'answer' => #{answer.id}, 'user' => #{self.user_id}}"
|
|
|
|
post_tweet answer
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def client
|
|
|
|
@client ||= Twitter::REST::Client.new(
|
|
|
|
consumer_key: APP_CONFIG['sharing']['twitter']['consumer_key'],
|
|
|
|
consumer_secret: APP_CONFIG['sharing']['twitter']['consumer_secret'],
|
|
|
|
access_token: self.access_token,
|
|
|
|
access_token_secret: self.access_secret
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def post_tweet(answer)
|
2014-12-27 08:37:54 -08:00
|
|
|
client.update! prepare_tweet(answer)
|
2014-12-12 12:42:34 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
def prepare_tweet(answer)
|
|
|
|
# TODO: improve this.
|
2015-04-18 14:15:47 -07:00
|
|
|
question_content = twitter_markdown answer.question.content.gsub(/\@(\w+)/, '\1')
|
2015-01-29 07:45:58 -08:00
|
|
|
answer_content = twitter_markdown answer.content
|
2014-12-12 12:42:34 -08:00
|
|
|
answer_url = show_user_answer_url(
|
|
|
|
id: answer.id,
|
2014-12-12 14:53:23 -08:00
|
|
|
username: answer.user.screen_name,
|
|
|
|
host: APP_CONFIG['hostname'],
|
|
|
|
protocol: (APP_CONFIG['https'] ? :https : :http)
|
2014-12-12 12:42:34 -08:00
|
|
|
)
|
2014-12-27 12:03:18 -08:00
|
|
|
"#{question_content[0..54]}#{'…' if question_content.length > 55}" \
|
2014-12-12 14:53:23 -08:00
|
|
|
" — #{answer_content[0..55]}#{'…' if answer_content.length > 56} #{answer_url}"
|
2014-12-12 12:42:34 -08:00
|
|
|
end
|
2015-04-18 14:15:47 -07:00
|
|
|
end
|