absolutize 0.0.9 → 0.0.10
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/absolutize.rb +6 -3
- data/spec/absolutize/absolutize_spec.rb +17 -2
- metadata +4 -5
- data/lib/absolutize.rb~ +0 -47
data/lib/absolutize.rb
CHANGED
@@ -16,12 +16,15 @@ class Absolutize
|
|
16
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
|
-
|
19
|
+
if @options[:force_escaping]
|
20
|
+
relative_url = URI.decode(relative_url)#force the decode of the URL
|
21
|
+
relative_url = URI.encode(relative_url, " <>\{\}|\\\^\[\]|`")
|
22
|
+
end
|
20
23
|
|
21
24
|
absolute_url = nil
|
22
25
|
begin
|
23
26
|
absolute_url = URI.join(@base_url, relative_url)
|
24
|
-
rescue URI::InvalidURIError =>
|
27
|
+
rescue URI::InvalidURIError => uri
|
25
28
|
puts "Unable to use URI.join attempting manually" if @options[:output_debug]
|
26
29
|
if @base_url =~ /\Ahttp/ and relative_url =~ /\A\//
|
27
30
|
puts "base url starts with http and relative_url is relative to root" if @options[:output_debug]
|
@@ -35,7 +38,7 @@ class Absolutize
|
|
35
38
|
#new url is absolute anyway
|
36
39
|
absolute_url = URI.parse(relative_url)
|
37
40
|
else
|
38
|
-
raise "Unable to absolutize #{base_url} and #{relative_url}"
|
41
|
+
raise "Unable to absolutize #{@base_url} and #{relative_url}"
|
39
42
|
end
|
40
43
|
end
|
41
44
|
|
@@ -19,8 +19,15 @@ describe Absolutize do
|
|
19
19
|
@absolutize.url("http://www.bbc.co.uk/").to_s.should == "http://www.bbc.co.uk/"
|
20
20
|
end
|
21
21
|
it "should return the same url for an absolute url" do
|
22
|
-
@absolutize.url("http://www.bbc.co.uk//asdf/asdf.html").to_s.should == "http://www.bbc.co.uk
|
22
|
+
@absolutize.url("http://www.bbc.co.uk//asdf/asdf.html").to_s.should == "http://www.bbc.co.uk//asdf/asdf.html"
|
23
23
|
end
|
24
|
+
|
25
|
+
it "should return the correct absolute URL when the url is already encoded" do
|
26
|
+
ab = Absolutize.new("http://www.baseurl.com/top_folder/index.html")
|
27
|
+
ab.url("%2ftop_folder%2fsub_folder%2findex.html").to_s.should == "http://www.baseurl.com/top_folder/sub_folder/index.html"
|
28
|
+
ab.url("/top_folder/sub_folder/index.html").to_s.should == "http://www.baseurl.com/top_folder/sub_folder/index.html"
|
29
|
+
end
|
30
|
+
|
24
31
|
it "should return the absolute url for a relative to root url" do
|
25
32
|
@absolutize.url("/asdf.html").to_s.should == "http://www.baseurl.com/asdf.html"
|
26
33
|
end
|
@@ -58,7 +65,15 @@ describe Absolutize do
|
|
58
65
|
@absolutize = Absolutize.new(@base_url, :force_escaping => false)
|
59
66
|
end
|
60
67
|
it "should not escape invalid characters" do
|
61
|
-
lambda { @absolutize.url("/root folder/asdf.html#anchor")}.should raise_error
|
68
|
+
lambda { @absolutize.url("/root folder/asdf.html#anchor")}.should raise_error
|
69
|
+
|
70
|
+
ab = Absolutize.new("http://www.baseurl.com/top_folder/index.html", :force_escaping => false)
|
71
|
+
#this is actually wrong, but we have not forced escaping
|
72
|
+
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
|
+
ab.url("/top_folder/sub_folder/index.html").to_s.should == "http://www.baseurl.com/top_folder/sub_folder/index.html"
|
75
|
+
|
62
76
|
end
|
77
|
+
|
63
78
|
end
|
64
79
|
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: 11
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 10
|
10
|
+
version: 0.0.10
|
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-18 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -34,7 +34,6 @@ files:
|
|
34
34
|
- spec/absolutize/absolutize_spec.rb
|
35
35
|
- spec/absolutize/absolutize_spec.rb~
|
36
36
|
- lib/absolutize.rb
|
37
|
-
- lib/absolutize.rb~
|
38
37
|
- README.textile
|
39
38
|
has_rdoc: false
|
40
39
|
homepage: http://github.com/vamosa/absolutize
|
data/lib/absolutize.rb~
DELETED
@@ -1,47 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'uri'
|
3
|
-
require File.expand_path(File.dirname(__FILE__) + '/absolutize')
|
4
|
-
|
5
|
-
class Absolutize
|
6
|
-
|
7
|
-
def initialize(base_url, options = {})
|
8
|
-
@base_url = base_url
|
9
|
-
@options = options
|
10
|
-
|
11
|
-
@options[:remove_anchors] = false if @options[:remove_anchors].nil?
|
12
|
-
@options[:force_escaping] = true if @options[:force_escaping].nil?
|
13
|
-
@options[:output_debug] = false if @options[:output_debug].nil?
|
14
|
-
end
|
15
|
-
|
16
|
-
def url(relative_url)
|
17
|
-
# encode the url if the new url contains spaces but doesn't contain %, i.e isn't already encoded
|
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]
|
20
|
-
|
21
|
-
absolute_url = nil
|
22
|
-
begin
|
23
|
-
absolute_url = URI.join(@base_url, relative_url)
|
24
|
-
rescue URI::InvalidURIError => urie
|
25
|
-
puts "Unable to use URI.join attempting manually" if @options[:output_debug]
|
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]
|
28
|
-
uri = URI.parse(@base_url)
|
29
|
-
if uri.port
|
30
|
-
absolute_url = URI.parse("#{uri.scheme}://#{uri.host}:#{uri.port}#{relative_url}")
|
31
|
-
else
|
32
|
-
absolute_url = URI.parse("#{uri.scheme}://#{uri.host}#{relative_url}")
|
33
|
-
end
|
34
|
-
elsif relative_url =~ /\Ahttp/
|
35
|
-
#new url is absolute anyway
|
36
|
-
absolute_url = URI.parse(relative_url)
|
37
|
-
else
|
38
|
-
raise "Unable to absolutize #{base_url} and #{relative_url}"
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
# remove any double slashes in the path
|
43
|
-
absolute_url.path = absolute_url.path.gsub("//", "/") unless absolute_url.path.nil?
|
44
|
-
|
45
|
-
absolute_url
|
46
|
-
end
|
47
|
-
end
|