Skip to content

Commit 673b25a

Browse files
committed
Expand shortened urls, fails on https
Stub expand url call in test
1 parent 8cd5936 commit 673b25a

4 files changed

Lines changed: 82 additions & 4 deletions

File tree

app/presenters/tweet/html_presenter.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,16 @@ def text
1414
buffer << tweet.tweet_text
1515

1616
# make links anchors
17-
buffer.gsub! /(https?[^\s]+)/o,
18-
%q(<a href="\\1" target="_blank">\\1</a>)
17+
buffer = buffer.gsub(/(https?[^\s]+)/o) do |url|
18+
require Rails.root.join('lib/expand_url')
19+
begin
20+
expanded_url = ExpandUrl.expand_url(url)
21+
rescue ExpandUrl::ExpansionError => e
22+
STDERR.puts "#{e.class}: failed expanding #{url.inspect}"
23+
expanded_url = url
24+
end
25+
%Q(<a href="#{expanded_url}" target="_blank">#{url}</a>)
26+
end
1927
# link hashtags
2028
buffer.gsub! /#(\w+)/, '<a href="http://twitter.com/search?q=%23\\1">#\\1</a>'
2129
# link users

lib/expand_url.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
require 'net/http'
2+
require 'uri'
3+
4+
# e.g.
5+
# url = http://t.co/z4t0E1vArh
6+
# ExpandUrl.expand_url(url)
7+
# => "http://www.haaretz.com/news/national/israel-s-ag-impels-ministers-to-crack-down-on-exclusion-of-women.premium-1.519917"
8+
module ExpandUrl
9+
class ExpansionError < StandardError; end
10+
module ExpansionErrors
11+
class BadUrl < ExpansionError; end
12+
class BadResponse < ExpansionError; end
13+
end
14+
HTTP_ERRORS = [Timeout::Error, Errno::EINVAL, Errno::ECONNRESET,
15+
Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError]
16+
class BasicResponse < Struct.new(:url, :code); end
17+
extend self
18+
19+
# raises ExpandUrl::ExpansionError
20+
def expand_url(url)
21+
response = get_response(url)
22+
case response.code
23+
when '301'
24+
log "url: #{url}\tresponse: #{response.inspect}"
25+
expand_url(response['location'])
26+
when '200'
27+
log "url: #{url}\tresponse: #{response.inspect}"
28+
url
29+
else
30+
log "url: #{url}\tresponse: #{response.inspect}"
31+
expand_url(response['location'])
32+
end
33+
end
34+
35+
def get_response(url)
36+
uri = url_to_uri(url)
37+
Net::HTTP.get_response(uri)
38+
rescue EOFError => e
39+
BasicResponse.new(url, '200')
40+
rescue *HTTP_ERRORS => e
41+
log url.inspect + e.inspect
42+
raise ExpansionErrors::BadResponse.new(e)
43+
end
44+
45+
def url_to_uri(url)
46+
begin
47+
uri = URI.parse(url)
48+
rescue URI::InvalidURIError, SocketError => e
49+
raise ExpansionErrors::BadUrl.new(e)
50+
end
51+
end
52+
53+
def log(msg)
54+
STDOUT.puts "#{msg}\t#{caller[1]}" if (ENV['debug'] == 'true')
55+
end
56+
57+
end

spec/lib/expand_url_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require 'spec_helper'
2+
3+
describe ExpandUrl do
4+
5+
it 'should be tested'
6+
7+
end

spec/presenters/tweet/html_presenter_spec.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@
44
subject { presenter }
55

66
let(:presenter) { described_class.new tweet }
7+
let(:tweet_link) {
8+
link = 'http://t.co/cyL9StoS'
9+
ExpandUrl.stub(:expand_url).with(link).and_return(link)
10+
link
11+
}
712
let(:tweet) {
8-
Tweet.new tweet_id: "263515718753079296",
9-
tweet_text: "at @SteelCityRuby with @coreyhaines - one of my favorite #rubyfriends http://t.co/cyL9StoS",
13+
Tweet.new(tweet_id: "263515718753079296",
14+
tweet_text: "at @SteelCityRuby with @coreyhaines - one of my favorite #rubyfriends #{tweet_link}",
1015
username: "joshsusser",
1116
media_url: "http://p.twimg.com/A6gyJmlCUAA9Il2.jpg",
1217
image: "A6gyJmlCUAA9Il2.jpg",
1318
media_display_url: "pic.twitter.com/cyL9StoS"
19+
)
1420
}
1521

1622
its(:username) { should == "@joshsusser" }

0 commit comments

Comments
 (0)