mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
Add Stack Exchange onebox
This commit is contained in:
46
lib/oneboxer/stack_exchange_onebox.rb
Normal file
46
lib/oneboxer/stack_exchange_onebox.rb
Normal file
@@ -0,0 +1,46 @@
|
||||
require_dependency 'oneboxer/handlebars_onebox'
|
||||
|
||||
module Oneboxer
|
||||
class StackExchangeOnebox < HandlebarsOnebox
|
||||
DOMAINS = [
|
||||
'stackexchange',
|
||||
'stackoverflow',
|
||||
'superuser',
|
||||
'serverfault',
|
||||
'askubuntu'
|
||||
]
|
||||
|
||||
# http://rubular.com/r/V3T0I1VTPn
|
||||
REGEX =
|
||||
/^http:\/\/(?:(?<subdomain>\w*)\.)?(?<domain>#{DOMAINS.join('|')})\.com\/(?:questions|q)\/(?<question>\d*)/
|
||||
|
||||
matcher REGEX
|
||||
favicon 'stackexchange.png'
|
||||
|
||||
def translate_url
|
||||
@url.match(REGEX) do |match|
|
||||
site = if match[:domain] == 'stackexchange'
|
||||
match[:subdomain]
|
||||
else
|
||||
match[:domain]
|
||||
end
|
||||
|
||||
["http://api.stackexchange.com/2.1/",
|
||||
"questions/#{match[:question]}",
|
||||
"?site=#{site}"
|
||||
].join
|
||||
end
|
||||
end
|
||||
|
||||
def parse(data)
|
||||
result = JSON.parse(data)['items'].first
|
||||
|
||||
result['creation_date'] =
|
||||
Time.at(result['creation_date'].to_i).strftime("%I:%M%p - %d %b %y")
|
||||
|
||||
result['tags'] = result['tags'].take(4).join(', ')
|
||||
|
||||
result
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user