unwind 0.9.1 → 0.9.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/unwind/version.rb +1 -1
- data/lib/unwind.rb +17 -9
- data/test/redirect_follower_test.rb +51 -2
- data/unwind.gemspec +1 -1
- data/vcr_cassettes/amazon.yml +1579 -0
- data/vcr_cassettes/missing_redirect.yml +31 -0
- data/vcr_cassettes/pdf.yml +3708 -0
- data/vcr_cassettes/relative_stackoverflow.yml +471 -0
- data/vcr_cassettes/relative_stackoverflow_2.yml +384 -0
- data/vcr_cassettes/ssl_tpope.yml +336 -0
- metadata +24 -7
data/lib/unwind/version.rb
CHANGED
data/lib/unwind.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
require "unwind/version"
|
2
|
-
require '
|
2
|
+
require 'faraday'
|
3
3
|
|
4
4
|
module Unwind
|
5
5
|
|
6
6
|
class TooManyRedirects < StandardError; end
|
7
|
+
class MissingRedirectLocation < StandardError; end
|
7
8
|
|
8
9
|
class RedirectFollower
|
9
10
|
|
@@ -23,20 +24,24 @@ module Unwind
|
|
23
24
|
ok_to_continue?
|
24
25
|
|
25
26
|
current_url ||= self.original_url
|
27
|
+
response = Faraday.get(current_url)
|
26
28
|
|
27
|
-
|
28
|
-
|
29
|
-
if response.kind_of?(Net::HTTPRedirection)
|
30
|
-
@redirects << current_url
|
29
|
+
if [301, 302, 307].include?(response.status)
|
30
|
+
@redirects << current_url.to_s
|
31
31
|
@redirect_limit -= 1
|
32
|
-
resolve
|
32
|
+
resolve redirect_url(response).normalize
|
33
33
|
else
|
34
|
-
@final_url = current_url
|
34
|
+
@final_url = current_url.to_s
|
35
35
|
@response = response
|
36
36
|
self
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
|
+
|
41
|
+
def self.resolve(original_url, limit=5)
|
42
|
+
new(original_url, limit).resolve
|
43
|
+
end
|
44
|
+
|
40
45
|
private
|
41
46
|
|
42
47
|
def ok_to_continue?
|
@@ -45,9 +50,12 @@ module Unwind
|
|
45
50
|
|
46
51
|
def redirect_url(response)
|
47
52
|
if response['location'].nil?
|
48
|
-
response.body.match(/<a href=\"([^>]+)\">/i)
|
53
|
+
body_match = response.body.match(/<a href=\"([^>]+)\">/i)
|
54
|
+
raise MissingRedirectLocation unless body_match
|
55
|
+
Addressable::URI.parse(body_match[0])
|
49
56
|
else
|
50
|
-
response['location']
|
57
|
+
redirect_uri = Addressable::URI.parse(response['location'])
|
58
|
+
redirect_uri.relative? ? response.env[:url].join(response['location']) : redirect_uri
|
51
59
|
end
|
52
60
|
end
|
53
61
|
|
@@ -11,7 +11,7 @@ describe 'Tests :)' do
|
|
11
11
|
|
12
12
|
it 'should resolve the url' do
|
13
13
|
VCR.use_cassette('xZVND1') do
|
14
|
-
follower = Unwind::RedirectFollower.
|
14
|
+
follower = Unwind::RedirectFollower.resolve('http://j.mp/xZVND1')
|
15
15
|
assert_equal 'http://ow.ly/i/s1O0', follower.final_url
|
16
16
|
assert_equal 'http://j.mp/xZVND1', follower.original_url
|
17
17
|
assert_equal 2, follower.redirects.count
|
@@ -19,9 +19,50 @@ describe 'Tests :)' do
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
+
it 'should handle relative redirects' do
|
23
|
+
VCR.use_cassette('relative stackoverflow') do
|
24
|
+
follower = Unwind::RedirectFollower.resolve('http://stackoverflow.com/q/9277007/871617?stw=1')
|
25
|
+
assert follower.redirected?
|
26
|
+
assert_equal 'http://stackoverflow.com/questions/9277007/gitlabhq-w-denied-for-rails', follower.final_url
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should still handine relative redirects' do
|
31
|
+
# http://bit.ly/A4H3a2
|
32
|
+
VCR.use_cassette('relative stackoverflow 2') do
|
33
|
+
follower = Unwind::RedirectFollower.resolve('http://bit.ly/A4H3a2')
|
34
|
+
assert follower.redirected?
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'should handle redirects to pdfs' do
|
39
|
+
VCR.use_cassette('pdf') do
|
40
|
+
follower = Unwind::RedirectFollower.resolve('http://binged.it/wVSFs5')
|
41
|
+
assert follower.redirected?
|
42
|
+
assert_equal 'https://microsoft.promo.eprize.com/bingtwitter/public/fulfillment/rules.pdf', follower.final_url
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should handle the lame amazon spaces' do
|
47
|
+
VCR.use_cassette('amazon') do
|
48
|
+
follower = Unwind::RedirectFollower.resolve('http://amzn.to/xrHQWS')
|
49
|
+
assert follower.redirected?
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
#http://amzn.to/xrHQWS
|
54
|
+
|
55
|
+
it 'should handle a https redirect' do
|
56
|
+
VCR.use_cassette('ssl tpope') do
|
57
|
+
follower = Unwind::RedirectFollower.resolve('http://github.com/tpope/vim-rails')
|
58
|
+
assert follower.redirected?
|
59
|
+
assert_equal 'https://github.com/tpope/vim-rails', follower.final_url
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
22
63
|
it 'should not be redirected' do
|
23
64
|
VCR.use_cassette('no redirect') do
|
24
|
-
follower = Unwind::RedirectFollower.
|
65
|
+
follower = Unwind::RedirectFollower.resolve('http://www.scottw.com')
|
25
66
|
assert !follower.redirected?
|
26
67
|
end
|
27
68
|
end
|
@@ -34,4 +75,12 @@ describe 'Tests :)' do
|
|
34
75
|
end
|
35
76
|
end
|
36
77
|
|
78
|
+
it 'should raise MissingRedirectLocation' do
|
79
|
+
VCR.use_cassette('missing redirect') do
|
80
|
+
follower = Unwind::RedirectFollower.new('http://tinyurl.com/6oqzkff')
|
81
|
+
missing_redirect_location = lambda{follower.resolve}
|
82
|
+
missing_redirect_location.must_raise Unwind::MissingRedirectLocation
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
37
86
|
end
|
data/unwind.gemspec
CHANGED