2013-02-06 03:16:51 +08:00
|
|
|
require_dependency 'oneboxer/handlebars_onebox'
|
|
|
|
|
|
|
|
module Oneboxer
|
|
|
|
class TwitterOnebox < HandlebarsOnebox
|
2013-06-13 13:42:54 +08:00
|
|
|
BASE_URL = 'https://api.twitter.com'.freeze
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-06-13 13:42:54 +08:00
|
|
|
REGEX =
|
|
|
|
/^https?:\/\/(?:www\.)?twitter.com\/(?<user>[^\/]+)\/status\/(?<id>\d+)$/
|
|
|
|
|
|
|
|
matcher REGEX
|
2013-06-13 07:39:18 +08:00
|
|
|
|
|
|
|
# TODO: use zocial instead
|
2013-02-06 03:16:51 +08:00
|
|
|
favicon 'twitter.png'
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2013-06-13 07:39:18 +08:00
|
|
|
def fetch_html
|
2013-06-13 13:42:54 +08:00
|
|
|
raise Discourse::SiteSettingMissing if twitter_credentials_missing?
|
|
|
|
|
|
|
|
tweet_for @url.match(REGEX)[:id]
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def parse(data)
|
|
|
|
result = JSON.parse(data)
|
|
|
|
|
2013-06-13 13:42:54 +08:00
|
|
|
result['created_at'] =
|
|
|
|
Time.parse(result['created_at']).strftime("%I:%M%p - %d %b %y")
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-06-14 12:09:37 +08:00
|
|
|
result['text'] = link_all_the_things_in result['text']
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2013-06-13 13:42:54 +08:00
|
|
|
private
|
|
|
|
|
2013-06-14 12:09:37 +08:00
|
|
|
def link_all_the_things_in(text)
|
|
|
|
link_hashtags_in link_handles_in link_urls_in(text)
|
2013-06-14 12:00:47 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def link_urls_in(text)
|
|
|
|
URI.extract(text, %w(http https)).each do |url|
|
|
|
|
text.gsub!(url, "<a href='#{url}' target='_blank'>#{url}</a>")
|
|
|
|
end
|
|
|
|
|
|
|
|
text
|
|
|
|
end
|
|
|
|
|
|
|
|
def link_handles_in(text)
|
|
|
|
text.scan(/\s@(\w+)/).flatten.uniq.each do |handle|
|
|
|
|
text.gsub!("@#{handle}", [
|
|
|
|
"<a href='https://twitter.com/#{handle}' target='_blank'>",
|
|
|
|
"@#{handle}",
|
|
|
|
"</a>"
|
|
|
|
].join)
|
|
|
|
end
|
|
|
|
|
|
|
|
text
|
|
|
|
end
|
|
|
|
|
2013-06-14 12:09:37 +08:00
|
|
|
def link_hashtags_in(text)
|
|
|
|
text.scan(/\s#(\w+)/).flatten.uniq.each do |hashtag|
|
|
|
|
text.gsub!("##{hashtag}", [
|
|
|
|
"<a href='https://twitter.com/search?q=%23#{hashtag}' ",
|
|
|
|
"target='_blank'>",
|
|
|
|
"##{hashtag}",
|
|
|
|
"</a>"
|
|
|
|
].join)
|
|
|
|
end
|
|
|
|
|
|
|
|
text
|
|
|
|
end
|
|
|
|
|
2013-06-13 13:42:54 +08:00
|
|
|
def tweet_for(id)
|
|
|
|
request = Net::HTTP::Get.new(tweet_uri_for id)
|
|
|
|
|
|
|
|
request.add_field 'Authorization', "Bearer #{bearer_token}"
|
|
|
|
|
|
|
|
http(tweet_uri_for id).request(request).body
|
|
|
|
end
|
|
|
|
|
|
|
|
def authorization
|
|
|
|
request = Net::HTTP::Post.new(auth_uri)
|
|
|
|
|
|
|
|
request.add_field 'Authorization',
|
|
|
|
"Basic #{bearer_token_credentials}"
|
|
|
|
request.add_field 'Content-Type',
|
|
|
|
'application/x-www-form-urlencoded;charset=UTF-8'
|
|
|
|
|
|
|
|
request.set_form_data 'grant_type' => 'client_credentials'
|
|
|
|
|
|
|
|
http(auth_uri).request(request).body
|
|
|
|
end
|
|
|
|
|
|
|
|
def bearer_token
|
|
|
|
@access_token ||= JSON.parse(authorization).fetch('access_token')
|
|
|
|
end
|
|
|
|
|
|
|
|
def bearer_token_credentials
|
|
|
|
Base64.strict_encode64(
|
|
|
|
"#{URI::encode(consumer_key)}:#{URI::encode(consumer_secret)}"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def auth_uri
|
|
|
|
URI.parse "#{BASE_URL}/oauth2/token"
|
|
|
|
end
|
|
|
|
|
|
|
|
def tweet_uri_for(id)
|
|
|
|
URI.parse "#{BASE_URL}/1.1/statuses/show.json?id=#{id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def http(uri)
|
|
|
|
Net::HTTP.new(uri.host, uri.port).tap { |http| http.use_ssl = true }
|
|
|
|
end
|
2013-06-13 07:39:18 +08:00
|
|
|
|
2013-06-13 13:42:54 +08:00
|
|
|
def consumer_key
|
|
|
|
SiteSetting.twitter_consumer_key
|
2013-06-13 07:39:18 +08:00
|
|
|
end
|
|
|
|
|
2013-06-13 13:42:54 +08:00
|
|
|
def consumer_secret
|
|
|
|
SiteSetting.twitter_consumer_secret
|
|
|
|
end
|
2013-06-13 07:39:18 +08:00
|
|
|
|
2013-06-13 13:42:54 +08:00
|
|
|
def twitter_credentials_missing?
|
|
|
|
consumer_key.blank? || consumer_secret.blank?
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|