Browse files

initial code

  • Loading branch information...
1 parent 3399061 commit a45a48e76b949de40dea44489ec199db7760fdfb @stewartmckee committed Aug 4, 2010
Showing with 121 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. 0 README
  3. +47 −0 README.textile
  4. +13 −0 absolutize.gemspec
  5. +34 −0 lib/absolutize.rb
  6. +23 −0 spec/absolutize/absolutize_spec.rb
  7. +2 −0 spec/spec.opts
  8. +1 −0 spec/spec_helper.rb
View
1 .gitignore
@@ -0,0 +1 @@
+*~
View
0 README
No changes.
View
47 README.textile
@@ -0,0 +1,47 @@
+
+h1. Absolutize v0.0.1
+
+h2. Intro
+
+I know, I know, i should just fix URI.join, tough i haven't!
+
+h2. Installation
+
+Install absolutize as a gem
+
+bq. gem install absolutize
+
+h2. Usage
+
+absol = Absolutize.new(base_url)
+uri = absol.url(relative_url)
+
+h3. Methods
+
+new - creates a new absolutize object based on a base_url
+url - absolutizes the relative_url based on the base_url
+
+
+h2 License
+
+h3 The MIT License
+
+Copyright (c) <year> <copyright holders>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
View
13 absolutize.gemspec
@@ -0,0 +1,13 @@
+spec = Gem::Specification.new do |s|
+ s.name = "absolutize"
+ s.version = "0.0.1"
+ s.author = "Stewart McKee"
+ s.email = "stewart.mckee@vamosa.com"
+ s.homepage = "http://www.vamosa.com/"
+ s.platform = Gem::Platform::RUBY
+ s.summary = "URI Absolitizing parser"
+ s.files = Dir["{spec,lib}/**/*"].delete_if { |f| f =~ /(rdoc)$/i }
+ s.require_path = "lib"
+ s.has_rdoc = false
+ s.extra_rdoc_files = ["README.textile"]
+end
View
34 lib/absolutize.rb
@@ -0,0 +1,34 @@
+require 'rubygems'
+require 'uri'
+require File.expand_path(File.dirname(__FILE__) + '/absolutize')
+
+class Absolutize
+
+ def initialize(base_url)
+ @base_url = base_url
+ end
+
+ def url(new_url)
+ # encode the url if the new url contains spaces but doesn't contain %, i.e isn't already encoded
+ new_url = URI.encode(new_url, " <>\{\}|\\\^\[\]`") if not new_url =~ /%/
+ begin
+ URI.join(@base_url, new_url).to_s
+ rescue URI::InvalidURIError => urie
+ puts "Unable to use URI.join attempting manually"
+ if @base_url =~ /\Ahttp/ and new_url =~ /\A\//
+ puts "base url starts with htt and new_url is relative to root"
+ uri = URI.parse(@base_url)
+ if uri.port
+ "#{uri.scheme}://#{uri.host}:#{uri.port}#{new_url}"
+ else
+ "#{uri.scheme}://#{uri.host}#{new_url}"
+ end
+ elsif new_url =~ /\Ahttp/
+ # new url is absolute anyway
+ new_url
+ else
+ raise "Unable to absolutize #{base_url} and #{new_url}"
+ end
+ end
+ end
+end
View
23 spec/absolutize/absolutize_spec.rb
@@ -0,0 +1,23 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe Absolutize do
+
+ before(:each) do
+ @base_url = "http://www.baseurl.com/"
+ @absolutize = Absolutize.new(@base_url)
+ end
+
+ it "should create an Absolutize object" do
+ @absolutize.should be_an_instance_of Absolutize
+ end
+
+ it "should return the same url for an absolute url" do
+ @absolutize.url("http://www.bbc.co.uk/").to_s.should == "http://www.bbc.co.uk/"
+ end
+ it "should return the absolute url for a relative to root url" do
+ @absolutize.url("/asdf.html").to_s.should == "http://www.baseurl.com/asdf.html"
+ end
+ it "should return the absolute url for a relative to folder url" do
+ @absolutize.url("asdf.html").to_s.should == "http://www.baseurl.com/asdf.html"
+ end
+end
View
2 spec/spec.opts
@@ -0,0 +1,2 @@
+--colour
+--format specdoc
View
1 spec/spec_helper.rb
@@ -0,0 +1 @@
+require File.expand_path(File.dirname(__FILE__) + '/../lib/absolutize')

0 comments on commit a45a48e

Please sign in to comment.