follow_http_redirects 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Rakefile +1 -0
- data/follow_http_redirects.gemspec +26 -0
- data/lib/follow_http_redirects/base.rb +31 -0
- data/lib/follow_http_redirects/net_http_extensions.rb +11 -0
- data/lib/follow_http_redirects/version.rb +3 -0
- data/lib/follow_http_redirects.rb +6 -0
- data/spec/follow_http_redirects/follow_http_redirects_spec.rb +20 -0
- data/spec/spec_helper.rb +4 -0
- metadata +80 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "follow_http_redirects/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "follow_http_redirects"
|
7
|
+
s.version = FollowHttpRedirects::VERSION
|
8
|
+
s.authors = ["Isak Sky"]
|
9
|
+
s.email = ["isak.sky@gmail.com"]
|
10
|
+
s.homepage = "http://www.github.com/youeye/follow_http_redirects"
|
11
|
+
s.summary = "Get a HTTP response, following redirects."
|
12
|
+
s.description = "This gem adds get_response_following_redirects to Net::HTTP. The method acts like get_response, except it follows redirects."
|
13
|
+
|
14
|
+
s.rubyforge_project = "follow_http_redirects"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
# s.add_development_dependency "rspec"
|
23
|
+
# s.add_runtime_dependency "rest-client"
|
24
|
+
s.add_development_dependency "rspec"
|
25
|
+
s.add_development_dependency "rake"
|
26
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module FollowHttpRedirects
|
2
|
+
def self.get_response_following_redirects orig_uri, requests_limit = 15
|
3
|
+
raise "Input must be an absolute URI." unless orig_uri.is_a?(URI::Generic) && orig_uri.absolute?
|
4
|
+
|
5
|
+
uri = orig_uri
|
6
|
+
requests_made = 0
|
7
|
+
while requests_made <= requests_limit
|
8
|
+
response = Net::HTTP.get_response uri
|
9
|
+
requests_made += 1
|
10
|
+
case response
|
11
|
+
when Net::HTTPSuccess
|
12
|
+
return response
|
13
|
+
when Net::HTTPRedirection
|
14
|
+
redirect_url = response.header['location']
|
15
|
+
unless redirect_url.is_a?(String) && !redirect_url.empty?
|
16
|
+
raise "Redirect Error, no redirect location specified."
|
17
|
+
end
|
18
|
+
|
19
|
+
temp_uri = URI(redirect_url)
|
20
|
+
if temp_uri.relative?
|
21
|
+
uri = uri.merge temp_uri #make URI absolute
|
22
|
+
else
|
23
|
+
uri = temp_uri
|
24
|
+
end
|
25
|
+
else
|
26
|
+
raise "Don't know what to do with this response: #{response.inspect}"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
raise "Too many redirects (#{requests_limit}) error"
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
#Add our method to the Net::HTTP module, so we can use Net:HTTP.get_response_for_real().
|
2
|
+
#Example call:
|
3
|
+
# Net::HTTP.get_response_for_real(URI.parse('http://www.google.com'))
|
4
|
+
module Net
|
5
|
+
class HTTP
|
6
|
+
# get_response_following_redirects
|
7
|
+
def self.get_response_following_redirects orig_uri, requests_limit = 15
|
8
|
+
FollowHttpRedirects.get_response_following_redirects orig_uri, requests_limit
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe "Follow Http Redirects" do
|
4
|
+
describe "Net::HTTP.get_response_for_real" do
|
5
|
+
# NOTE: Note: These tests may fail if server(s) go down. They passed when I wrote them.
|
6
|
+
# That is the nature of these types of tests.
|
7
|
+
['http://www.google.com',
|
8
|
+
'http://www.twitter.com',
|
9
|
+
'http://www.youeye.com',
|
10
|
+
'http://bit.ly/aOBsBN', #Redirects to youeye.com
|
11
|
+
'http://apps.facebook.com/pickoffgame' #triksy url with relative redirect
|
12
|
+
].each do |url_str|
|
13
|
+
it "should be able to get '#{url_str}'." do
|
14
|
+
uri = URI url_str
|
15
|
+
response = Net::HTTP.get_response_following_redirects uri
|
16
|
+
response.should be_a Net::HTTPSuccess
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: follow_http_redirects
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Isak Sky
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-02-18 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &2154133400 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2154133400
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rake
|
27
|
+
requirement: &2154132980 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2154132980
|
36
|
+
description: This gem adds get_response_following_redirects to Net::HTTP. The method
|
37
|
+
acts like get_response, except it follows redirects.
|
38
|
+
email:
|
39
|
+
- isak.sky@gmail.com
|
40
|
+
executables: []
|
41
|
+
extensions: []
|
42
|
+
extra_rdoc_files: []
|
43
|
+
files:
|
44
|
+
- .gitignore
|
45
|
+
- Gemfile
|
46
|
+
- Rakefile
|
47
|
+
- follow_http_redirects.gemspec
|
48
|
+
- lib/follow_http_redirects.rb
|
49
|
+
- lib/follow_http_redirects/base.rb
|
50
|
+
- lib/follow_http_redirects/net_http_extensions.rb
|
51
|
+
- lib/follow_http_redirects/version.rb
|
52
|
+
- spec/follow_http_redirects/follow_http_redirects_spec.rb
|
53
|
+
- spec/spec_helper.rb
|
54
|
+
homepage: http://www.github.com/youeye/follow_http_redirects
|
55
|
+
licenses: []
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ! '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
requirements: []
|
73
|
+
rubyforge_project: follow_http_redirects
|
74
|
+
rubygems_version: 1.8.6
|
75
|
+
signing_key:
|
76
|
+
specification_version: 3
|
77
|
+
summary: Get a HTTP response, following redirects.
|
78
|
+
test_files:
|
79
|
+
- spec/follow_http_redirects/follow_http_redirects_spec.rb
|
80
|
+
- spec/spec_helper.rb
|