Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial implementation for url_for

  • Loading branch information...
commit 92828f3531b929b052d48819fb7b865998270b85 1 parent 40b156a
@emk authored
View
3  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
View
24 README.rdoc
@@ -1,7 +1,27 @@
= sinatra-url-for
-Description goes here.
+<code>sinatra-url-for</code> 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 <code>Sinatra::Base</code>, then you need to call
+<code>helpers</code> 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.
View
37 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
+ # <code>:path_only</code>, which will generate an absolute path within
+ # the current domain (the default), or <code>:full</code>, 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
View
0  lib/sinatra_url_for.rb
No changes.
View
7 spec/sinatra_url_for_spec.rb
@@ -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
View
2  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|
View
26 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 == <<EOD
+/
+/foo
+http://example.org/foo
+EOD
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.