emk-sinatra-url-for 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +21 -0
- data/README.rdoc +39 -0
- data/VERSION.yml +4 -0
- data/lib/sinatra/url_for.rb +37 -0
- data/spec/spec_helper.rb +9 -0
- data/spec/url_for_spec.rb +26 -0
- metadata +80 -0
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
sinatra-url-for - Construct absolute paths and full URLs
|
2
|
+
Copyright 2009 Eric Kidd
|
3
|
+
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
a copy of this software and associated documentation files (the
|
6
|
+
"Software"), to deal in the Software without restriction, including
|
7
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
= sinatra-url-for
|
2
|
+
|
3
|
+
<code>sinatra-url-for</code> can be used by a Sinatra application to
|
4
|
+
construct absolute paths and full URLs.
|
5
|
+
|
6
|
+
To install it, run:
|
7
|
+
|
8
|
+
sudo gem install emk-sinatra-url-for -s http://gems.github.com
|
9
|
+
|
10
|
+
To include it in a Sinatra application, write:
|
11
|
+
|
12
|
+
require 'rubygems'
|
13
|
+
gem 'emk-sinatra-url-for'
|
14
|
+
require 'sinatra/url_for'
|
15
|
+
|
16
|
+
Once this is done, you should be able to call +url_for+ from within a
|
17
|
+
request. Assuming that your application is running on
|
18
|
+
<code>example.com</code>, and that it has been mapped to
|
19
|
+
<code>/myapp</code>, you should get:
|
20
|
+
|
21
|
+
url_for "/" # Returns "/myapp/"
|
22
|
+
url_for "/foo" # Returns "/myapp/foo"
|
23
|
+
url_for "/foo", :full # Returns "http://example.com/myapp/foo"
|
24
|
+
|
25
|
+
If you're subclassing <code>Sinatra::Base</code>, then you need to call
|
26
|
+
<code>helpers</code> manually:
|
27
|
+
|
28
|
+
class MyApp < Sinatra::Base
|
29
|
+
helpers Sinatra::UrlForHelper
|
30
|
+
# ...
|
31
|
+
end
|
32
|
+
|
33
|
+
Thanks to "cypher23" on #mephisto and the folks on #rack for pointing me in
|
34
|
+
the right direction. If this gem fails to work correctly on your web
|
35
|
+
system, please feel free to submit patches and/or bug reports!
|
36
|
+
|
37
|
+
== Copyright
|
38
|
+
|
39
|
+
Copyright 2009 Eric Kidd. See LICENSE for details.
|
data/VERSION.yml
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
module Sinatra
|
2
|
+
module UrlForHelper
|
3
|
+
# Construct a link to +url_fragment+, which should be given relative to
|
4
|
+
# the base of this Sinatra app. The mode should be either
|
5
|
+
# <code>:path_only</code>, which will generate an absolute path within
|
6
|
+
# the current domain (the default), or <code>:full</code>, which will
|
7
|
+
# include the site name and port number. (The latter is typically
|
8
|
+
# necessary for links in RSS feeds.) Example usage:
|
9
|
+
#
|
10
|
+
# url_for "/" # Returns "/myapp/"
|
11
|
+
# url_for "/foo" # Returns "/myapp/foo"
|
12
|
+
# url_for "/foo", :full # Returns "http://example.com/myapp/foo"
|
13
|
+
#--
|
14
|
+
# See README.rdoc for a list of some of the people who helped me clean
|
15
|
+
# up earlier versions of this code.
|
16
|
+
def url_for url_fragment, mode=:path_only
|
17
|
+
case mode
|
18
|
+
when :path_only
|
19
|
+
base = request.script_name
|
20
|
+
when :full
|
21
|
+
scheme = request.scheme
|
22
|
+
if (scheme == 'http' && request.port == 80 ||
|
23
|
+
scheme == 'https' && request.port == 443)
|
24
|
+
port = ""
|
25
|
+
else
|
26
|
+
port = ":#{request.port}"
|
27
|
+
end
|
28
|
+
base = "#{scheme}://#{request.host}#{port}#{request.script_name}"
|
29
|
+
else
|
30
|
+
raise TypeError, "Unknown url_for mode #{mode}"
|
31
|
+
end
|
32
|
+
"#{base}#{url_fragment}"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
helpers UrlForHelper
|
37
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'sinatra'
|
3
|
+
require 'sinatra/test'
|
4
|
+
require 'sinatra/url_for'
|
5
|
+
|
6
|
+
get "/" do
|
7
|
+
content_type "text/plain"
|
8
|
+
<<"EOD"
|
9
|
+
#{url_for("/")}
|
10
|
+
#{url_for("/foo")}
|
11
|
+
#{url_for("/foo", :full)}
|
12
|
+
EOD
|
13
|
+
end
|
14
|
+
|
15
|
+
describe Sinatra::UrlForHelper do
|
16
|
+
include Sinatra::Test
|
17
|
+
it "should return absolute paths and full URLs" do
|
18
|
+
get "/"
|
19
|
+
response.should be_ok
|
20
|
+
response.body.should == <<EOD
|
21
|
+
/
|
22
|
+
/foo
|
23
|
+
http://example.org/foo
|
24
|
+
EOD
|
25
|
+
end
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: emk-sinatra-url-for
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Eric Kidd
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-04-22 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: sinatra
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.9.1.1
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.1.11
|
34
|
+
version:
|
35
|
+
description:
|
36
|
+
email: git@randomhacks.net
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- README.rdoc
|
43
|
+
- LICENSE
|
44
|
+
files:
|
45
|
+
- README.rdoc
|
46
|
+
- VERSION.yml
|
47
|
+
- lib/sinatra
|
48
|
+
- lib/sinatra/url_for.rb
|
49
|
+
- spec/spec_helper.rb
|
50
|
+
- spec/url_for_spec.rb
|
51
|
+
- LICENSE
|
52
|
+
has_rdoc: true
|
53
|
+
homepage: http://github.com/emk/sinatra-url-for
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options:
|
56
|
+
- --inline-source
|
57
|
+
- --charset=UTF-8
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
version:
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: "0"
|
71
|
+
version:
|
72
|
+
requirements: []
|
73
|
+
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 1.2.0
|
76
|
+
signing_key:
|
77
|
+
specification_version: 2
|
78
|
+
summary: Construct absolute paths and full URLs for a Sinatra application
|
79
|
+
test_files: []
|
80
|
+
|