absolutize 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile ADDED
@@ -0,0 +1,22 @@
1
+
2
+ h1. Absolutize v0.0.1
3
+
4
+ h2. Intro
5
+
6
+ I know, I know, i should just fix URI.join, tough i haven't!
7
+
8
+ h2. Installation
9
+
10
+ Install absolutize as a gem
11
+
12
+ bq. gem install absolutize
13
+
14
+ h2. Usage
15
+
16
+ absol = Absolutize.new(base_url)
17
+ uri = absol.url(relative_url)
18
+
19
+ h3. Methods
20
+
21
+ new - creates a new absolutize object based on a base_url
22
+ url - absolutizes the relative_url based on the base_url
data/lib/absolutize.rb ADDED
@@ -0,0 +1,34 @@
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)
8
+ @base_url = base_url
9
+ end
10
+
11
+ def url(new_url)
12
+ # encode the url if the new url contains spaces but doesn't contain %, i.e isn't already encoded
13
+ new_url = URI.encode(new_url, " <>\{\}|\\\^\[\]`") if not new_url =~ /%/
14
+ begin
15
+ URI.join(@base_url, new_url).to_s
16
+ rescue URI::InvalidURIError => urie
17
+ puts "Unable to use URI.join attempting manually"
18
+ if @base_url =~ /\Ahttp/ and new_url =~ /\A\//
19
+ puts "base url starts with htt and new_url is relative to root"
20
+ uri = URI.parse(@base_url)
21
+ if uri.port
22
+ "#{uri.scheme}://#{uri.host}:#{uri.port}#{new_url}"
23
+ else
24
+ "#{uri.scheme}://#{uri.host}#{new_url}"
25
+ end
26
+ elsif new_url =~ /\Ahttp/
27
+ # new url is absolute anyway
28
+ new_url
29
+ else
30
+ raise "Unable to absolutize #{base_url} and #{new_url}"
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,33 @@
1
+ require 'rubygems'
2
+ require File.expand_path(File.dirname(__FILE__) + '/absolutize')
3
+
4
+ class Absolutize
5
+
6
+ def initialize(base_url)
7
+ @base_url = base_url
8
+ end
9
+
10
+ def url(new_url)
11
+ # encode the url if the new url contains spaces but doesn't contain %, i.e isn't already encoded
12
+ new_url = URI.encode(new_url, " <>\{\}|\\\^\[\]`") if not new_url =~ /%/
13
+ begin
14
+ URI.join(@base_url, new_url).to_s
15
+ rescue URI::InvalidURIError => urie
16
+ puts "Unable to use URI.join attempting manually"
17
+ if @base_url =~ /\Ahttp/ and new_url =~ /\A\//
18
+ puts "base url starts with htt and new_url is relative to root"
19
+ uri = URI.parse(@base_url)
20
+ if uri.port
21
+ "#{uri.scheme}://#{uri.host}:#{uri.port}#{new_url}"
22
+ else
23
+ "#{uri.scheme}://#{uri.host}#{new_url}"
24
+ end
25
+ elsif new_url =~ /\Ahttp/
26
+ # new url is absolute anyway
27
+ new_url
28
+ else
29
+ raise "Unable to absolutize #{base_url} and #{new_url}"
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,23 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Absolutize do
4
+
5
+ before(:each) do
6
+ @base_url = "http://www.baseurl.com/"
7
+ @absolutize = Absolutize.new(@base_url)
8
+ end
9
+
10
+ it "should create an Absolutize object" do
11
+ @absolutize.should be_an_instance_of Absolutize
12
+ end
13
+
14
+ it "should return the same url for an absolute url" do
15
+ @absolutize.url("http://www.bbc.co.uk/").to_s.should == "http://www.bbc.co.uk/"
16
+ end
17
+ it "should return the absolute url for a relative to root url" do
18
+ @absolutize.url("/asdf.html").to_s.should == "http://www.baseurl.com/asdf.html"
19
+ end
20
+ it "should return the absolute url for a relative to folder url" do
21
+ @absolutize.url("asdf.html").to_s.should == "http://www.baseurl.com/asdf.html"
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Absolutize do
4
+
5
+ before(:each) do
6
+ @base_url = "http://www.baseurl.com/"
7
+ @absolutize = Absolutize.new(@base_url)
8
+ end
9
+
10
+ it "should create an Absolutize object" do
11
+ @absolutize.should be_an_instance_of Absolutize
12
+ end
13
+
14
+ it "should return the same url for an absolute url" do
15
+ @absolutize.url("http://www.bbc.co.uk/").to_s.should == "http://www.bbc.co.uk/"
16
+ end
17
+ it "should return the absolute url for a relative to root url" do
18
+ @absolutize.url("/asdf.html").to_s.should == "http://www.bbc.co.uk/asdf.html"
19
+ end
20
+ it "should return the absolute url for a relative to folder url" do
21
+ @absolutize.url("asdf.html").to_s.should == "http://www.bbc.co.uk/asdf.html"
22
+ end
23
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format specdoc
@@ -0,0 +1 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/absolutize')
@@ -0,0 +1 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/spell_checker')
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: absolutize
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Stewart McKee
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-08-04 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description:
23
+ email: stewart.mckee@vamosa.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README.textile
30
+ files:
31
+ - spec/spec.opts
32
+ - spec/spec_helper.rb~
33
+ - spec/spec_helper.rb
34
+ - spec/absolutize/absolutize_spec.rb
35
+ - spec/absolutize/absolutize_spec.rb~
36
+ - lib/absolutize.rb
37
+ - lib/absolutize.rb~
38
+ - README.textile
39
+ has_rdoc: false
40
+ homepage: http://www.vamosa.com/
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ hash: 3
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 3
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.3.7
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: URI Absolitizing parser
73
+ test_files: []
74
+