-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
88 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Empty file.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |