Skip to content

Commit

Permalink
Initial implementation for url_for
Browse files Browse the repository at this point in the history
  • Loading branch information
emk committed Apr 22, 2009
1 parent 40b156a commit 92828f3
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 11 deletions.
3 changes: 2 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
24 changes: 22 additions & 2 deletions README.rdoc
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.
37 changes: 37 additions & 0 deletions lib/sinatra/url_for.rb
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 removed lib/sinatra_url_for.rb
Empty file.
7 changes: 0 additions & 7 deletions spec/sinatra_url_for_spec.rb

This file was deleted.

2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -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|

Expand Down
26 changes: 26 additions & 0 deletions spec/url_for_spec.rb
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

0 comments on commit 92828f3

Please sign in to comment.