mynyml-rack-abstract-format 0.9.4 → 0.9.6

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -1,14 +1,16 @@
1
- SUMMARY
1
+ ===== Summary
2
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.
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.
6
7
 
7
8
  This is especially useful when dealing with routes as it allows a resource to
8
9
  always point to the same action independently from the requested format.
9
10
 
10
- USAGE
11
+ ===== Usage
11
12
 
13
+ require 'rack'
12
14
  require 'rack/abstract_format'
13
15
 
14
16
  use Rack::AbstractFormat
@@ -17,14 +19,9 @@ run app
17
19
  The request:
18
20
 
19
21
  GET /path/resource.xml
22
+ Accept: text/html
20
23
 
21
24
  will become:
22
25
 
23
26
  GET /path/resource
24
- env['request.format'] #=> 'xml'
25
-
26
- TIP
27
-
28
- To make sure there is always an extension, as well as to retrieve format
29
- information from the request's Accept header, use together with rack-contrib's
30
- Rack::AcceptFormat middleware.
27
+ env['HTTP_ACCEPT'] #=> 'application/xml,text/html'
data/Rakefile CHANGED
@@ -22,7 +22,7 @@ end
22
22
 
23
23
  spec = Gem::Specification.new do |s|
24
24
  s.name = 'rack-abstract-format'
25
- s.version = '0.9.4'
25
+ s.version = '0.9.6'
26
26
  s.summary = "Rack middleware that abstracts format (extension) away from the path (into env)"
27
27
  s.description = "Rack middleware that abstracts format (extension) away from the path (into env)"
28
28
  s.author = "Martin Aumont"
data/examples/app.ru CHANGED
@@ -1,15 +1,18 @@
1
1
  # run me with:
2
2
  # $rackup examples/app.ru -p 3000
3
3
  #
4
+ require 'pathname'
4
5
  require 'rubygems'
5
6
  require 'rack'
6
- require 'rack/abstract_format'
7
+
8
+ root = Pathname(__FILE__).dirname.parent.expand_path
9
+ require root + 'lib/rack/abstract_format'
7
10
 
8
11
  class App
9
12
  def call(env)
10
13
  body = <<-TXT
11
- env['PATH_INFO'] #=> #{env['PATH_INFO'].inspect}
12
- env['request.format'] #=> #{env['request.format'].inspect}
14
+ env['PATH_INFO'] #=> #{env['PATH_INFO'].inspect}
15
+ env['HTTP_ACCEPT'] #=> #{env['HTTP_ACCEPT'].inspect}
13
16
  TXT
14
17
  [200, {'Content-Type' => 'text/plain'}, [body]]
15
18
  end
@@ -10,11 +10,16 @@ module Rack
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['Accept'] = concat(env['Accept'], Rack::Mime.mime_type(path.extname))
13
+ env['HTTP_ACCEPT'] = concat(env['HTTP_ACCEPT'], Rack::Mime.mime_type(path.extname))
14
14
  @app.call(env)
15
15
  end
16
16
 
17
17
  private
18
+ # TODO remove duplicates?
19
+ #
20
+ # # actual example; firefox request at /foo.xml:
21
+ # ["application/xml", "text/html", "application/xhtml+xml", "application/xml;q=0.9", "*/*;q=0.8"]
22
+ #
18
23
  def concat(accept, type)
19
24
  (accept || '').split(',').unshift(type).join(',')
20
25
  end
@@ -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.4
4
+ version: 0.9.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Aumont
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-02 00:00:00 -04:00
12
+ date: 2009-06-03 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -25,7 +25,7 @@ class AbstractFormatTest < Test::Unit::TestCase
25
25
  end
26
26
 
27
27
  test "prepends requested media type to Accept header" do
28
- env = get('/path/resource.html', 'Accept' => 'application/xml')
29
- assert_equal 'text/html,application/xml' , env['Accept']
28
+ env = get('/path/resource.html', 'HTTP_ACCEPT' => 'application/xml')
29
+ assert_equal 'text/html,application/xml' , env['HTTP_ACCEPT']
30
30
  end
31
31
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mynyml-rack-abstract-format
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.4
4
+ version: 0.9.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Aumont
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-01 21:00:00 -07:00
12
+ date: 2009-06-02 21:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15