redirect_follower 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +44 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/lib/redirect_follower.rb +67 -0
- data/redirect_follower.gemspec +54 -0
- data/test/helper.rb +10 -0
- data/test/test_redirect_follower.rb +68 -0
- metadata +75 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Christoph Olszowka
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
= RedirectFollower
|
2
|
+
|
3
|
+
Allows you to resolve redirecting urls (i.e. from url-shortening services like bit.ly) and find
|
4
|
+
out their destination url as well as the body.
|
5
|
+
|
6
|
+
The code is mostly ripped from John Nunemaker's blog post "Following Redirects with Net/HTTP"
|
7
|
+
(http://railstips.org/blog/archives/2009/03/04/following-redirects-with-nethttp/), but wrapped
|
8
|
+
into a Rubygem
|
9
|
+
|
10
|
+
Install with:
|
11
|
+
|
12
|
+
gem install redirect_follower
|
13
|
+
|
14
|
+
Then, either shorthand for the destination url only or for the full response:
|
15
|
+
|
16
|
+
$ irb
|
17
|
+
> require 'redirect_follower'
|
18
|
+
> RedirectFollower('http://is.gd/bNZYZ') # Just find out the destination url
|
19
|
+
=> 'http://www.taschen.com/pages/en/catalogue/classics/all/06724/facts.byrne_six_books_of_euclid.htm'
|
20
|
+
|
21
|
+
> redirect = RedirectFollower.new('http://is.gd/bNZYZ')
|
22
|
+
> redirect.url
|
23
|
+
=> 'http://www.taschen.com/pages/en/catalogue/classics/all/06724/facts.byrne_six_books_of_euclid.htm'
|
24
|
+
> redirect.body
|
25
|
+
=> 'a load of html...'
|
26
|
+
> redirect.response.body
|
27
|
+
=> 'a load of html...' # Same as redirect.body
|
28
|
+
> redirect.response
|
29
|
+
=> # Full response object from Net::HTTP
|
30
|
+
|
31
|
+
|
32
|
+
== Note on Patches/Pull Requests
|
33
|
+
|
34
|
+
* Fork the project.
|
35
|
+
* Make your feature addition or bug fix.
|
36
|
+
* Add tests for it. This is important so I don't break it in a
|
37
|
+
future version unintentionally.
|
38
|
+
* Commit, do not mess with rakefile, version, or history.
|
39
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
40
|
+
* Send me a pull request. Bonus points for topic branches.
|
41
|
+
|
42
|
+
== Copyright
|
43
|
+
|
44
|
+
Copyright (c) 2010 Christoph Olszowka. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "redirect_follower"
|
8
|
+
gem.summary = %Q{Find out the destination of redirecting urls with Net::HTTP}
|
9
|
+
gem.description = %Q{Allows you to resolve redirecting urls (i.e. from url-shortening services like bit.ly) and find out their destination url as well as the body.}
|
10
|
+
gem.email = "christoph at olszowka de"
|
11
|
+
gem.homepage = "http://github.com/colszowka/redirect_follower"
|
12
|
+
gem.authors = ["Christoph Olszowka"]
|
13
|
+
gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'rake/testtask'
|
22
|
+
Rake::TestTask.new(:test) do |test|
|
23
|
+
test.libs << 'lib' << 'test'
|
24
|
+
test.pattern = 'test/**/test_*.rb'
|
25
|
+
test.verbose = true
|
26
|
+
end
|
27
|
+
|
28
|
+
begin
|
29
|
+
require 'rcov/rcovtask'
|
30
|
+
Rcov::RcovTask.new do |test|
|
31
|
+
test.libs << 'test'
|
32
|
+
test.pattern = 'test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
rescue LoadError
|
36
|
+
task :rcov do
|
37
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
task :test => :check_dependencies
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "redirect_follower #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
|
3
|
+
#
|
4
|
+
# Shorthand for RedirectFollower.new(url).resolve.url - just get the
|
5
|
+
# destination url for given url
|
6
|
+
#
|
7
|
+
def RedirectFollower(url)
|
8
|
+
RedirectFollower.new(url).url
|
9
|
+
end
|
10
|
+
|
11
|
+
#
|
12
|
+
# Follow redirects for given urls with net http to find out their destinations
|
13
|
+
#
|
14
|
+
# Source code mostly ripped from John Nunemaker:
|
15
|
+
# http://railstips.org/blog/archives/2009/03/04/following-redirects-with-nethttp/
|
16
|
+
#
|
17
|
+
class RedirectFollower
|
18
|
+
class TooManyRedirects < StandardError; end
|
19
|
+
|
20
|
+
attr_accessor :original_url, :redirect_limit
|
21
|
+
attr_writer :url, :response
|
22
|
+
|
23
|
+
def initialize(original_url, limit=5)
|
24
|
+
@original_url, @redirect_limit = original_url, limit
|
25
|
+
end
|
26
|
+
|
27
|
+
def url
|
28
|
+
resolve unless @url
|
29
|
+
@url
|
30
|
+
end
|
31
|
+
|
32
|
+
def body
|
33
|
+
response.body
|
34
|
+
end
|
35
|
+
|
36
|
+
def response
|
37
|
+
resolve unless @response
|
38
|
+
@response
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def resolve
|
44
|
+
raise TooManyRedirects if redirect_limit < 0
|
45
|
+
|
46
|
+
# Set up current url if not resolved yet
|
47
|
+
self.url = original_url unless @url
|
48
|
+
|
49
|
+
self.response = Net::HTTP.get_response(URI.parse(url))
|
50
|
+
|
51
|
+
if response.kind_of?(Net::HTTPRedirection)
|
52
|
+
self.url = redirect_url
|
53
|
+
self.redirect_limit -= 1
|
54
|
+
resolve
|
55
|
+
end
|
56
|
+
|
57
|
+
self
|
58
|
+
end
|
59
|
+
|
60
|
+
def redirect_url
|
61
|
+
if response['location'].nil?
|
62
|
+
response.body.match(/<a href=\"([^>]+)\">/i)[1]
|
63
|
+
else
|
64
|
+
response['location']
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{redirect_follower}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Christoph Olszowka"]
|
12
|
+
s.date = %q{2010-04-30}
|
13
|
+
s.description = %q{Allows you to resolve redirecting urls (i.e. from url-shortening services like bit.ly) and find out their destination url as well as the body.}
|
14
|
+
s.email = %q{christoph at olszowka de}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"lib/redirect_follower.rb",
|
27
|
+
"redirect_follower.gemspec",
|
28
|
+
"test/helper.rb",
|
29
|
+
"test/test_redirect_follower.rb"
|
30
|
+
]
|
31
|
+
s.homepage = %q{http://github.com/colszowka/redirect_follower}
|
32
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
33
|
+
s.require_paths = ["lib"]
|
34
|
+
s.rubygems_version = %q{1.3.5}
|
35
|
+
s.summary = %q{Find out the destination of redirecting urls with Net::HTTP}
|
36
|
+
s.test_files = [
|
37
|
+
"test/test_redirect_follower.rb",
|
38
|
+
"test/helper.rb"
|
39
|
+
]
|
40
|
+
|
41
|
+
if s.respond_to? :specification_version then
|
42
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
43
|
+
s.specification_version = 3
|
44
|
+
|
45
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
46
|
+
s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
47
|
+
else
|
48
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
49
|
+
end
|
50
|
+
else
|
51
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
data/test/helper.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestRedirectFollower < Test::Unit::TestCase
|
4
|
+
should "return the github url for RedirectFollower('http://bit.ly/cteFsP')" do
|
5
|
+
assert_equal 'http://github.com/colszowka/redirect_follower', RedirectFollower('http://bit.ly/cteFsP')
|
6
|
+
end
|
7
|
+
|
8
|
+
should "return the github url minus the subdomain for RedirectFollower('http://bit.ly/cteFsP')" do
|
9
|
+
assert_equal 'http://github.com/colszowka/redirect_follower', RedirectFollower('http://www.github.com/colszowka/redirect_follower')
|
10
|
+
end
|
11
|
+
|
12
|
+
context "A new RedirectFollower instance initialized with 'http://bit.ly/cteFsP'" do
|
13
|
+
setup { @redirect = RedirectFollower.new('http://bit.ly/cteFsP')}
|
14
|
+
|
15
|
+
should "return the github url for url" do
|
16
|
+
assert_equal 'http://github.com/colszowka/redirect_follower', @redirect.url
|
17
|
+
end
|
18
|
+
|
19
|
+
should "return some html for body" do
|
20
|
+
assert_match /\<div/, @redirect.body
|
21
|
+
end
|
22
|
+
|
23
|
+
should "have body equal to response.body" do
|
24
|
+
assert_equal @redirect.response.body, @redirect.body
|
25
|
+
end
|
26
|
+
|
27
|
+
should "have response kind of HTTPSuccess" do
|
28
|
+
assert @redirect.response.kind_of?(Net::HTTPSuccess)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context "A new RedirectFollower instance initialized with 'http://bit.ly/aRmOtk' and a limit of 1" do
|
33
|
+
setup { @redirect = RedirectFollower.new('http://bit.ly/aRmOtk', 1)}
|
34
|
+
|
35
|
+
should "raise RedirectFollower::TooManyRedirects when requesting url" do
|
36
|
+
assert_raise(RedirectFollower::TooManyRedirects) { @redirect.url }
|
37
|
+
end
|
38
|
+
|
39
|
+
should "raise RedirectFollower::TooManyRedirects when requesting body" do
|
40
|
+
assert_raise(RedirectFollower::TooManyRedirects) { @redirect.body }
|
41
|
+
end
|
42
|
+
|
43
|
+
should "raise RedirectFollower::TooManyRedirects when requesting response" do
|
44
|
+
assert_raise(RedirectFollower::TooManyRedirects) { @redirect.response }
|
45
|
+
end
|
46
|
+
|
47
|
+
context "after modifiying the limit to 2" do
|
48
|
+
setup { @redirect.redirect_limit = 2 }
|
49
|
+
|
50
|
+
should "return the github url for url" do
|
51
|
+
assert_equal 'http://github.com/colszowka/redirect_follower', @redirect.url
|
52
|
+
end
|
53
|
+
|
54
|
+
should "return some html for body" do
|
55
|
+
assert_match /\<div/, @redirect.body
|
56
|
+
end
|
57
|
+
|
58
|
+
should "have body equal to response.body" do
|
59
|
+
assert_equal @redirect.response.body, @redirect.body
|
60
|
+
end
|
61
|
+
|
62
|
+
should "have response kind of HTTPSuccess" do
|
63
|
+
assert @redirect.response.kind_of?(Net::HTTPSuccess)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: redirect_follower
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Christoph Olszowka
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-04-30 00:00:00 +02:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: thoughtbot-shoulda
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description: Allows you to resolve redirecting urls (i.e. from url-shortening services like bit.ly) and find out their destination url as well as the body.
|
26
|
+
email: christoph at olszowka de
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .document
|
36
|
+
- .gitignore
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- VERSION
|
41
|
+
- lib/redirect_follower.rb
|
42
|
+
- redirect_follower.gemspec
|
43
|
+
- test/helper.rb
|
44
|
+
- test/test_redirect_follower.rb
|
45
|
+
has_rdoc: true
|
46
|
+
homepage: http://github.com/colszowka/redirect_follower
|
47
|
+
licenses: []
|
48
|
+
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options:
|
51
|
+
- --charset=UTF-8
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
version:
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
version:
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.3.5
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Find out the destination of redirecting urls with Net::HTTP
|
73
|
+
test_files:
|
74
|
+
- test/test_redirect_follower.rb
|
75
|
+
- test/helper.rb
|