rack-accept-header-updater 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Cyril Rohr
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,7 @@
1
+ = rack-accept-header-updater
2
+
3
+ A Rack middleware for automatically removing file extensions from URIs, and update the Accept HTTP Header accordingly.
4
+
5
+ == Copyright
6
+
7
+ Copyright (c) 2009 Cyril Rohr. See LICENSE for details.
@@ -0,0 +1,49 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "rack-accept-header-updater"
8
+ gem.summary = %Q{A Rack middleware for automatically removing file extensions from URIs, and update the Accept HTTP Header accordingly.}
9
+ gem.description = %Q{A Rack middleware for automatically removing file extensions from URIs, and update the Accept HTTP Header accordingly.}
10
+ gem.email = "cyril.rohr@gmail.com"
11
+ gem.homepage = "http://github.com/crohr/rack-accept-header-updater"
12
+ gem.authors = ["Cyril Rohr"]
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'spec/rake/spectask'
21
+ Spec::Rake::SpecTask.new(:spec) do |spec|
22
+ spec.libs << 'lib' << 'spec'
23
+ spec.spec_files = FileList['spec/**/*_spec.rb']
24
+ end
25
+
26
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
27
+ spec.libs << 'lib' << 'spec'
28
+ spec.pattern = 'spec/**/*_spec.rb'
29
+ spec.rcov = true
30
+ end
31
+
32
+
33
+
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ if File.exist?('VERSION')
40
+ version = File.read('VERSION')
41
+ else
42
+ version = ""
43
+ end
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "rack-accept-header-updater #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,43 @@
1
+ module Rack
2
+ #
3
+ # A Rack middleware for automatically removing file extensions from URIs, and update the Accept HTTP Header accordingly.
4
+ #
5
+ # e.g.:
6
+ # GET /some/resource.json HTTP/1.1
7
+ # Accept: */*
8
+ # ->
9
+ # GET /some/resource HTTP/1.1
10
+ # Accept: application/json, */*
11
+ #
12
+ # You can add custom types with this kind of function (taken from sinatra):
13
+ # def mime(ext, type)
14
+ # ext = ".#{ext}" unless ext.to_s[0] == ?.
15
+ # Rack::Mime::MIME_TYPES[ext.to_s] = type
16
+ # end
17
+ # then:
18
+ # mime :my_custom_extension_v1, 'application/vnd.com.example.MyCustomExtension+json;level=1'
19
+ # results in the following behaviour:
20
+ # GET /some/resource.my_custom_extension_v1 HTTP/1.1
21
+ # Accept: */*
22
+ # ->
23
+ # GET /some/resource HTTP/1.1
24
+ # Accept: application/vnd.com.example.MyCustomExtension+json;level=1, */*
25
+ #
26
+ class AcceptHeaderUpdater
27
+
28
+ def initialize(app, options = {})
29
+ @app = app
30
+ end
31
+
32
+ def call(env)
33
+ req = Rack::Request.new(env)
34
+ unless (ext = ::File.extname(req.path_info)).empty?
35
+ if (mime_type = Rack::Mime::MIME_TYPES[ext.downcase])
36
+ env['HTTP_ACCEPT'] = [mime_type, env['HTTP_ACCEPT']].join(",")
37
+ end
38
+ req.path_info.gsub!(/#{ext}$/, '')
39
+ end
40
+ @app.call(env)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,50 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{rack-accept-header-updater}
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Cyril Rohr"]
12
+ s.date = %q{2009-10-21}
13
+ s.description = %q{A Rack middleware for automatically removing file extensions from URIs, and update the Accept HTTP Header accordingly.}
14
+ s.email = %q{cyril.rohr@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/rack/accept_header_updater.rb",
27
+ "rack-accept-header-updater.gemspec",
28
+ "spec/rack_accept_header_updater_spec.rb",
29
+ "spec/spec_helper.rb"
30
+ ]
31
+ s.homepage = %q{http://github.com/crohr/rack-accept-header-updater}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.5}
35
+ s.summary = %q{A Rack middleware for automatically removing file extensions from URIs, and update the Accept HTTP Header accordingly.}
36
+ s.test_files = [
37
+ "spec/rack_accept_header_updater_spec.rb",
38
+ "spec/spec_helper.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
46
+ else
47
+ end
48
+ else
49
+ end
50
+ end
@@ -0,0 +1,46 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ def mime(ext, type)
4
+ ext = ".#{ext}" unless ext.to_s[0] == ?.
5
+ Rack::Mime::MIME_TYPES[ext.to_s] = type
6
+ end
7
+
8
+ describe Rack::AcceptHeaderUpdater do
9
+ before(:each) do
10
+ Rack::Mime::MIME_TYPES.clear
11
+ end
12
+ it "should add the correct mime type to the list of accepted types when a known extension is given" do
13
+ mime :json, 'application/json'
14
+ app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, [[env['HTTP_ACCEPT'], env['PATH_INFO']].join("|")]] }
15
+ request = Rack::MockRequest.env_for("/some/resource.json", :input => "foo=bar", 'HTTP_ACCEPT' => '*/*')
16
+ status, headers, body = Rack::AcceptHeaderUpdater.new(app).call(request)
17
+ body.should == ["application/json,*/*|/some/resource"]
18
+ end
19
+ it "should remove the file extension even if there are no mime types associated" do
20
+ app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, [[env['HTTP_ACCEPT'], env['PATH_INFO']].join("|")]] }
21
+ request = Rack::MockRequest.env_for("/some/resource.json", :input => "foo=bar", 'HTTP_ACCEPT' => '*/*')
22
+ status, headers, body = Rack::AcceptHeaderUpdater.new(app).call(request)
23
+ body.should == ["*/*|/some/resource"]
24
+ end
25
+ it "should be case insensitive for extensions" do
26
+ mime :json, 'application/json'
27
+ app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, [[env['HTTP_ACCEPT'], env['PATH_INFO']].join("|")]] }
28
+ request = Rack::MockRequest.env_for("/some/resource.JSoN", :input => "foo=bar", 'HTTP_ACCEPT' => '*/*')
29
+ status, headers, body = Rack::AcceptHeaderUpdater.new(app).call(request)
30
+ body.should == ["application/json,*/*|/some/resource"]
31
+ end
32
+ it "should not modify the Accept Header if no extension is given" do
33
+ app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, [[env['HTTP_ACCEPT'], env['PATH_INFO']].join("|")]] }
34
+ request = Rack::MockRequest.env_for("/some/resource", :input => "foo=bar", 'HTTP_ACCEPT' => 'application/json, */*')
35
+ status, headers, body = Rack::AcceptHeaderUpdater.new(app).call(request)
36
+ body.should == ["application/json, */*|/some/resource"]
37
+ end
38
+ it "should have no problem with complex mime types" do
39
+ mime :collection_json_v1, 'application/vnd.com.example.Collection+json;level=1'
40
+ app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, [[env['HTTP_ACCEPT'], env['PATH_INFO']].join("|")]] }
41
+ request = Rack::MockRequest.env_for("/some/resource.collection_json_v1", :input => "foo=bar", 'HTTP_ACCEPT' => '*/*')
42
+ status, headers, body = Rack::AcceptHeaderUpdater.new(app).call(request)
43
+ body.should == ["application/vnd.com.example.Collection+json;level=1,*/*|/some/resource"]
44
+ end
45
+ end
46
+
@@ -0,0 +1,11 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'rack'
4
+ require 'rack/accept_header_updater'
5
+ require 'spec'
6
+ require 'spec/autorun'
7
+
8
+
9
+ Spec::Runner.configure do |config|
10
+
11
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-accept-header-updater
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Cyril Rohr
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-21 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A Rack middleware for automatically removing file extensions from URIs, and update the Accept HTTP Header accordingly.
17
+ email: cyril.rohr@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION
32
+ - lib/rack/accept_header_updater.rb
33
+ - rack-accept-header-updater.gemspec
34
+ - spec/rack_accept_header_updater_spec.rb
35
+ - spec/spec_helper.rb
36
+ has_rdoc: true
37
+ homepage: http://github.com/crohr/rack-accept-header-updater
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options:
42
+ - --charset=UTF-8
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.3.5
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: A Rack middleware for automatically removing file extensions from URIs, and update the Accept HTTP Header accordingly.
64
+ test_files:
65
+ - spec/rack_accept_header_updater_spec.rb
66
+ - spec/spec_helper.rb