2013-06-11 00:27:13 +08:00
|
|
|
require_dependency 'oneboxer/handlebars_onebox'
|
|
|
|
|
|
|
|
module Oneboxer
|
|
|
|
class StackExchangeOnebox < HandlebarsOnebox
|
2013-09-15 03:40:11 +08:00
|
|
|
|
|
|
|
unless defined? DOMAINS
|
|
|
|
DOMAINS = [
|
|
|
|
'stackexchange',
|
|
|
|
'stackoverflow',
|
|
|
|
'superuser',
|
|
|
|
'serverfault',
|
|
|
|
'askubuntu'
|
|
|
|
]
|
|
|
|
end
|
2013-06-11 00:27:13 +08:00
|
|
|
|
|
|
|
# http://rubular.com/r/V3T0I1VTPn
|
2013-09-15 03:40:11 +08:00
|
|
|
unless defined? REGEX
|
|
|
|
REGEX = /^http:\/\/(?:(?:(?<subsubdomain>\w*)\.)?(?<subdomain>\w*)\.)?(?<domain>#{DOMAINS.join('|')})\.com\/(?:questions|q)\/(?<question>\d*)/
|
|
|
|
end
|
2013-06-11 00:27:13 +08:00
|
|
|
|
|
|
|
matcher REGEX
|
|
|
|
favicon 'stackexchange.png'
|
|
|
|
|
|
|
|
def translate_url
|
|
|
|
@url.match(REGEX) do |match|
|
|
|
|
site = if match[:domain] == 'stackexchange'
|
2013-06-12 10:23:24 +08:00
|
|
|
[match[:subsubdomain],match[:subdomain]].compact.join('.')
|
2013-06-11 00:27:13 +08:00
|
|
|
else
|
2013-06-12 10:23:24 +08:00
|
|
|
[match[:subdomain],match[:domain]].compact.join('.')
|
2013-06-11 00:27:13 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
["http://api.stackexchange.com/2.1/",
|
|
|
|
"questions/#{match[:question]}",
|
|
|
|
"?site=#{site}"
|
|
|
|
].join
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse(data)
|
|
|
|
result = JSON.parse(data)['items'].first
|
|
|
|
|
2013-08-15 19:01:37 +08:00
|
|
|
if result
|
|
|
|
result['creation_date'] =
|
|
|
|
Time.at(result['creation_date'].to_i).strftime("%I:%M%p - %d %b %y")
|
2013-06-11 00:27:13 +08:00
|
|
|
|
2013-08-15 19:01:37 +08:00
|
|
|
result['tags'] = result['tags'].take(4).join(', ')
|
|
|
|
end
|
2013-06-11 00:27:13 +08:00
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|