From eaef66a4231ab19ef69affb1f8c56854b2203fa6 Mon Sep 17 00:00:00 2001 From: Tijmen Brommet Date: Sat, 2 Mar 2013 00:46:55 +0100 Subject: [PATCH 1/2] Fix display host for onebox --- lib/oneboxer/base_onebox.rb | 7 ++++++- lib/oneboxer/handlebars_onebox.rb | 7 +------ lib/oneboxer/oembed_onebox.rb | 8 +------- lib/oneboxer/open_graph_onebox.rb | 9 +-------- .../oneboxer/android_app_store_onebox_spec.rb | 2 +- .../oneboxer/apple_app_onebox_spec.rb | 2 +- .../oneboxer/wikipedia_onebox_spec.rb | 2 +- spec/components/oneboxer_spec.rb | 18 ++++++++++++++++++ 8 files changed, 30 insertions(+), 25 deletions(-) diff --git a/lib/oneboxer/base_onebox.rb b/lib/oneboxer/base_onebox.rb index 74da125987e..f0d4c3e53b3 100644 --- a/lib/oneboxer/base_onebox.rb +++ b/lib/oneboxer/base_onebox.rb @@ -43,6 +43,11 @@ module Oneboxer @url end + def nice_host + host = URI.parse(@url).host + host.nil? ? '' : host.gsub('www.', '') + rescue URI::InvalidURIError + '' # In case there is a problem with the URL, we just won't set the host + end end - end diff --git a/lib/oneboxer/handlebars_onebox.rb b/lib/oneboxer/handlebars_onebox.rb index 48047b2a0b9..f000a500aa9 100644 --- a/lib/oneboxer/handlebars_onebox.rb +++ b/lib/oneboxer/handlebars_onebox.rb @@ -32,12 +32,7 @@ module Oneboxer args[:original_url] = @url args[:lang] = @lang || "" args[:favicon] = ActionController::Base.helpers.image_path(self.class.favicon_file) if self.class.favicon_file.present? - begin - parsed = URI.parse(@url) - args[:host] = parsed.host.split('.').last(2).join('.') - rescue URI::InvalidURIError - # In case there is a problem with the URL, we just won't set the host - end + args[:host] = nice_host Mustache.render(File.read(template), args) rescue => ex diff --git a/lib/oneboxer/oembed_onebox.rb b/lib/oneboxer/oembed_onebox.rb index d0de7919581..3ddb15eaba2 100644 --- a/lib/oneboxer/oembed_onebox.rb +++ b/lib/oneboxer/oembed_onebox.rb @@ -33,13 +33,7 @@ module Oneboxer end parsed['html'] ||= parsed['abstract'] - - begin - parsed_uri = URI.parse(@url) - parsed['host'] = parsed_uri.host.split('.').last(2).join('.') - rescue URI::InvalidURIError - # In case there is a problem with the URL, we just won't set the host - end + parsed['host'] = nice_host Mustache.render(File.read(template), parsed) diff --git a/lib/oneboxer/open_graph_onebox.rb b/lib/oneboxer/open_graph_onebox.rb index 53e4a4349ff..30ca1274ec3 100644 --- a/lib/oneboxer/open_graph_onebox.rb +++ b/lib/oneboxer/open_graph_onebox.rb @@ -21,16 +21,9 @@ module Oneboxer @opts[:original_url] = @url @opts[:text] = @opts['description'] @opts[:unsafe] = true - - begin - parsed = URI.parse(@url) - @opts[:host] = parsed.host.split('.').last(2).join('.') - rescue URI::InvalidURIError - # In case there is a problem with the URL, we just won't set the host - end + @opts[:host] = nice_host Mustache.render(File.read(template), @opts) end - end end diff --git a/spec/components/oneboxer/android_app_store_onebox_spec.rb b/spec/components/oneboxer/android_app_store_onebox_spec.rb index fca6cf19160..ec4cd66ccaa 100644 --- a/spec/components/oneboxer/android_app_store_onebox_spec.rb +++ b/spec/components/oneboxer/android_app_store_onebox_spec.rb @@ -21,7 +21,7 @@ private
diff --git a/spec/components/oneboxer/apple_app_onebox_spec.rb b/spec/components/oneboxer/apple_app_onebox_spec.rb index e1ab1f0a858..5cb50cbb26d 100644 --- a/spec/components/oneboxer/apple_app_onebox_spec.rb +++ b/spec/components/oneboxer/apple_app_onebox_spec.rb @@ -21,7 +21,7 @@ private
diff --git a/spec/components/oneboxer/wikipedia_onebox_spec.rb b/spec/components/oneboxer/wikipedia_onebox_spec.rb index 75bd6cc0599..b27aa7b718c 100644 --- a/spec/components/oneboxer/wikipedia_onebox_spec.rb +++ b/spec/components/oneboxer/wikipedia_onebox_spec.rb @@ -22,7 +22,7 @@ private
diff --git a/spec/components/oneboxer_spec.rb b/spec/components/oneboxer_spec.rb index 5ddec3f29df..d085aa31e6e 100644 --- a/spec/components/oneboxer_spec.rb +++ b/spec/components/oneboxer_spec.rb @@ -72,6 +72,24 @@ describe Oneboxer do end + describe '#nice_host' do + it 'strips www from the domain' do + DummyOnebox.new('http://www.cnn.com/thing').nice_host.should eq 'cnn.com' + end + + it 'respects double TLDs' do + DummyOnebox.new('http://news.bbc.co.uk/thing').nice_host.should eq 'news.bbc.co.uk' + end + + it 'returns an empty string if the URL is bogus' do + DummyOnebox.new('whatever').nice_host.should eq '' + end + + it 'returns an empty string if the URL unparsable' do + DummyOnebox.new(nil).nice_host.should eq '' + end + end + context 'without caching' do it 'calls the onebox method of our matched class' do Oneboxer.onebox_nocache(@dummy_onebox_url).should == 'dummy!' From ba04e72a994af2b75e745ec754484d2308811a1f Mon Sep 17 00:00:00 2001 From: Tijmen Brommet Date: Sat, 2 Mar 2013 00:48:49 +0100 Subject: [PATCH 2/2] Fix weirdly indented code --- spec/components/oneboxer_spec.rb | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/spec/components/oneboxer_spec.rb b/spec/components/oneboxer_spec.rb index d085aa31e6e..b2852bd98de 100644 --- a/spec/components/oneboxer_spec.rb +++ b/spec/components/oneboxer_spec.rb @@ -17,21 +17,21 @@ describe "Dynamic Oneboxer" do @dummy_onebox_url = "http://dummy2.localhost/dummy-object" end - context 'find onebox for url' do + context 'find onebox for url' do - it 'returns blank with an unknown url' do - Oneboxer.onebox_for_url('http://asdfasdfasdfasdf.asdf').should be_blank - end + it 'returns blank with an unknown url' do + Oneboxer.onebox_for_url('http://asdfasdfasdfasdf.asdf').should be_blank + end - it 'returns something when matched' do - Oneboxer.onebox_for_url(@dummy_onebox_url).should be_present - end + it 'returns something when matched' do + Oneboxer.onebox_for_url(@dummy_onebox_url).should be_present + end - it 'returns an instance of our class when matched' do - Oneboxer.onebox_for_url(@dummy_onebox_url).kind_of?(DummyDynamicOnebox).should be_true - end + it 'returns an instance of our class when matched' do + Oneboxer.onebox_for_url(@dummy_onebox_url).kind_of?(DummyDynamicOnebox).should be_true + end - end + end end @@ -181,5 +181,3 @@ describe Oneboxer do end - -