absolutize 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +1 -1
- data/lib/absolutize.rb +17 -11
- data/lib/absolutize.rb~ +17 -11
- data/spec/absolutize/absolutize_spec.rb +15 -0
- data/spec/absolutize/absolutize_spec.rb~ +13 -0
- metadata +3 -3
data/README.textile
CHANGED
data/lib/absolutize.rb
CHANGED
@@ -13,29 +13,35 @@ class Absolutize
|
|
13
13
|
@options[:output_debug] = false
|
14
14
|
end
|
15
15
|
|
16
|
-
def url(
|
16
|
+
def url(relative_url)
|
17
17
|
# encode the url if the new url contains spaces but doesn't contain %, i.e isn't already encoded
|
18
|
-
|
18
|
+
relative_url = relative_url.split("#").first if relative_url.include?"#" and @options[:remove_anchors]
|
19
|
+
relative_url = URI.encode(relative_url, " <>\{\}|\\\^\[\]`") if @options[:force_escaping]
|
19
20
|
|
20
|
-
|
21
|
+
absolute_url = nil
|
21
22
|
begin
|
22
|
-
URI.join(@base_url,
|
23
|
+
absolute_url = URI.join(@base_url, relative_url)
|
23
24
|
rescue URI::InvalidURIError => urie
|
24
25
|
puts "Unable to use URI.join attempting manually" if @options[:output_debug]
|
25
|
-
if @base_url =~ /\Ahttp/ and
|
26
|
-
puts "base url starts with
|
26
|
+
if @base_url =~ /\Ahttp/ and relative_url =~ /\A\//
|
27
|
+
puts "base url starts with http and relative_url is relative to root" if @options[:output_debug]
|
27
28
|
uri = URI.parse(@base_url)
|
28
29
|
if uri.port
|
29
|
-
"#{uri.scheme}://#{uri.host}:#{uri.port}#{
|
30
|
+
absolute_url = URI.parse("#{uri.scheme}://#{uri.host}:#{uri.port}#{relative_url}")
|
30
31
|
else
|
31
|
-
"#{uri.scheme}://#{uri.host}#{
|
32
|
+
absolute_url = URI.parse("#{uri.scheme}://#{uri.host}#{relative_url}")
|
32
33
|
end
|
33
|
-
elsif
|
34
|
+
elsif relative_url =~ /\Ahttp/
|
34
35
|
#new url is absolute anyway
|
35
|
-
|
36
|
+
absolute_url = URI.parse(relative_url)
|
36
37
|
else
|
37
|
-
raise "Unable to absolutize #{base_url} and #{
|
38
|
+
raise "Unable to absolutize #{base_url} and #{relative_url}"
|
38
39
|
end
|
39
40
|
end
|
41
|
+
|
42
|
+
# remove any double slashes in the path
|
43
|
+
absolute_url.path = absolute_url.path.gsub("//", "/")
|
44
|
+
|
45
|
+
absolute_url
|
40
46
|
end
|
41
47
|
end
|
data/lib/absolutize.rb~
CHANGED
@@ -13,29 +13,35 @@ class Absolutize
|
|
13
13
|
@options[:output_debug] = false
|
14
14
|
end
|
15
15
|
|
16
|
-
def url(
|
16
|
+
def url(relative_url)
|
17
17
|
# encode the url if the new url contains spaces but doesn't contain %, i.e isn't already encoded
|
18
|
-
|
18
|
+
relative_url = relative_url.split("#").first if relative_url.include?"#" and @options[:remove_anchors]
|
19
|
+
relative_url = URI.encode(relative_url, " <>\{\}|\\\^\[\]`") if @options[:force_escaping]
|
19
20
|
|
20
|
-
|
21
|
+
absolute_url = nil
|
21
22
|
begin
|
22
|
-
URI.join(@base_url,
|
23
|
+
absolute_url = URI.join(@base_url, relative_url)
|
23
24
|
rescue URI::InvalidURIError => urie
|
24
25
|
puts "Unable to use URI.join attempting manually" if @options[:output_debug]
|
25
|
-
if @base_url =~ /\Ahttp/ and
|
26
|
-
puts "base url starts with
|
26
|
+
if @base_url =~ /\Ahttp/ and relative_url =~ /\A\//
|
27
|
+
puts "base url starts with http and relative_url is relative to root" if @options[:output_debug]
|
27
28
|
uri = URI.parse(@base_url)
|
28
29
|
if uri.port
|
29
|
-
"#{uri.scheme}://#{uri.host}:#{uri.port}#{
|
30
|
+
absolute_url = URI.parse("#{uri.scheme}://#{uri.host}:#{uri.port}#{relative_url}")
|
30
31
|
else
|
31
|
-
"#{uri.scheme}://#{uri.host}#{
|
32
|
+
absolute_url = URI.parse("#{uri.scheme}://#{uri.host}#{relative_url}")
|
32
33
|
end
|
33
|
-
elsif
|
34
|
+
elsif relative_url =~ /\Ahttp/
|
34
35
|
#new url is absolute anyway
|
35
|
-
|
36
|
+
absolute_url = URI.parse(relative_url)
|
36
37
|
else
|
37
|
-
raise "Unable to absolutize #{base_url} and #{
|
38
|
+
raise "Unable to absolutize #{base_url} and #{relative_url}"
|
38
39
|
end
|
39
40
|
end
|
41
|
+
|
42
|
+
# remove any double slashes in the path
|
43
|
+
absolute_url.path = absolute_url.path.gsub("//", "/")
|
44
|
+
|
45
|
+
absolute_url
|
40
46
|
end
|
41
47
|
end
|
@@ -14,10 +14,25 @@ describe Absolutize do
|
|
14
14
|
it "should return the same url for an absolute url" do
|
15
15
|
@absolutize.url("http://www.bbc.co.uk/").to_s.should == "http://www.bbc.co.uk/"
|
16
16
|
end
|
17
|
+
it "should return the same url for an absolute url" do
|
18
|
+
@absolutize.url("http://www.bbc.co.uk//asdf/asdf.html").to_s.should == "http://www.bbc.co.uk/asdf/asdf.html"
|
19
|
+
end
|
17
20
|
it "should return the absolute url for a relative to root url" do
|
18
21
|
@absolutize.url("/asdf.html").to_s.should == "http://www.baseurl.com/asdf.html"
|
19
22
|
end
|
20
23
|
it "should return the absolute url for a relative to folder url" do
|
21
24
|
@absolutize.url("asdf.html").to_s.should == "http://www.baseurl.com/asdf.html"
|
22
25
|
end
|
26
|
+
it "should handle urls with spaces" do
|
27
|
+
@absolutize.url("/root folder/asdf.html").to_s.should == "http://www.baseurl.com/root%20folder/asdf.html"
|
28
|
+
end
|
29
|
+
it "should handle urls with ['s" do
|
30
|
+
@absolutize.url("/folder/asdf.html?id=[asdf]").to_s.should == "http://www.baseurl.com/folder/asdf.html?id=%5Basdf%5D"
|
31
|
+
end
|
32
|
+
it "should not encode urls with %'s" do
|
33
|
+
@absolutize.url("/root%20folder/asdf.html?id=asdf").to_s.should == "http://www.baseurl.com/root%20folder/asdf.html?id=asdf"
|
34
|
+
end
|
35
|
+
it "should not encode existing encoded characters" do
|
36
|
+
@absolutize.url("/root%20folder/asdf.html?id=[asdf]").to_s.should == "http://www.baseurl.com/root%20folder/asdf.html?id=%5Basdf%5D"
|
37
|
+
end
|
23
38
|
end
|
@@ -14,10 +14,23 @@ describe Absolutize do
|
|
14
14
|
it "should return the same url for an absolute url" do
|
15
15
|
@absolutize.url("http://www.bbc.co.uk/").to_s.should == "http://www.bbc.co.uk/"
|
16
16
|
end
|
17
|
+
it "should return the same url for an absolute url" do
|
18
|
+
@absolutize.url("http://www.bbc.co.uk//asdf/asdf.html").to_s.should == "http://www.bbc.co.uk/asdf/asdf.html"
|
19
|
+
end
|
17
20
|
it "should return the absolute url for a relative to root url" do
|
18
21
|
@absolutize.url("/asdf.html").to_s.should == "http://www.baseurl.com/asdf.html"
|
19
22
|
end
|
20
23
|
it "should return the absolute url for a relative to folder url" do
|
21
24
|
@absolutize.url("asdf.html").to_s.should == "http://www.baseurl.com/asdf.html"
|
22
25
|
end
|
26
|
+
it "should handle urls with spaces" do
|
27
|
+
@absolutize.url("/root folder/asdf.html").to_s.should == "http://www.baseurl.com/root%20folder/asdf.html"
|
28
|
+
end
|
29
|
+
it "should handle urls with ['s" do
|
30
|
+
@absolutize.url("/folder/asdf.html?id=[asdf]").to_s.should == "http://www.baseurl.com/folder/asdf.html?id=%5Basdf%5D"
|
31
|
+
end
|
32
|
+
it "should not encode urls with %'s" do
|
33
|
+
@absolutize.url("/root%20folder/asdf.html?id=asdf").to_s.should == "http://www.baseurl.com/root%20folder/asdf.html?id=asdf"
|
34
|
+
end
|
35
|
+
|
23
36
|
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: 21
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 5
|
10
|
+
version: 0.0.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Stewart McKee
|