2013-06-12 19:39:18 -04:00
|
|
|
# TODO implement per: http://meta.discourse.org/t/twitter-oneboxes-are-bust/7410/3
|
2013-02-05 14:16:51 -05:00
|
|
|
require_dependency 'oneboxer/handlebars_onebox'
|
|
|
|
|
|
|
|
module Oneboxer
|
|
|
|
class TwitterOnebox < HandlebarsOnebox
|
|
|
|
|
2013-06-12 19:39:18 -04:00
|
|
|
REGEX = /^https?:\/\/(?:www\.)?twitter.com\/(?<user>[^\/]+)\/status\/(?<id>\d+)$/
|
|
|
|
# matcher REGEX
|
|
|
|
|
|
|
|
# TODO: use zocial instead
|
2013-02-05 14:16:51 -05:00
|
|
|
favicon 'twitter.png'
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2013-06-12 19:39:18 -04:00
|
|
|
def fetch_html
|
|
|
|
m = @url.match(REGEX)
|
|
|
|
|
|
|
|
if SiteSetting.twitter_consumer_key.present? && SiteSetting.twitter_consumer_secret.present?
|
|
|
|
token = prepare_access_token(SiteSetting.twitter_consumer_key, SiteSetting.twitter_consumer_secret)
|
|
|
|
token.request(:get, "https://api.twitter.com/1.1/statuses/show/#{URI::encode(m[:id])}.json")
|
|
|
|
else
|
|
|
|
# perhaps?
|
|
|
|
raise Discourse::SiteSettingMissing
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def parse(data)
|
|
|
|
result = JSON.parse(data)
|
|
|
|
|
|
|
|
result["created_at"] = Time.parse(result["created_at"]).strftime("%I:%M%p - %d %b %y")
|
|
|
|
|
|
|
|
# Hyperlink URLs
|
2013-02-25 11:42:20 -05:00
|
|
|
URI.extract(result['text'], %w(http https)).each do |url|
|
2013-02-05 14:16:51 -05:00
|
|
|
result['text'].gsub!(url, "<a href='#{url}' target='_blank'>#{url}</a>")
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2013-06-12 19:39:18 -04:00
|
|
|
protected
|
|
|
|
|
|
|
|
def get_message_json(consumer_key, consumer_secret)
|
|
|
|
raise "NOT IMPLEMENTED"
|
|
|
|
# implement per http://meta.discourse.org/t/twitter-oneboxes-are-bust/7410/3
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|