From 1fb0b424ed62a38120419abec93050fce6fc540a Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Mon, 10 Jun 2013 09:27:13 -0700 Subject: [PATCH] Add Stack Exchange onebox --- app/assets/images/favicons/stackexchange.png | Bin 0 -> 1438 bytes lib/oneboxer/stack_exchange_onebox.rb | 46 ++++++++++++++++ .../templates/stack_exchange_onebox.hbrs | 38 +++++++++++++ .../oneboxer/stack_exchange_onebox_spec.rb | 52 ++++++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 app/assets/images/favicons/stackexchange.png create mode 100644 lib/oneboxer/stack_exchange_onebox.rb create mode 100644 lib/oneboxer/templates/stack_exchange_onebox.hbrs create mode 100644 spec/components/oneboxer/stack_exchange_onebox_spec.rb diff --git a/app/assets/images/favicons/stackexchange.png b/app/assets/images/favicons/stackexchange.png new file mode 100644 index 0000000000000000000000000000000000000000..91e4dbbe4b5f9d11f558519529db543ba59d1ffd GIT binary patch literal 1438 zcmV;P1!4M$P)4Tx05}naRo`#hR1`jmZ&IWdKOk5~hl<6oRa0BJ8yc;~21%2p?MfD<>DVeH z9(p*dx19w`~g7O0}n_%Aq@s%d)fBDv`JHkDym6Hd+5XuAtvnwRpGmK zVkc9?T=n|PIo~X-eVh__(Z?q}P9Z-Dj?gOW6|D%o20XmjW-qs4UjrD(li^iv8@eK9k+ZFm zVRFymFOPAzG5-%Pn|1W;U4vNroTa&AxDScmEA~{ri9gr1^c?U@uwSpaNnw8l_>cP1 zd;)kMQS_;jeRSUEM_*s96y65j1$)tOrwdK{YIQMt92l|D^(E_=$Rjw{b!QT@q!)ni zR`|5oW9X5n$Wv+HVc@|^eX5yXnsHX8PF3UX~a6)MwxDE0HaPjyrlI!;jX{6Kvuh*8ej?;85ekN$?5uuCiS zBTvvVG+XTxAO{m@bvM#Jr)z6J><&E22D|vq?Y?Vkbo_DijopiF$2PET#mZ8eu=y$(ArYkv7@Ex`GL?QCc!_*KFrd&;n1r7 zqW-CFs9&fT)ZaU5gc&=gBz-DaCw(vdOp0__x+47~U6sC(E(JNe@4cTT*n6*E zVH4eoU1-&7pEV~_PRe`a7v+@vy!^5}8?Y3)UmlaER0003sNkliwksH3c>LlA!?Ty)ku8L?pRKgT?*)9C;ido?)boOW{?aGR%*@h{pS@#v`s_Wj zB^d1g4FA9UefL-L)s^}$@K|7EWMn5sGsHAT&Yye?Y#=r~9o&ENHtE%?Pc=Y4F(a&G z-YLmVgo07mG4yve3Ke*gdg07*qoM6N<$f\w*)\.)?(?#{DOMAINS.join('|')})\.com\/(?:questions|q)\/(?\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 diff --git a/lib/oneboxer/templates/stack_exchange_onebox.hbrs b/lib/oneboxer/templates/stack_exchange_onebox.hbrs new file mode 100644 index 00000000000..57d5946e4e5 --- /dev/null +++ b/lib/oneboxer/templates/stack_exchange_onebox.hbrs @@ -0,0 +1,38 @@ +
+ {{#host}} + + {{/host}} + +
+ {{#owner.profile_image}} + + {{owner.display_name}} + + {{/owner.profile_image}} + +

+ {{{title}}} +

+ + + +
+ {{tags}} +
+
+
+
diff --git a/spec/components/oneboxer/stack_exchange_onebox_spec.rb b/spec/components/oneboxer/stack_exchange_onebox_spec.rb new file mode 100644 index 00000000000..3a4dbd9a74c --- /dev/null +++ b/spec/components/oneboxer/stack_exchange_onebox_spec.rb @@ -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