mynyml-rack-abstract-format 0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +19 -0
- data/README +28 -0
- data/Rakefile +66 -0
- data/examples/app.rb +19 -0
- data/lib/rack/abstract_format.rb +17 -0
- data/simple_router.gemspec +63 -0
- data/test/test_abstract_format.rb +31 -0
- data/test/test_helper.rb +20 -0
- metadata +64 -0
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright © 2009 Martin Aumont (mynyml)
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
4
|
+
this software and associated documentation files (the "Software"), to deal in
|
5
|
+
the Software without restriction, including without limitation the rights to
|
6
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
7
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
8
|
+
so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
11
|
+
copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
+
SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
SUMMARY
|
2
|
+
|
3
|
+
Strips the extension from the requested path (env['PATH_INFO']) and stores it
|
4
|
+
into env['request.format']. This allows dealing with path information
|
5
|
+
separately from format information, which are often different concerns.
|
6
|
+
|
7
|
+
This is especially useful when dealing with routes as it allows a resource to
|
8
|
+
always point to the same action independently from the requested format.
|
9
|
+
|
10
|
+
USAGE
|
11
|
+
|
12
|
+
use Rack::AbstractFormat
|
13
|
+
run app
|
14
|
+
|
15
|
+
The request:
|
16
|
+
|
17
|
+
GET /path/resource.xml
|
18
|
+
|
19
|
+
will become:
|
20
|
+
|
21
|
+
GET /path/resource
|
22
|
+
env['request.format'] #=> 'xml'
|
23
|
+
|
24
|
+
TIP
|
25
|
+
|
26
|
+
To make sure there is always an extension, as well as to retrieve format
|
27
|
+
information from the request's Accept header, use together with rack-contrib's
|
28
|
+
Rack::AcceptFormat middleware.
|
data/Rakefile
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# --------------------------------------------------
|
2
|
+
# based on thin's Rakefile (http://github.com/macournoyer/thin)
|
3
|
+
# --------------------------------------------------
|
4
|
+
require 'rake/gempackagetask'
|
5
|
+
require 'rake/rdoctask'
|
6
|
+
require 'pathname'
|
7
|
+
require 'yaml'
|
8
|
+
|
9
|
+
RUBY_1_9 = RUBY_VERSION =~ /^1\.9/
|
10
|
+
WIN = (RUBY_PLATFORM =~ /mswin|cygwin/)
|
11
|
+
SUDO = (WIN ? "" : "sudo")
|
12
|
+
|
13
|
+
def gem
|
14
|
+
RUBY_1_9 ? 'gem19' : 'gem'
|
15
|
+
end
|
16
|
+
|
17
|
+
def all_except(res)
|
18
|
+
Dir['**/*'].reject do |path|
|
19
|
+
Array(res).any? {|re| path.match(re) }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
spec = Gem::Specification.new do |s|
|
24
|
+
s.name = 'rack-abstract-format'
|
25
|
+
s.version = '0.9'
|
26
|
+
s.summary = "Rack middleware that abstracts format (extension) away from the path (into env)"
|
27
|
+
s.description = "Rack middleware that abstracts format (extension) away from the path (into env)"
|
28
|
+
s.author = "Martin Aumont"
|
29
|
+
s.email = 'mynyml@gmail.com'
|
30
|
+
s.homepage = ''
|
31
|
+
s.has_rdoc = true
|
32
|
+
s.require_path = "lib"
|
33
|
+
s.files = all_except([/doc\/.*/, /pkg\/*/])
|
34
|
+
end
|
35
|
+
|
36
|
+
Rake::GemPackageTask.new(spec) do |p|
|
37
|
+
p.gem_spec = spec
|
38
|
+
end
|
39
|
+
|
40
|
+
desc "Remove package products"
|
41
|
+
task :clean => :clobber_package
|
42
|
+
|
43
|
+
desc "Update the gemspec for GitHub's gem server"
|
44
|
+
task :gemspec do
|
45
|
+
Pathname("#{spec.name}.gemspec").open('w') {|f| f << YAML.dump(spec) }
|
46
|
+
end
|
47
|
+
|
48
|
+
desc "Install gem"
|
49
|
+
task :install => [:clobber, :package] do
|
50
|
+
sh "#{SUDO} #{gem} install pkg/#{spec.full_name}.gem"
|
51
|
+
end
|
52
|
+
|
53
|
+
desc "Uninstall gem"
|
54
|
+
task :uninstall => :clean do
|
55
|
+
sh "#{SUDO} #{gem} uninstall -v #{spec.version} -x #{spec.name}"
|
56
|
+
end
|
57
|
+
|
58
|
+
desc "Generate rdoc documentation."
|
59
|
+
Rake::RDocTask.new("rdoc") { |rdoc|
|
60
|
+
rdoc.rdoc_dir = 'doc/rdoc'
|
61
|
+
rdoc.title = "Simple Router - Document"
|
62
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
63
|
+
rdoc.options << '--charset' << 'utf-8'
|
64
|
+
rdoc.rdoc_files.include('README')
|
65
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
66
|
+
}
|
data/examples/app.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# run me with:
|
2
|
+
# $rackup examples/app.rb
|
3
|
+
#
|
4
|
+
require 'rubygems'
|
5
|
+
require 'rack'
|
6
|
+
require 'rack/abstract_format'
|
7
|
+
|
8
|
+
class App
|
9
|
+
def call(env)
|
10
|
+
body = <<-TXT
|
11
|
+
env['PATH_INFO'] #=> #{env['PATH_INFO'].inspect}
|
12
|
+
env['request.format'] #=> #{env['request.format'].inspect}
|
13
|
+
TXT
|
14
|
+
[200, {'Content-Type' => 'text/plain'}, [body]]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
use Rack::AbstractFormat
|
19
|
+
run App.new
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
|
3
|
+
module Rack
|
4
|
+
class AbstractFormat
|
5
|
+
|
6
|
+
def initialize(app)
|
7
|
+
@app = app
|
8
|
+
end
|
9
|
+
|
10
|
+
def call(env)
|
11
|
+
path = Pathname(env['PATH_INFO'])
|
12
|
+
env['PATH_INFO'] = path.to_s.sub(/#{path.extname}$/,'')
|
13
|
+
env['request.format'] = path.extname.sub(/^\./,'')
|
14
|
+
@app.call(env)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_router
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.8.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Martin Aumont
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-21 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Rack middleware that abstracts format information (extension) away from the path (into env)
|
17
|
+
email: mynyml@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- Rakefile
|
26
|
+
- test
|
27
|
+
- test/test_abstract_format.rb
|
28
|
+
- test/test_helper.rb
|
29
|
+
- lib
|
30
|
+
- lib/rack
|
31
|
+
- lib/rack/abstract_format.rb
|
32
|
+
- LICENSE
|
33
|
+
- README
|
34
|
+
has_rdoc: true
|
35
|
+
homepage: ""
|
36
|
+
licenses: []
|
37
|
+
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
requirements: []
|
56
|
+
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 1.3.3
|
59
|
+
signing_key:
|
60
|
+
specification_version: 3
|
61
|
+
summary: Rack middleware that abstracts format information (extension) away from the path (into env)
|
62
|
+
test_files: []
|
63
|
+
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'test/test_helper'
|
2
|
+
|
3
|
+
App = Rack::Builder.new {
|
4
|
+
use Rack::Lint
|
5
|
+
use Rack::AbstractFormat
|
6
|
+
run lambda {|env| [200, {'Content-Type' => 'text/html'}, ['']] }
|
7
|
+
}.to_app
|
8
|
+
|
9
|
+
class AbstractFormatTest < Test::Unit::TestCase
|
10
|
+
|
11
|
+
def get(path)
|
12
|
+
env = Rack::MockRequest.env_for(path)
|
13
|
+
App.call(env).last
|
14
|
+
env
|
15
|
+
end
|
16
|
+
|
17
|
+
test "strips extension" do
|
18
|
+
env = get('/path/resource.xml')
|
19
|
+
assert_equal '/path/resource', env['PATH_INFO']
|
20
|
+
end
|
21
|
+
|
22
|
+
test "doesn't get confused with dots in path" do
|
23
|
+
env = get('/path.to/resource.xml')
|
24
|
+
assert_equal '/path.to/resource', env['PATH_INFO']
|
25
|
+
end
|
26
|
+
|
27
|
+
test "stores format in environment" do
|
28
|
+
env = get('/path.to/resource.xml')
|
29
|
+
assert_equal 'xml', env['request.format']
|
30
|
+
end
|
31
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'rubygems'
|
4
|
+
require 'rack'
|
5
|
+
begin
|
6
|
+
require 'ruby-debug'
|
7
|
+
rescue LoadError, RuntimeError
|
8
|
+
end
|
9
|
+
|
10
|
+
root = Pathname(__FILE__).dirname.parent.expand_path
|
11
|
+
$:.unshift(root.join('lib'))
|
12
|
+
|
13
|
+
require 'rack/abstract_format'
|
14
|
+
|
15
|
+
class Test::Unit::TestCase
|
16
|
+
def self.test(name, &block)
|
17
|
+
name = :"test_#{name.gsub(/\s/,'_')}"
|
18
|
+
define_method(name, &block)
|
19
|
+
end
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mynyml-rack-abstract-format
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.9"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Martin Aumont
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-20 21:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Rack middleware that abstracts format (extension) away from the path (into env)
|
17
|
+
email: mynyml@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- Rakefile
|
26
|
+
- test
|
27
|
+
- test/test_abstract_format.rb
|
28
|
+
- test/test_helper.rb
|
29
|
+
- examples
|
30
|
+
- examples/app.rb
|
31
|
+
- simple_router.gemspec
|
32
|
+
- lib
|
33
|
+
- lib/rack
|
34
|
+
- lib/rack/abstract_format.rb
|
35
|
+
- LICENSE
|
36
|
+
- README
|
37
|
+
has_rdoc: true
|
38
|
+
homepage: ""
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: "0"
|
49
|
+
version:
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: "0"
|
55
|
+
version:
|
56
|
+
requirements: []
|
57
|
+
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 1.2.0
|
60
|
+
signing_key:
|
61
|
+
specification_version: 3
|
62
|
+
summary: Rack middleware that abstracts format (extension) away from the path (into env)
|
63
|
+
test_files: []
|
64
|
+
|