rack_content_type_default 1.1.0
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.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/.travis.yml +24 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +5 -0
- data/MIT-LICENSE +20 -0
- data/README.md +47 -0
- data/Rakefile +8 -0
- data/lib/rack/content_type_default.rb +52 -0
- data/lib/rack/content_type_default/version.rb +7 -0
- data/rack_content_type_default.gemspec +26 -0
- metadata +109 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: eeb07d130a6bfcafad8b67813d926fcbff1ade28d569556ab6fac578537fd0c9
|
4
|
+
data.tar.gz: a5bcefef85708443b8b4f9da0eb56b641f7793530a88800a20bbfc59ee3a1a03
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 71e6316f50a15373b8488e0e23c77dce1baab823a47c85a3c1397e26edbb10d6763b00696e24934ad2d7a0762e006afa7cf83334b05ff23f54f1d5312e05df76
|
7
|
+
data.tar.gz: ced08988af4f4bed2b09d3ae279ea23c0439d596fdd8dee3af1d2c3cd5a2dac2176e4db43ccc01f13ac6e5c9365e555a214b60c7fcd67cd6a23162537354390e
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
dist: focal
|
2
|
+
language: ruby
|
3
|
+
cache: bundler
|
4
|
+
|
5
|
+
rvm:
|
6
|
+
- 2.5
|
7
|
+
- 2.6
|
8
|
+
- 2.7
|
9
|
+
- 3.0
|
10
|
+
|
11
|
+
before_install:
|
12
|
+
- gem update --system --force -N > /dev/null && echo "Rubygems version $(gem --version)"
|
13
|
+
- gem install bundler --force -N && bundle --version
|
14
|
+
|
15
|
+
install: BUNDLE_JOBS=4 bundle install
|
16
|
+
|
17
|
+
script: bundle exec rspec
|
18
|
+
|
19
|
+
deploy:
|
20
|
+
edge: true # opt in to dpl v2
|
21
|
+
provider: rubygems
|
22
|
+
on:
|
23
|
+
tags: true
|
24
|
+
condition: $TRAVIS_RUBY_VERSION == 3.0
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2014-2021 Medidata Solutions Worldwide
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all 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
|
19
|
+
THE SOFTWARE.
|
20
|
+
|
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# Rack::ContentTypeDefault
|
2
|
+
|
3
|
+
Rack::ContentTypeDefault is a tiny piece of middleware that sets default `CONTENT-TYPE:` header when it isn't present
|
4
|
+
for specific requests. It checks if a content type is set and does not override the existing one.
|
5
|
+
|
6
|
+
The following options may be specified:
|
7
|
+
|
8
|
+
1. Request method(s) on which the content type is applied. The default value is post.
|
9
|
+
2. Desired content type. The default value is 'application/json'.
|
10
|
+
3. Paths on which the content type is applied. The default is all paths when none are specified.
|
11
|
+
4. A boolean value to indicate whether or not to apply 'application/xml' or 'application/json' if the path ends in .xml
|
12
|
+
or .json, respectively. The default value is false.
|
13
|
+
|
14
|
+
|
15
|
+
The code has been copied and adapted from https://gist.github.com/tstachl/6264249
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
Add the following line to your application's Gemfile:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
gem 'rack_content_type_default', '~> 1.0'
|
23
|
+
```
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
Examples:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
require 'rack/content-type-default'
|
31
|
+
config.middleware.use Rack::ContentTypeDefault, :post, 'application/x-www-form-urlencoded'
|
32
|
+
```
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
require 'rack/content-type-default'
|
36
|
+
config.middleware.use Rack::ContentTypeDefault, [:get, :post], 'application/xml', '/authenticate.xml'
|
37
|
+
```
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
require 'rack/content-type-default'
|
41
|
+
config.middleware.use Rack::ContentTypeDefault, :post, 'application/json', ['/authenticate.json', '/show'], true
|
42
|
+
```
|
43
|
+
|
44
|
+
## Why
|
45
|
+
|
46
|
+
It is useful if you want to make your application opinionated about the content type it expects from clients when not
|
47
|
+
provided. In addition it bridges a gap between test and production environments if the clients differ.
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# This is a simple Rack Middleware to set the request content type for specific routes. It checks if a content type is
|
2
|
+
# set and does not override the existing one. The options allow you to specify the request methods and the content type
|
3
|
+
# to be set. The options also allow you to set a default content type only on select paths. In addition, the options
|
4
|
+
# allow you to set content type to to application/json or application/xml if possible based on the path ending.
|
5
|
+
# Copied and adapted from https://gist.github.com/tstachl/6264249
|
6
|
+
module Rack
|
7
|
+
class ContentTypeDefault
|
8
|
+
def initialize(app, methods = [:post], content_type = 'application/json', paths = 'all', use_path_hint = false)
|
9
|
+
@app = app
|
10
|
+
@methods = [*methods].map { |item| item.to_s.upcase }
|
11
|
+
@content_type = content_type
|
12
|
+
@paths = [*paths]
|
13
|
+
@use_path_hint = use_path_hint
|
14
|
+
end
|
15
|
+
|
16
|
+
def call(env)
|
17
|
+
req = Rack::Request.new(env)
|
18
|
+
if override_content_type?(req)
|
19
|
+
env['CONTENT_TYPE'] = @use_path_hint ? determine_content_type(req.env['PATH_INFO']) : @content_type
|
20
|
+
end
|
21
|
+
@app.call(env)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
# Determine whether or not to override content type
|
27
|
+
def override_content_type?(req)
|
28
|
+
(req.content_type.nil? || req.content_type.empty?) && match_method?(req.request_method) &&
|
29
|
+
match_path?(req.env['PATH_INFO'])
|
30
|
+
end
|
31
|
+
|
32
|
+
# Match the method on the request with the methods specified or the default.
|
33
|
+
# If @methods was provided as empty, content type will not be set.
|
34
|
+
def match_method?(method)
|
35
|
+
@methods.include?(method)
|
36
|
+
end
|
37
|
+
|
38
|
+
# Match the path on the request with the paths specified. Content type is set only if a match is found.
|
39
|
+
# If @paths was provided with 'all' as the first element (default), content type will be set for all requests.
|
40
|
+
# If @paths was explicitly set to empty, content type will not be set.
|
41
|
+
def match_path?(filter)
|
42
|
+
@paths.first == 'all' || @paths.any? { |path| /#{path}$/.match?(filter) || /#{path}\./.match?(filter) }
|
43
|
+
end
|
44
|
+
|
45
|
+
# Determine if content type can be set to application/json or application/xml based on the path ending.
|
46
|
+
# If the path does not end in xml or json, content type is set to the given default.
|
47
|
+
def determine_content_type(path_info)
|
48
|
+
inferred = path_info.split('.').last
|
49
|
+
%w[json xml].include?(inferred) ? "application/#{inferred}" : @content_type
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/rack/content_type_default/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'rack_content_type_default'
|
7
|
+
s.version = Rack::ContentTypeDefault::VERSION
|
8
|
+
s.authors = ['Purnima Mavinkurve', 'Connor Savage']
|
9
|
+
s.email = ['pmavinkurve@mdsol.com']
|
10
|
+
s.homepage = 'https://github.com/mdsol/rack_content_type_default'
|
11
|
+
s.summary = 'Rack Middleware for setting content type when not provided'
|
12
|
+
s.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
|
13
|
+
|
14
|
+
# Specify which files should be added to the gem when it is released.
|
15
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
16
|
+
s.files = Dir.chdir(File.expand_path(__dir__)) do
|
17
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
|
18
|
+
end
|
19
|
+
s.require_paths = ['lib']
|
20
|
+
|
21
|
+
s.add_runtime_dependency 'rack'
|
22
|
+
|
23
|
+
s.add_development_dependency 'rack-test'
|
24
|
+
s.add_development_dependency 'rspec'
|
25
|
+
s.add_development_dependency 'simplecov'
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack_content_type_default
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Purnima Mavinkurve
|
8
|
+
- Connor Savage
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2021-02-23 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rack
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ">="
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '0'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rack-test
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rspec
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: simplecov
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
description:
|
71
|
+
email:
|
72
|
+
- pmavinkurve@mdsol.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".travis.yml"
|
79
|
+
- CHANGELOG.md
|
80
|
+
- Gemfile
|
81
|
+
- MIT-LICENSE
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- lib/rack/content_type_default.rb
|
85
|
+
- lib/rack/content_type_default/version.rb
|
86
|
+
- rack_content_type_default.gemspec
|
87
|
+
homepage: https://github.com/mdsol/rack_content_type_default
|
88
|
+
licenses: []
|
89
|
+
metadata: {}
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: 2.5.0
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubygems_version: 3.1.2
|
106
|
+
signing_key:
|
107
|
+
specification_version: 4
|
108
|
+
summary: Rack Middleware for setting content type when not provided
|
109
|
+
test_files: []
|