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 CHANGED
@@ -1,5 +1,5 @@
1
1
 
2
- h1. Absolutize v0.0.4
2
+ h1. Absolutize v0.0.5
3
3
 
4
4
  h2. Intro
5
5
 
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(new_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
- new_url = new_url.split("#").first if new_url.include?"#" and @options[:remove_anchors]
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
- new_url = URI.encode(new_url, " <>\{\}|\\\^\[\]`") if not new_url =~ /%/ and @options[:force_escaping]
21
+ absolute_url = nil
21
22
  begin
22
- URI.join(@base_url, new_url).to_s
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 new_url =~ /\A\//
26
- puts "base url starts with htt and new_url is relative to root" 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]
27
28
  uri = URI.parse(@base_url)
28
29
  if uri.port
29
- "#{uri.scheme}://#{uri.host}:#{uri.port}#{new_url}"
30
+ absolute_url = URI.parse("#{uri.scheme}://#{uri.host}:#{uri.port}#{relative_url}")
30
31
  else
31
- "#{uri.scheme}://#{uri.host}#{new_url}"
32
+ absolute_url = URI.parse("#{uri.scheme}://#{uri.host}#{relative_url}")
32
33
  end
33
- elsif new_url =~ /\Ahttp/
34
+ elsif relative_url =~ /\Ahttp/
34
35
  #new url is absolute anyway
35
- new_url
36
+ absolute_url = URI.parse(relative_url)
36
37
  else
37
- raise "Unable to absolutize #{base_url} and #{new_url}"
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(new_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
- new_url = new_url.split("#").first if new_url.include?"#" and @options[:remove_anchors]
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
- new_url = URI.encode(new_url, " <>\{\}|\\\^\[\]`") if not new_url =~ /%/ and @options[:force_escaping]
21
+ absolute_url = nil
21
22
  begin
22
- URI.join(@base_url, new_url).to_s
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 new_url =~ /\A\//
26
- puts "base url starts with htt and new_url is relative to root" 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]
27
28
  uri = URI.parse(@base_url)
28
29
  if uri.port
29
- "#{uri.scheme}://#{uri.host}:#{uri.port}#{new_url}"
30
+ absolute_url = URI.parse("#{uri.scheme}://#{uri.host}:#{uri.port}#{relative_url}")
30
31
  else
31
- "#{uri.scheme}://#{uri.host}#{new_url}"
32
+ absolute_url = URI.parse("#{uri.scheme}://#{uri.host}#{relative_url}")
32
33
  end
33
- elsif new_url =~ /\Ahttp/
34
+ elsif relative_url =~ /\Ahttp/
34
35
  #new url is absolute anyway
35
- new_url
36
+ absolute_url = URI.parse(relative_url)
36
37
  else
37
- raise "Unable to absolutize #{base_url} and #{new_url}"
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: 23
4
+ hash: 21
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 4
10
- version: 0.0.4
9
+ - 5
10
+ version: 0.0.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Stewart McKee