Permalink
Browse files

Merge pull request #321 from tijmenb/fix-hosts-in-oneboxer

Fix display hosts in oneboxer
  • Loading branch information...
2 parents 8c914e8 + ba04e72 commit 35d5c899ddfd879966bd89e21561551dd1764109 @eviltrout eviltrout committed Mar 4, 2013
@@ -43,6 +43,11 @@ def translate_url
@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
@@ -32,12 +32,7 @@ def onebox
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
@@ -33,13 +33,7 @@ def onebox
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)
@@ -21,16 +21,9 @@ def onebox
@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
@@ -21,7 +21,7 @@ def expected_android_app_store_result
<div class='source'>
<div class='info'>
<a href='https://play.google.com/store/apps/details?id=com.moosoft.parrot' target="_blank">
- <img class='favicon' src="/assets/favicons/google_play.png"> google.com
+ <img class='favicon' src="/assets/favicons/google_play.png"> play.google.com
</a>
</div>
</div>
@@ -21,7 +21,7 @@ def expected_apple_app_result
<div class='source'>
<div class='info'>
<a href='https://itunes.apple.com/us/app/minecraft-pocket-edition-lite/id479651754' target="_blank">
- <img class='favicon' src="/assets/favicons/apple.png"> apple.com
+ <img class='favicon' src="/assets/favicons/apple.png"> itunes.apple.com
</a>
</div>
</div>
@@ -22,7 +22,7 @@ def expected_wikipedia_result
<div class='source'>
<div class='info'>
<a href='http://en.wikipedia.org/wiki/Ruby' target="_blank">
- <img class='favicon' src="/assets/favicons/wikipedia.png"> wikipedia.org
+ <img class='favicon' src="/assets/favicons/wikipedia.png"> en.wikipedia.org
</a>
</div>
</div>
@@ -17,21 +17,21 @@ def onebox
@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
@@ -72,6 +72,24 @@ def onebox
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!'
@@ -163,5 +181,3 @@ def onebox
end
-
-

0 comments on commit 35d5c89

Please sign in to comment.