mirror of
https://github.com/codeninjasllc/discourse.git
synced 2025-04-29 23:43:55 -04:00
Add Stack Exchange onebox
This commit is contained in:
parent
ad6705cca7
commit
1fb0b424ed
4 changed files with 136 additions and 0 deletions
app/assets/images/favicons
lib/oneboxer
spec/components/oneboxer
BIN
app/assets/images/favicons/stackexchange.png
Normal file
BIN
app/assets/images/favicons/stackexchange.png
Normal file
Binary file not shown.
After ![]() (image error) Size: 1.4 KiB |
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
|
38
lib/oneboxer/templates/stack_exchange_onebox.hbrs
Normal file
38
lib/oneboxer/templates/stack_exchange_onebox.hbrs
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
<div class="onebox-result">
|
||||||
|
{{#host}}
|
||||||
|
<div class="source">
|
||||||
|
<div class="info">
|
||||||
|
<a href="{{link}}" class="track-link" target="_blank">
|
||||||
|
{{#favicon}}
|
||||||
|
<img class="favicon" src="{{favicon}}">
|
||||||
|
{{/favicon}}
|
||||||
|
{{host}}
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{{/host}}
|
||||||
|
|
||||||
|
<div class="onebox-result-body">
|
||||||
|
{{#owner.profile_image}}
|
||||||
|
<a href="{{owner.link}}" target="_blank">
|
||||||
|
<img alt="{{owner.display_name}}" src="{{owner.profile_image}}">
|
||||||
|
</a>
|
||||||
|
{{/owner.profile_image}}
|
||||||
|
|
||||||
|
<h4>
|
||||||
|
<a href="{{link}}" target="_blank">{{{title}}}</a>
|
||||||
|
</h4>
|
||||||
|
|
||||||
|
<div class="date">
|
||||||
|
asked by <a href="{{owner.link}}" target="_blank">
|
||||||
|
{{owner.display_name}}
|
||||||
|
</a>
|
||||||
|
on <a href="{{link}}" target="_blank">{{creation_date}}</a>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<strong>{{tags}}</strong>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="clearfix"></div>
|
||||||
|
</div>
|
52
spec/components/oneboxer/stack_exchange_onebox_spec.rb
Normal file
52
spec/components/oneboxer/stack_exchange_onebox_spec.rb
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe Oneboxer::StackExchangeOnebox do
|
||||||
|
describe '#translate_url' do
|
||||||
|
let(:question) { '15622543' }
|
||||||
|
let(:api_url) {
|
||||||
|
"http://api.stackexchange.com/2.1/questions/#{question}?site=#{site}"
|
||||||
|
}
|
||||||
|
|
||||||
|
context 'when the question is from Stack Overflow' do
|
||||||
|
let(:site) { 'stackoverflow' }
|
||||||
|
|
||||||
|
it 'returns the correct api url for an expanded url' do
|
||||||
|
onebox = described_class.new([
|
||||||
|
"http://#{site}.com/",
|
||||||
|
"questions/#{question}/discourse-ruby-2-0-rails-4"
|
||||||
|
].join)
|
||||||
|
|
||||||
|
expect(onebox.translate_url).to eq api_url
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns the correct api url for a share url' do
|
||||||
|
onebox = described_class.new("http://#{site}.com/q/#{question}")
|
||||||
|
|
||||||
|
expect(onebox.translate_url).to eq api_url
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when the question is from Super User' do
|
||||||
|
let(:site) { 'superuser' }
|
||||||
|
|
||||||
|
it 'returns the correct api url' do
|
||||||
|
onebox = described_class.new("http://#{site}.com/q/#{question}")
|
||||||
|
|
||||||
|
expect(onebox.translate_url).to eq api_url
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when the question is from a Stack Exchange subdomain' do
|
||||||
|
let(:site) { 'gamedev' }
|
||||||
|
|
||||||
|
it 'returns the correct api url' do
|
||||||
|
onebox = described_class.new([
|
||||||
|
"http://#{site}.stackexchange.com/",
|
||||||
|
"questions/#{question}/how-to-prevent-the-too-awesome-to-use-syndrome"
|
||||||
|
].join)
|
||||||
|
|
||||||
|
expect(onebox.translate_url).to eq api_url
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Add table
Add a link
Reference in a new issue