mynyml-rack-respond_to 0.9
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/LICENSE +19 -0
- data/README +48 -0
- data/Rakefile +66 -0
- data/examples/recommended_use.ru +38 -0
- data/examples/simple_app.ru +36 -0
- data/lib/rack/respond_to.rb +82 -0
- data/rack-respond_to.gemspec +67 -0
- data/test/test_helper.rb +20 -0
- data/test/test_respond_to.rb +86 -0
- metadata +65 -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,48 @@
|
|
1
|
+
SUMMARY
|
2
|
+
|
3
|
+
Rack convenience middleware that allows triggering different actions based on
|
4
|
+
requested format. Standalone version of the equivalent Rails functionality.
|
5
|
+
|
6
|
+
INSTALLATION
|
7
|
+
|
8
|
+
sudo gem install mynyml-rack-respond_to --source=http://gems.github.com/
|
9
|
+
|
10
|
+
USAGE
|
11
|
+
|
12
|
+
require 'rubygems'
|
13
|
+
require 'rack'
|
14
|
+
require 'rack/respond_to'
|
15
|
+
|
16
|
+
class App
|
17
|
+
include Rack::RespondTo
|
18
|
+
|
19
|
+
def call(env)
|
20
|
+
# simply pass in the env if another middleware already added
|
21
|
+
# the format to env['request.format']
|
22
|
+
#Rack::RespondTo.env = env
|
23
|
+
|
24
|
+
# otherwise, you can assign it directly
|
25
|
+
Rack::RespondTo.format = 'html'
|
26
|
+
|
27
|
+
body = respond_to do |format|
|
28
|
+
format.html { '<em>html</em>' }
|
29
|
+
format.xml { '<body>xml</body>' }
|
30
|
+
end
|
31
|
+
|
32
|
+
content_type = Rack::RespondTo.mime_type
|
33
|
+
[200, {'Content-Type' => content_type}, [body]]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
run App.new
|
38
|
+
|
39
|
+
TIP
|
40
|
+
|
41
|
+
To fully extract the requested format, use:
|
42
|
+
|
43
|
+
Rack::AcceptFormat (sudo gem install rack-rack-contrib --source=http://gems.github.com/)
|
44
|
+
Rack::AbstractFormat (sudo gem install mynyml-rack-abstract-format --source=http://gems.github.com/)
|
45
|
+
|
46
|
+
These will allow using only <tt>Rack::RespondTo.env = env</tt> to set up ResondTo.
|
47
|
+
|
48
|
+
See examples/recommended_use.ru for a complete example.
|
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-respond_to'
|
25
|
+
s.version = '0.9'
|
26
|
+
s.summary = "Rack middleware port of Rails's respond_to feature"
|
27
|
+
s.description = "Rack middleware port of Rails's respond_to feature"
|
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
|
+
}
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# install required dependencies:
|
2
|
+
#
|
3
|
+
# $sudo gem install rack-rack-contrib --source=http://gems.github.com/
|
4
|
+
# $sudo gem install mynyml-rack-abstract-format --source=http://gems.github.com/
|
5
|
+
#
|
6
|
+
# and run me with:
|
7
|
+
# $rackup examples/recommended_use.ru -p 8080
|
8
|
+
#
|
9
|
+
require 'rubygems'
|
10
|
+
require 'rack'
|
11
|
+
require 'rack/contrib' # for Rack::AcceptFormat
|
12
|
+
require 'rack/abstract_format'
|
13
|
+
require 'rack/respond_to'
|
14
|
+
|
15
|
+
class App
|
16
|
+
include Rack::RespondTo
|
17
|
+
|
18
|
+
def call(env)
|
19
|
+
Rack::RespondTo.env = env
|
20
|
+
request = Rack::Request.new(env)
|
21
|
+
|
22
|
+
body = case request.path_info
|
23
|
+
when '/foo'
|
24
|
+
respond_to do |format|
|
25
|
+
format.html { '<em>html</em>' }
|
26
|
+
format.xml { '<body>xml</body>' }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
content_type = Rack::RespondTo.mime_type
|
31
|
+
[200, {'Content-Type' => content_type}, [body]]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
use Rack::AcceptFormat
|
36
|
+
use Rack::AbstractFormat
|
37
|
+
run App.new
|
38
|
+
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# run me with:
|
2
|
+
# $rackup examples/simple_app.ru -p 8080
|
3
|
+
#
|
4
|
+
require 'pathname'
|
5
|
+
root = Pathname(__FILE__).dirname.parent.expand_path
|
6
|
+
$:.unshift(root.join('lib'))
|
7
|
+
|
8
|
+
require 'rubygems'
|
9
|
+
require 'rack'
|
10
|
+
require 'rack/respond_to'
|
11
|
+
|
12
|
+
class App
|
13
|
+
include Rack::RespondTo
|
14
|
+
|
15
|
+
def call(env)
|
16
|
+
# simply pass in the env if another middleware already added
|
17
|
+
# the format to env['request.format']
|
18
|
+
#Rack::RespondTo.env = env
|
19
|
+
|
20
|
+
# otherwise, you can assign it directly
|
21
|
+
Rack::RespondTo.format = 'html'
|
22
|
+
|
23
|
+
body = case env['PATH_INFO']
|
24
|
+
when '/'
|
25
|
+
respond_to do |format|
|
26
|
+
format.html { '<em>html</em>' }
|
27
|
+
format.xml { '<body>xml</body>' }
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
content_type = Rack::RespondTo.mime_type
|
32
|
+
[200, {'Content-Type' => content_type}, [body]]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
run App.new
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module Rack
|
2
|
+
|
3
|
+
# Based on Rails's API, and sinatra-respond_to (http://github.com/cehoffman/sinatra-respond_to)
|
4
|
+
#
|
5
|
+
# See examples/ directory for code examples.
|
6
|
+
module RespondTo
|
7
|
+
class << self
|
8
|
+
# Assign the environment directly if it contains the format in
|
9
|
+
# env['request.format']. This is useful in conjunction with other
|
10
|
+
# middlewares that store the format in the env key.
|
11
|
+
attr_accessor :env
|
12
|
+
|
13
|
+
# If used completely standalone, you can assign the request format directly.
|
14
|
+
#
|
15
|
+
# RespondTo.format = 'xml'
|
16
|
+
attr_writer :format
|
17
|
+
|
18
|
+
# Requested format
|
19
|
+
def format
|
20
|
+
(@format || self.env['request.format']).to_s.strip.downcase.sub(/^\./,'')
|
21
|
+
end
|
22
|
+
|
23
|
+
def included(base) #:nodoc:
|
24
|
+
base.extend(ClassMethods)
|
25
|
+
base.class_eval do
|
26
|
+
include InstanceMethods
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# Convenience method that returns the mime type of the given format
|
31
|
+
# (similar to Rack::Mime.mime_type).
|
32
|
+
#
|
33
|
+
# If format argument is omitted, will return the mime type for
|
34
|
+
# RespondTo.format (i.e. equivelent to RespondTo.mime_type(RespondTo.format)).
|
35
|
+
# Useful for setting content type:
|
36
|
+
#
|
37
|
+
# ===== Example
|
38
|
+
#
|
39
|
+
# [200, {'Content-Type' => Rack::RespondTo.mime_type}, [body]]
|
40
|
+
def mime_type(format = nil)
|
41
|
+
format ||= self.format
|
42
|
+
ext = format.sub(/^\./,'').insert(0,'.')
|
43
|
+
Rack::Mime.mime_type(ext)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
module InstanceMethods
|
48
|
+
# Delegates to the equivalent class method.
|
49
|
+
def respond_to(&block)
|
50
|
+
self.class.respond_to(&block)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
module ClassMethods
|
55
|
+
# Allows defining different actions and returns the one which corresponds
|
56
|
+
# to the current RespondTo.format.
|
57
|
+
#
|
58
|
+
# ===== Example
|
59
|
+
#
|
60
|
+
# RespondTo.format = 'html'
|
61
|
+
#
|
62
|
+
# respond_to do |format|
|
63
|
+
# format.html { '<em>html</em>' }
|
64
|
+
# format.xml { '<content>xml</content>' }
|
65
|
+
# end
|
66
|
+
# #=> '<em>html</em>'
|
67
|
+
#
|
68
|
+
def respond_to(&block)
|
69
|
+
format = Format.new
|
70
|
+
block.call(format)
|
71
|
+
handler = format[RespondTo.mime_type]
|
72
|
+
handler.nil? ? nil : handler.call
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
class Format < Hash #:nodoc:
|
77
|
+
def method_missing(format, *args, &handler)
|
78
|
+
self[RespondTo.mime_type(format.to_s)] = handler
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-respond_to
|
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-22 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Rack middleware port of Rails's respond_to feature
|
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_respond_to.rb
|
28
|
+
- test/test_helper.rb
|
29
|
+
- examples
|
30
|
+
- examples/simple_app.ru
|
31
|
+
- examples/recommended_use.ru
|
32
|
+
- lib
|
33
|
+
- lib/rack
|
34
|
+
- lib/rack/respond_to.rb
|
35
|
+
- rack-respond_to.gemspec
|
36
|
+
- LICENSE
|
37
|
+
- README
|
38
|
+
has_rdoc: true
|
39
|
+
homepage: ""
|
40
|
+
licenses: []
|
41
|
+
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
version:
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
version:
|
59
|
+
requirements: []
|
60
|
+
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 1.3.3
|
63
|
+
signing_key:
|
64
|
+
specification_version: 3
|
65
|
+
summary: Rack middleware port of Rails's respond_to feature
|
66
|
+
test_files: []
|
67
|
+
|
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/respond_to'
|
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
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'test/test_helper'
|
2
|
+
|
3
|
+
class App
|
4
|
+
include Rack::RespondTo
|
5
|
+
end
|
6
|
+
|
7
|
+
class TestRespondTo < Test::Unit::TestCase
|
8
|
+
|
9
|
+
def setup
|
10
|
+
Rack::RespondTo.format = nil
|
11
|
+
end
|
12
|
+
|
13
|
+
## api
|
14
|
+
|
15
|
+
test "format accessor" do
|
16
|
+
Rack::RespondTo.format = 'xml'
|
17
|
+
end
|
18
|
+
|
19
|
+
test "env accessor" do
|
20
|
+
Rack::RespondTo.env = {}
|
21
|
+
end
|
22
|
+
|
23
|
+
test "mixin injects respond_to class method" do
|
24
|
+
assert App.respond_to?(:respond_to)
|
25
|
+
end
|
26
|
+
|
27
|
+
test "mixin injects respond_to instance method" do
|
28
|
+
assert App.new.respond_to?(:respond_to)
|
29
|
+
end
|
30
|
+
|
31
|
+
## format
|
32
|
+
|
33
|
+
test "format is extracted from env" do
|
34
|
+
Rack::RespondTo.env = {'request.format' => 'xml'}
|
35
|
+
assert_equal 'xml', Rack::RespondTo.format
|
36
|
+
end
|
37
|
+
|
38
|
+
test "explicitly specified format takes precedence of env's" do
|
39
|
+
Rack::RespondTo.env = {'request.format' => 'xml'}
|
40
|
+
Rack::RespondTo.format = 'html'
|
41
|
+
assert_equal 'html', Rack::RespondTo.format
|
42
|
+
end
|
43
|
+
|
44
|
+
test "format is normalized" do
|
45
|
+
Rack::RespondTo.format = '.html'
|
46
|
+
assert_equal 'html', Rack::RespondTo.format
|
47
|
+
|
48
|
+
Rack::RespondTo.format = :html
|
49
|
+
assert_equal 'html', Rack::RespondTo.format
|
50
|
+
|
51
|
+
Rack::RespondTo.format = ' html '
|
52
|
+
assert_equal 'html', Rack::RespondTo.format
|
53
|
+
|
54
|
+
Rack::RespondTo.format = 'HTML'
|
55
|
+
assert_equal 'html', Rack::RespondTo.format
|
56
|
+
end
|
57
|
+
|
58
|
+
## respond_to
|
59
|
+
|
60
|
+
test "respond_to returns block for requested format" do
|
61
|
+
Rack::RespondTo.format = 'xml'
|
62
|
+
response = App.respond_to do |format|
|
63
|
+
format.xml { 'xml' }
|
64
|
+
format.txt { 'txt' }
|
65
|
+
end
|
66
|
+
assert_equal 'xml', response
|
67
|
+
end
|
68
|
+
|
69
|
+
test "respond_to returns nil when no format matches" do
|
70
|
+
Rack::RespondTo.format = 'html'
|
71
|
+
response = App.respond_to do |format|
|
72
|
+
format.xml { 'xml' }
|
73
|
+
format.txt { 'txt' }
|
74
|
+
end
|
75
|
+
assert_equal nil, response
|
76
|
+
end
|
77
|
+
|
78
|
+
test "respond_to handles synonymous formats" do
|
79
|
+
Rack::RespondTo.format = 'htm' # htm should trigger both htm and html
|
80
|
+
response = App.respond_to do |format|
|
81
|
+
format.html { 'html' }
|
82
|
+
format.json { 'json' }
|
83
|
+
end
|
84
|
+
assert_equal 'html', response
|
85
|
+
end
|
86
|
+
end
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mynyml-rack-respond_to
|
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-21 21:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Rack middleware port of Rails's respond_to feature
|
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_respond_to.rb
|
28
|
+
- test/test_helper.rb
|
29
|
+
- examples
|
30
|
+
- examples/simple_app.ru
|
31
|
+
- examples/recommended_use.ru
|
32
|
+
- lib
|
33
|
+
- lib/rack
|
34
|
+
- lib/rack/respond_to.rb
|
35
|
+
- rack-respond_to.gemspec
|
36
|
+
- LICENSE
|
37
|
+
- README
|
38
|
+
has_rdoc: true
|
39
|
+
homepage: ""
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
version:
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.2.0
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: Rack middleware port of Rails's respond_to feature
|
64
|
+
test_files: []
|
65
|
+
|