rack-abstract-format 0.9.8 → 0.9.9

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1 +1,2 @@
1
1
  pkg/
2
+ *.gem
data/Manifest CHANGED
@@ -1,10 +1,11 @@
1
1
  .gitignore
2
2
  LICENSE
3
3
  Manifest
4
- README
4
+ README.md
5
5
  Rakefile
6
6
  examples/app.ru
7
7
  lib/rack/abstract_format.rb
8
8
  rack-abstract-format.gemspec
9
+ specs.watchr
9
10
  test/test_abstract_format.rb
10
11
  test/test_helper.rb
@@ -0,0 +1,55 @@
1
+ Summary
2
+ -------
3
+
4
+ Strips the extension from the requested path (`env['PATH_INFO']`), casts it to
5
+ media type, and prepends it to `env['HTTP_ACCEPT']`. This allows dealing with path
6
+ information separately from format information, which are often different
7
+ concerns.
8
+
9
+ This is especially useful when dealing with routes as it allows a resource to
10
+ always point to the same action independently of the requested format.
11
+
12
+ Usage
13
+ -----
14
+
15
+ require 'rack'
16
+ require 'rack/abstract_format'
17
+
18
+ use Rack::AbstractFormat
19
+ run app
20
+
21
+ The request:
22
+
23
+ GET /path/resource.xml
24
+ Accept: text/html
25
+
26
+ will become:
27
+
28
+ GET /path/resource
29
+ env['HTTP_ACCEPT'] #=> 'application/xml,text/html'
30
+
31
+ AbstractFormat also accepts an optional argument, used to set the default
32
+ format that should be assumed when none is specified on the URL:
33
+
34
+ use Rack::AbstractFormat, 'text/html'
35
+ #=> GET /path/resource # requested
36
+ #=> GET /path/resource.html # assumed
37
+
38
+ See Also
39
+ --------
40
+
41
+ * [simple_router][1]
42
+ * [rack-respond_to][2]
43
+
44
+ Links
45
+ -----
46
+
47
+ * code: <http://github.com/mynyml/rack-abstract-format>
48
+ * docs: <http://rdoc.info/projects/mynyml/rack-abstract-format>
49
+ * wiki: <http://wiki.github.com/mynyml/rack-abstract-format>
50
+ * bugs: <http://github.com/mynyml/rack-abstract-format/issues>
51
+
52
+
53
+ [1]: http://github.com/mynyml/simple_router
54
+ [2]: http://github.com/mynyml/rack-respond_to
55
+
data/Rakefile CHANGED
@@ -23,5 +23,3 @@ task(:yardoc) do
23
23
  YARD::CLI::Yardoc.run *(options + files)
24
24
  end
25
25
 
26
-
27
-
@@ -3,14 +3,14 @@ require 'pathname'
3
3
  module Rack
4
4
  class AbstractFormat
5
5
 
6
- def initialize(app)
7
- @app = app
6
+ def initialize(app, default = nil)
7
+ @app, @default = app, default
8
8
  end
9
9
 
10
10
  def call(env)
11
11
  path = Pathname(env['PATH_INFO'])
12
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))
13
+ env['HTTP_ACCEPT'] = concat(env['HTTP_ACCEPT'], Rack::Mime.mime_type(path.extname, @default))
14
14
  @app.call(env)
15
15
  end
16
16
 
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'rack-abstract-format'
3
- s.version = "0.9.8"
3
+ s.version = "0.9.9"
4
4
  s.summary = "Rack middleware that abstracts format (extension) away from the path (into env)"
5
5
  s.description = "Rack middleware that abstracts format (extension) away from the path (into env)."
6
6
  s.author = "mynyml"
@@ -0,0 +1,20 @@
1
+ # Run me with:
2
+ # $ watchr specs.watchr
3
+
4
+ # --------------------------------------------------
5
+ # Watchr Rules
6
+ # --------------------------------------------------
7
+ watch( '^(lib|test)/.*\.rb' ) { rake }
8
+
9
+ # --------------------------------------------------
10
+ # Signal Handling
11
+ # --------------------------------------------------
12
+ Signal.trap('QUIT') { rake } # Ctrl-\
13
+ Signal.trap('INT' ) { abort("\n") } # Ctrl-C
14
+
15
+ # --------------------------------------------------
16
+ # Helpers
17
+ # --------------------------------------------------
18
+ def rake(task='')
19
+ system "rake -s #{task}"
20
+ end
@@ -1,6 +1,6 @@
1
1
  require 'test/test_helper'
2
2
 
3
- App = Rack::Builder.new {
3
+ @app = Rack::Builder.new {
4
4
  use Rack::Lint
5
5
  use Rack::AbstractFormat
6
6
  run lambda {|env| [200, {'Content-Type' => 'text/html'}, ['']] }
@@ -8,7 +8,7 @@ App = Rack::Builder.new {
8
8
 
9
9
  def get(path, opts={})
10
10
  env = Rack::MockRequest.env_for(path, opts)
11
- App.call(env)
11
+ @app.call(env)
12
12
  env
13
13
  end
14
14
 
@@ -28,3 +28,13 @@ assert { 'text/html,application/xml' == env['HTTP_ACCEPT'] }
28
28
  env = get('/path/resource', 'HTTP_ACCEPT' => 'text/html')
29
29
  assert { 'text/html' == env['HTTP_ACCEPT'] }
30
30
 
31
+ # test: default format
32
+ @app = Rack::Builder.new {
33
+ use Rack::Lint
34
+ use Rack::AbstractFormat, 'text/html'
35
+ run lambda {|env| [200, {'Content-Type' => 'text/html'}, ['']] }
36
+ }.to_app
37
+
38
+ env = get('/path/resource', 'HTTP_ACCEPT' => 'application/xml')
39
+ assert { 'text/html,application/xml' == env['HTTP_ACCEPT'] }
40
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-abstract-format
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.8
4
+ version: 0.9.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - mynyml
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-06 00:00:00 -05:00
12
+ date: 2010-01-07 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -44,11 +44,12 @@ files:
44
44
  - .gitignore
45
45
  - LICENSE
46
46
  - Manifest
47
- - README
47
+ - README.md
48
48
  - Rakefile
49
49
  - examples/app.ru
50
50
  - lib/rack/abstract_format.rb
51
51
  - rack-abstract-format.gemspec
52
+ - specs.watchr
52
53
  - test/test_abstract_format.rb
53
54
  - test/test_helper.rb
54
55
  has_rdoc: false
data/README DELETED
@@ -1,27 +0,0 @@
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'