absolutize 0.0.10 → 0.0.11
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +2 -1
- data/lib/absolutize.rb +21 -13
- data/spec/absolutize/absolutize_spec.rb +21 -6
- metadata +4 -4
data/README.textile
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
|
2
|
-
h1. Absolutize v0.0.
|
2
|
+
h1. Absolutize v0.0.11
|
3
3
|
|
4
4
|
h2. Intro
|
5
5
|
|
@@ -23,6 +23,7 @@ Creates a new absolutize object based on a base_url
|
|
23
23
|
** :remove_anchors - removes any anchor tags (false)
|
24
24
|
** :force_escaping - forces escaping of urls if it hasn't been done already (true)
|
25
25
|
** :output_debug - puts debug info while processing the url (false)
|
26
|
+
** :raise_exceptions - raises exceptions when it can't parse a url, otherwise returns passed in value (false)
|
26
27
|
|
27
28
|
bq. absolutizer = Absolutize.new("http://www.vamosa.com/mainfolder/", :remove_anchors => true)
|
28
29
|
|
data/lib/absolutize.rb
CHANGED
@@ -11,6 +11,8 @@ class Absolutize
|
|
11
11
|
@options[:remove_anchors] = false if @options[:remove_anchors].nil?
|
12
12
|
@options[:force_escaping] = true if @options[:force_escaping].nil?
|
13
13
|
@options[:output_debug] = false if @options[:output_debug].nil?
|
14
|
+
@options[:raise_exceptions] = false if @options[:output_debug].nil?
|
15
|
+
|
14
16
|
end
|
15
17
|
|
16
18
|
def url(relative_url)
|
@@ -25,23 +27,29 @@ class Absolutize
|
|
25
27
|
begin
|
26
28
|
absolute_url = URI.join(@base_url, relative_url)
|
27
29
|
rescue URI::InvalidURIError => uri
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
30
|
+
begin
|
31
|
+
puts "Unable to use URI.join attempting manually" if @options[:output_debug]
|
32
|
+
if @base_url =~ /\Ahttp/ and relative_url =~ /\A\//
|
33
|
+
puts "base url starts with http and relative_url is relative to root" if @options[:output_debug]
|
34
|
+
uri = URI.parse(@base_url)
|
35
|
+
if uri.port
|
36
|
+
absolute_url = URI.parse("#{uri.scheme}://#{uri.host}:#{uri.port}#{relative_url}")
|
37
|
+
else
|
38
|
+
absolute_url = URI.parse("#{uri.scheme}://#{uri.host}#{relative_url}")
|
39
|
+
end
|
40
|
+
elsif relative_url =~ /\Ahttp/
|
41
|
+
#new url is absolute anyway
|
42
|
+
absolute_url = URI.parse(relative_url)
|
34
43
|
else
|
35
|
-
|
44
|
+
raise "Unable to absolutize #{@base_url} and #{relative_url}" if @options[:raise_exceptions]
|
45
|
+
absolute_url = relative_url
|
36
46
|
end
|
37
|
-
|
38
|
-
#
|
39
|
-
|
40
|
-
|
41
|
-
raise "Unable to absolutize #{@base_url} and #{relative_url}"
|
47
|
+
rescue URI::InvalidURIError => uri_2
|
48
|
+
# ok, givin it a fair go trying to get this url, raise an exception if the option is set otherwise give up and return original relative url
|
49
|
+
raise "Unable to absolutize #{@base_url} and #{relative_url}" if @options[:raise_exceptions]
|
50
|
+
absolute_url = relative_url
|
42
51
|
end
|
43
52
|
end
|
44
|
-
|
45
53
|
absolute_url
|
46
54
|
end
|
47
55
|
end
|
@@ -49,6 +49,9 @@ describe Absolutize do
|
|
49
49
|
it "should encode with pipes" do
|
50
50
|
@absolutize.url("/root%20folder/asdf.html?lang=en|us").to_s.should == "http://www.baseurl.com/root%20folder/asdf.html?lang=en%7Cus"
|
51
51
|
end
|
52
|
+
it "should not raise an exception with really bad url" do
|
53
|
+
@absolutize.url("http://.asdf.com/").to_s.should == "http://.asdf.com/"
|
54
|
+
end
|
52
55
|
end
|
53
56
|
|
54
57
|
describe "with remove_anchors true" do
|
@@ -65,15 +68,27 @@ describe Absolutize do
|
|
65
68
|
@absolutize = Absolutize.new(@base_url, :force_escaping => false)
|
66
69
|
end
|
67
70
|
it "should not escape invalid characters" do
|
68
|
-
lambda { @absolutize.url("/root folder/asdf.html#anchor")}.should raise_error
|
69
|
-
|
70
71
|
ab = Absolutize.new("http://www.baseurl.com/top_folder/index.html", :force_escaping => false)
|
71
|
-
|
72
|
+
|
73
|
+
#this example should be decoded first, but we are checking that when force_escaping is off it does not
|
72
74
|
ab.url("%2ftop_folder%2fsub_folder%2findex.html").to_s.should == "http://www.baseurl.com/top_folder/%2ftop_folder%2fsub_folder%2findex.html"
|
73
|
-
#should work fine
|
74
75
|
ab.url("/top_folder/sub_folder/index.html").to_s.should == "http://www.baseurl.com/top_folder/sub_folder/index.html"
|
75
|
-
|
76
76
|
end
|
77
|
-
|
77
|
+
end
|
78
|
+
|
79
|
+
describe "with raise_exceptions true" do
|
80
|
+
before(:each) do
|
81
|
+
@absolutize = Absolutize.new(@base_url, :raise_exceptions => true)
|
82
|
+
end
|
83
|
+
it "should raise an exception when absolutize can't fix a url" do
|
84
|
+
lambda { @absolutize.url("http://.asdf.com/root folder/asdf.html#anchor")}.should raise_error
|
85
|
+
end
|
86
|
+
it "should not raise an exception when a valid url is given" do
|
87
|
+
lambda { @absolutize.url("http://www.asdf.com/rootfolder/asdf.html#anchor")}.should_not raise_error
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should not raise an exception when a fixable url is given" do
|
91
|
+
lambda { @absolutize.url("http://www.asdf.com/root folder[1]/asdf.html#anchor")}.should_not raise_error
|
92
|
+
end
|
78
93
|
end
|
79
94
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: absolutize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 9
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 11
|
10
|
+
version: 0.0.11
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Stewart McKee
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-08-
|
18
|
+
date: 2010-08-23 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|