rack-abstract-format 0.9.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +1 -0
- data/LICENSE +19 -0
- data/Manifest +10 -0
- data/README +27 -0
- data/Rakefile +27 -0
- data/examples/app.ru +28 -0
- data/lib/rack/abstract_format.rb +22 -0
- data/rack-abstract-format.gemspec +16 -0
- data/test/test_abstract_format.rb +30 -0
- data/test/test_helper.rb +16 -0
- metadata +83 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
pkg/
|
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/Manifest
ADDED
data/README
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
===== Summary
|
2
|
+
|
3
|
+
Strips the extension from the requested path (env['PATH_INFO']), casts it to
|
4
|
+
media type, and prepends it to env['HTTP_ACCEPT']. This allows dealing with path
|
5
|
+
information separately from format information, which are often different
|
6
|
+
concerns.
|
7
|
+
|
8
|
+
This is especially useful when dealing with routes as it allows a resource to
|
9
|
+
always point to the same action independently of the requested format.
|
10
|
+
|
11
|
+
===== Usage
|
12
|
+
|
13
|
+
require 'rack'
|
14
|
+
require 'rack/abstract_format'
|
15
|
+
|
16
|
+
use Rack::AbstractFormat
|
17
|
+
run app
|
18
|
+
|
19
|
+
The request:
|
20
|
+
|
21
|
+
GET /path/resource.xml
|
22
|
+
Accept: text/html
|
23
|
+
|
24
|
+
will become:
|
25
|
+
|
26
|
+
GET /path/resource
|
27
|
+
env['HTTP_ACCEPT'] #=> 'application/xml,text/html'
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
def gem_opt
|
2
|
+
defined?(Gem) ? "-rubygems" : ""
|
3
|
+
end
|
4
|
+
|
5
|
+
# --------------------------------------------------
|
6
|
+
# Tests
|
7
|
+
# --------------------------------------------------
|
8
|
+
task(:default => :test)
|
9
|
+
|
10
|
+
desc "Run tests"
|
11
|
+
task(:test) do
|
12
|
+
exit system("ruby #{gem_opt} test/test_abstract_format.rb")
|
13
|
+
end
|
14
|
+
|
15
|
+
# --------------------------------------------------
|
16
|
+
# Docs
|
17
|
+
# --------------------------------------------------
|
18
|
+
desc "Generate YARD Documentation"
|
19
|
+
task(:yardoc) do
|
20
|
+
require 'yard'
|
21
|
+
files = %w( lib/**/*.rb )
|
22
|
+
options = %w( -o doc/yard --readme README --files LICENSE )
|
23
|
+
YARD::CLI::Yardoc.run *(options + files)
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
|
data/examples/app.ru
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# run me with:
|
2
|
+
# $rackup examples/app.ru -p 3000
|
3
|
+
#
|
4
|
+
# and try different url extensions:
|
5
|
+
# localhost:3000/foo.html
|
6
|
+
# localhost:3000/foo.xml
|
7
|
+
# localhost:3000/foo.txt
|
8
|
+
# ...
|
9
|
+
#
|
10
|
+
require 'pathname'
|
11
|
+
require 'rack'
|
12
|
+
|
13
|
+
$:.unshift Pathname(__FILE__).dirname.parent + 'lib'
|
14
|
+
require 'rack/abstract_format'
|
15
|
+
|
16
|
+
class App
|
17
|
+
def call(env)
|
18
|
+
body = <<-TXT
|
19
|
+
env['PATH_INFO'] #=> #{env['PATH_INFO'].inspect}
|
20
|
+
env['HTTP_ACCEPT'] #=> #{env['HTTP_ACCEPT'].inspect}
|
21
|
+
TXT
|
22
|
+
[200, {'Content-Type' => 'text/plain'}, [body]]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
use Rack::AbstractFormat
|
27
|
+
run App.new
|
28
|
+
|
@@ -0,0 +1,22 @@
|
|
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['HTTP_ACCEPT'] = concat(env['HTTP_ACCEPT'], Rack::Mime.mime_type(path.extname, nil))
|
14
|
+
@app.call(env)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
def concat(accept, type)
|
19
|
+
(accept || '').split(',').unshift(type).compact.join(',')
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'rack-abstract-format'
|
3
|
+
s.version = "0.9.8"
|
4
|
+
s.summary = "Rack middleware that abstracts format (extension) away from the path (into env)"
|
5
|
+
s.description = "Rack middleware that abstracts format (extension) away from the path (into env)."
|
6
|
+
s.author = "mynyml"
|
7
|
+
s.email = "mynyml@gmail.com"
|
8
|
+
s.homepage = "http://github.com/mynyml/rack-abstract-format"
|
9
|
+
s.rubyforge_project = "rack-abstract-format"
|
10
|
+
s.has_rdoc = false
|
11
|
+
s.require_path = "lib"
|
12
|
+
s.files = File.read("Manifest").strip.split("\n")
|
13
|
+
|
14
|
+
s.add_development_dependency 'rack'
|
15
|
+
s.add_development_dependency 'nanotest'
|
16
|
+
end
|
@@ -0,0 +1,30 @@
|
|
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
|
+
def get(path, opts={})
|
10
|
+
env = Rack::MockRequest.env_for(path, opts)
|
11
|
+
App.call(env)
|
12
|
+
env
|
13
|
+
end
|
14
|
+
|
15
|
+
# test: strips extension
|
16
|
+
env = get('/path/resource.xml')
|
17
|
+
assert { '/path/resource' == env['PATH_INFO'] }
|
18
|
+
|
19
|
+
# test: doesn't get confused with dots in path
|
20
|
+
env = get('/path.to/resource.xml')
|
21
|
+
assert { '/path.to/resource' == env['PATH_INFO'] }
|
22
|
+
|
23
|
+
# test: prepends requested media type to Accept header
|
24
|
+
env = get('/path/resource.html', 'HTTP_ACCEPT' => 'application/xml')
|
25
|
+
assert { 'text/html,application/xml' == env['HTTP_ACCEPT'] }
|
26
|
+
|
27
|
+
# test: url without extension
|
28
|
+
env = get('/path/resource', 'HTTP_ACCEPT' => 'text/html')
|
29
|
+
assert { 'text/html' == env['HTTP_ACCEPT'] }
|
30
|
+
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
|
3
|
+
require 'rack'
|
4
|
+
require 'nanotest'
|
5
|
+
begin
|
6
|
+
require 'ruby-debug'
|
7
|
+
require 'redgreen' # gem install mynyml-redgreen
|
8
|
+
require 'nanotest/focus' # gem install nanotest_extensions
|
9
|
+
require 'nanotest/stats' # gem install nanotest_extensions
|
10
|
+
rescue LoadError, RuntimeError
|
11
|
+
end
|
12
|
+
|
13
|
+
$:.unshift Pathname(__FILE__).dirname.parent + 'lib'
|
14
|
+
require 'rack/abstract_format'
|
15
|
+
|
16
|
+
include Nanotest
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-abstract-format
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.8
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- mynyml
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-06 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rack
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: nanotest
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
description: Rack middleware that abstracts format (extension) away from the path (into env).
|
36
|
+
email: mynyml@gmail.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files: []
|
42
|
+
|
43
|
+
files:
|
44
|
+
- .gitignore
|
45
|
+
- LICENSE
|
46
|
+
- Manifest
|
47
|
+
- README
|
48
|
+
- Rakefile
|
49
|
+
- examples/app.ru
|
50
|
+
- lib/rack/abstract_format.rb
|
51
|
+
- rack-abstract-format.gemspec
|
52
|
+
- test/test_abstract_format.rb
|
53
|
+
- test/test_helper.rb
|
54
|
+
has_rdoc: false
|
55
|
+
homepage: http://github.com/mynyml/rack-abstract-format
|
56
|
+
licenses: []
|
57
|
+
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: "0"
|
68
|
+
version:
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: "0"
|
74
|
+
version:
|
75
|
+
requirements: []
|
76
|
+
|
77
|
+
rubyforge_project: rack-abstract-format
|
78
|
+
rubygems_version: 1.3.5
|
79
|
+
signing_key:
|
80
|
+
specification_version: 3
|
81
|
+
summary: Rack middleware that abstracts format (extension) away from the path (into env)
|
82
|
+
test_files: []
|
83
|
+
|