2013-02-05 14:16:51 -05:00
|
|
|
require_dependency 'oneboxer/handlebars_onebox'
|
2013-10-10 20:46:20 -04:00
|
|
|
require_dependency 'twitter_api'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
module Oneboxer
|
|
|
|
class TwitterOnebox < HandlebarsOnebox
|
|
|
|
|
2013-09-14 15:40:11 -04:00
|
|
|
unless defined? BASE_URL
|
|
|
|
BASE_URL = 'https://api.twitter.com'.freeze
|
|
|
|
end
|
|
|
|
|
|
|
|
unless defined? REGEX
|
|
|
|
REGEX = /^https?:\/\/(?:www\.)?twitter.com\/(?<user>[^\/]+)\/status\/(?<id>\d+)$/
|
|
|
|
end
|
2013-06-13 01:42:54 -04:00
|
|
|
|
|
|
|
matcher REGEX
|
2013-06-12 19:39:18 -04:00
|
|
|
|
|
|
|
# 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
|
2013-10-10 20:46:20 -04:00
|
|
|
raise Discourse::SiteSettingMissing if TwitterApi.twitter_credentials_missing?
|
2013-06-13 01:42:54 -04:00
|
|
|
|
2013-10-10 20:46:20 -04:00
|
|
|
# a bit odd, but I think the api expects html
|
|
|
|
TwitterApi.raw_tweet_for(@url.match(REGEX)[:id])
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def parse(data)
|
|
|
|
result = JSON.parse(data)
|
|
|
|
|
2013-06-13 01:42:54 -04:00
|
|
|
result['created_at'] =
|
|
|
|
Time.parse(result['created_at']).strftime("%I:%M%p - %d %b %y")
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-10-10 20:46:20 -04:00
|
|
|
result['text'] = TwitterApi.prettify_tweet(result)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|