diff --git a/LICENSE b/LICENSE
index 7bcaec7..e6acb8c 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,4 +1,5 @@
-Copyright (c) 2009 Eric Kidd
+sinatra-url-for - Construct absolute paths and full URLs
+Copyright 2009 Eric Kidd
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
diff --git a/README.rdoc b/README.rdoc
index dc4db51..2a213ce 100644
--- a/README.rdoc
+++ b/README.rdoc
@@ -1,7 +1,27 @@
= sinatra-url-for
-Description goes here.
+sinatra-url-for
can be used by a Sinatra application to
+construct absolute paths and full URLs. For example:
+
+ gem 'emk-sinatra-url-for'
+ require 'sinatra/url_for'
+
+ # From within a request.
+ url_for "/" # Returns "/myapp/"
+ url_for "/foo" # Returns "/myapp/foo"
+ url_for "/foo", :full # Returns "http://example.com/myapp/foo"
+
+If you're subclassing Sinatra::Base
, then you need to call
+helpers
manually:
+
+ class MyApp < Sinatra::Base
+ helpers Sinatra::UrlForHelper
+ # ...
+ end
+
+Thanks to "cypher23" on #mephisto and the folks on #rack for pointing me in
+the right direction.
== Copyright
-Copyright (c) 2009 Eric Kidd. See LICENSE for details.
+Copyright 2009 Eric Kidd. See LICENSE for details.
diff --git a/lib/sinatra/url_for.rb b/lib/sinatra/url_for.rb
new file mode 100644
index 0000000..3dfa1dd
--- /dev/null
+++ b/lib/sinatra/url_for.rb
@@ -0,0 +1,37 @@
+module Sinatra
+ module UrlForHelper
+ # Construct a link to +url_fragment+, which should be given relative to
+ # the base of this Sinatra app. The mode should be either
+ # :path_only
, which will generate an absolute path within
+ # the current domain (the default), or :full
, which will
+ # include the site name and port number. (The latter is typically
+ # necessary for links in RSS feeds.) Example usage:
+ #
+ # url_for "/" # Returns "/myapp/"
+ # url_for "/foo" # Returns "/myapp/foo"
+ # url_for "/foo", :full # Returns "http://example.com/myapp/foo"
+ #--
+ # See README.rdoc for a list of some of the people who helped me clean
+ # up earlier versions of this code.
+ def url_for url_fragment, mode=:path_only
+ case mode
+ when :path_only
+ base = request.script_name
+ when :full
+ scheme = request.scheme
+ if (scheme == 'http' && request.port == 80 ||
+ scheme == 'https' && request.port == 443)
+ port = ""
+ else
+ port = ":#{request.port}"
+ end
+ base = "#{scheme}://#{request.host}#{port}#{request.script_name}"
+ else
+ raise TypeError, "Unknown url_for mode #{mode}"
+ end
+ "#{base}#{url_fragment}"
+ end
+ end
+
+ helpers UrlForHelper
+end
diff --git a/lib/sinatra_url_for.rb b/lib/sinatra_url_for.rb
deleted file mode 100644
index e69de29..0000000
diff --git a/spec/sinatra_url_for_spec.rb b/spec/sinatra_url_for_spec.rb
deleted file mode 100644
index f79c1a2..0000000
--- a/spec/sinatra_url_for_spec.rb
+++ /dev/null
@@ -1,7 +0,0 @@
-require 'spec_helper'
-
-describe "SinatraUrlFor" do
- it "fails" do
- fail "hey buddy, you should probably rename this file and start specing for real"
- end
-end
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 6e65ef0..fe4e321 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -1,8 +1,8 @@
+require 'rubygems'
require 'spec'
$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
-require 'sinatra_url_for'
Spec::Runner.configure do |config|
diff --git a/spec/url_for_spec.rb b/spec/url_for_spec.rb
new file mode 100644
index 0000000..e9e7754
--- /dev/null
+++ b/spec/url_for_spec.rb
@@ -0,0 +1,26 @@
+require 'spec_helper'
+require 'sinatra'
+require 'sinatra/test'
+require 'sinatra/url_for'
+
+get "/" do
+ content_type "text/plain"
+ <<"EOD"
+#{url_for("/")}
+#{url_for("/foo")}
+#{url_for("/foo", :full)}
+EOD
+end
+
+describe Sinatra::UrlForHelper do
+ include Sinatra::Test
+ it "should return absolute paths and full URLs" do
+ get "/"
+ response.should be_ok
+ response.body.should == <