Merge pull request #1002 from chrishunt/add-stack-exchange-onebox
Add Stack Exchange onebox
This commit is contained in:
commit
ab5507ac06
Binary file not shown.
After Width: | Height: | Size: 1.4 KiB |
|
@ -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
|
|
@ -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>
|
|
@ -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…
Reference in New Issue