discourse/lib/oneboxer/twitter_onebox.rb

40 lines
894 B
Ruby
Raw Normal View History

2013-02-06 03:16:51 +08:00
require_dependency 'oneboxer/handlebars_onebox'
require_dependency 'twitter_api'
2013-02-06 03:16:51 +08:00
module Oneboxer
class TwitterOnebox < HandlebarsOnebox
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
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
raise Discourse::SiteSettingMissing if TwitterApi.twitter_credentials_missing?
# a bit odd, but I think the api expects html
TwitterApi.raw_tweet_for(@url.match(REGEX)[:id])
2013-02-06 03:16:51 +08:00
end
def parse(data)
result = JSON.parse(data)
result['created_at'] =
Time.parse(result['created_at']).strftime("%I:%M%p - %d %b %y")
2013-02-06 03:16:51 +08:00
result['text'] = TwitterApi.prettify_tweet(result)
2013-02-06 03:16:51 +08:00
result
end
end
end