format_restricter_rails 1.0.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 +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +74 -0
- data/Rakefile +11 -0
- data/bin/console +13 -0
- data/bin/setup +8 -0
- data/lib/format_restricter_rails/engine.rb +4 -0
- data/lib/format_restricter_rails/includes.rb +31 -0
- data/lib/format_restricter_rails/railtie.rb +38 -0
- data/lib/format_restricter_rails/version.rb +3 -0
- data/lib/format_restricter_rails.rb +13 -0
- data/respond_to_by_controller.gemspec +28 -0
- metadata +131 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b4d7d23d624a097eadc3686966910b7289a5176c
|
4
|
+
data.tar.gz: ede176de4c1a46d0d18698a09059f9fe52d05507
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 40d3b502fb25e361bd161d1a2428f6cb889d1d61d41478760206f607ecb07b932162db92b563e0c22474d694c4f5a1498fded65419077bcaa86e5902eb95a186
|
7
|
+
data.tar.gz: 663264cc3a72ebb7e3e64597aacf6d515c7dd253688c52b630ea5ef85a56e6236d2e107b30fe7cac0545d06eaf5097e4c668eebbff7f1bfcbe449976fcdbab91
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Roberts
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# format_restricter_rails
|
2
|
+
|
3
|
+
This gem provides a simple way to block your Rails controller actions from trying to process unsupported formats. Without this, you'll eventually start seeing the following errors on your production site:
|
4
|
+
|
5
|
+
````
|
6
|
+
ActionView::MissingTemplate (Missing template tasks/index, application/index with {:locale=>[:en], :formats=>[:json], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}
|
7
|
+
````
|
8
|
+
|
9
|
+
Why are you getting these errors? Because you designed your sweet little controller action to only process html requests and someone decided to send it a .json request. Rails happily obliged, processed the action and failed when it couldn't find a corresponding json template to render.
|
10
|
+
|
11
|
+
When this happens, you may think to start defining `respond_to` calls in your actions. Once you realize that's going to explode your code base, you'll probably try to call `respond_to :html` at the top of your controller class in hopes that will help. Sadly, it won't; and that's where this gem comes in. It provides a single controller class method, called `restrict_formats_to` that you can add to your controller to declare the formats that it is allowed to process. When unallowed formats are requested, halts execution and returns an **HTTP Error 406 Not acceptable**.
|
12
|
+
|
13
|
+
## Versioning Scheme
|
14
|
+
|
15
|
+
This gem uses [semver](http:/semver.org).
|
16
|
+
|
17
|
+
## Supported Rails Versions
|
18
|
+
|
19
|
+
Currently tested with Rails 4+.
|
20
|
+
Not tested in Rails 5 yet.
|
21
|
+
|
22
|
+
## Installation
|
23
|
+
|
24
|
+
Add this line to your Rails application's Gemfile:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
gem 'format_restricter_rails'
|
28
|
+
```
|
29
|
+
|
30
|
+
And then execute:
|
31
|
+
|
32
|
+
$ bundle
|
33
|
+
|
34
|
+
## Usage
|
35
|
+
|
36
|
+
The `restrict_formats_to` is just a wrapper around a `before_action` helper and you can use the standard `only:` and `except:` options.
|
37
|
+
|
38
|
+
#### Example 1: Restrict all actions
|
39
|
+
|
40
|
+
````ruby
|
41
|
+
class MySnazzyController < ApplicationController
|
42
|
+
restrict_formats_to :html, :json
|
43
|
+
|
44
|
+
def my_action_1
|
45
|
+
end
|
46
|
+
|
47
|
+
def my_action_2
|
48
|
+
end
|
49
|
+
end
|
50
|
+
````
|
51
|
+
|
52
|
+
#### Example 2: Mix and match
|
53
|
+
|
54
|
+
````ruby
|
55
|
+
class MySnazzyController < ApplicationController
|
56
|
+
restrict_formats_to :html, only: :my_action_1
|
57
|
+
restrict_formats_to :json, only: :my_action_2
|
58
|
+
|
59
|
+
def my_action_1
|
60
|
+
end
|
61
|
+
|
62
|
+
def my_action_2
|
63
|
+
end
|
64
|
+
end
|
65
|
+
````
|
66
|
+
|
67
|
+
## Contributing
|
68
|
+
|
69
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/corlewsolutions/format_restricter_rails.
|
70
|
+
|
71
|
+
## License
|
72
|
+
|
73
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
74
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "format_restricter_rails"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
require "pry"
|
10
|
+
Pry.start
|
11
|
+
|
12
|
+
# require "irb"
|
13
|
+
# IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
module FormatRestricterRails
|
2
|
+
module Includes
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.extend(ClassMethods)
|
6
|
+
base.instance_eval do
|
7
|
+
private_class_method :normalize_option
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def restrict_formats_to(*args)
|
13
|
+
options = args.extract_options!
|
14
|
+
except_list = normalize_option(options, :except)
|
15
|
+
only_list = normalize_option(options, :only)
|
16
|
+
allowed_formats = args
|
17
|
+
before_action(only: only_list, except: except_list) do |controller|
|
18
|
+
unless allowed_formats.include?(request.format.symbol)
|
19
|
+
render nothing: true, status: 406
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def normalize_option(options, key)
|
25
|
+
value = options.fetch(key, [])
|
26
|
+
value = [value] unless value.is_a?(Array)
|
27
|
+
value.collect {|e| e.to_sym}
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module FormatRestricterRails
|
2
|
+
|
3
|
+
def self.include_modules_into_host_app
|
4
|
+
ApplicationController.include FormatRestricterRails::Includes
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.was_eager_loaded?
|
8
|
+
@was_eager_loaded ||= false
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.was_eager_loaded=value
|
12
|
+
@was_eager_loaded = value
|
13
|
+
end
|
14
|
+
|
15
|
+
class Railtie < Rails::Railtie
|
16
|
+
# This code will be run before Rails eager loads all of the application code. This method will typically only fire
|
17
|
+
# in production-like environments, however, it could happen in development if the necessary config options are set for the
|
18
|
+
# development.rb.
|
19
|
+
config.before_eager_load do
|
20
|
+
FormatRestricterRails.include_modules_into_host_app
|
21
|
+
FormatRestricterRails.was_eager_loaded = true
|
22
|
+
end
|
23
|
+
|
24
|
+
config.after_initialize do
|
25
|
+
# Don't rerun some setup code if eager loading occured. It can result in weird things happening.
|
26
|
+
unless FormatRestricterRails.was_eager_loaded?
|
27
|
+
FormatRestricterRails.include_modules_into_host_app
|
28
|
+
end
|
29
|
+
|
30
|
+
# Make sure the code is reloaded in development
|
31
|
+
if Rails.env.development?
|
32
|
+
ActionDispatch::Callbacks.before do
|
33
|
+
FormatRestricterRails.include_modules_into_host_app
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require "rails"
|
2
|
+
require "format_restricter_rails/version"
|
3
|
+
require "format_restricter_rails/engine"
|
4
|
+
require "format_restricter_rails/includes"
|
5
|
+
require "format_restricter_rails/railtie"
|
6
|
+
|
7
|
+
# Setup pry for development when running "rake console". Guard against load
|
8
|
+
# errors in production (since pry is only loaded as a DEVELOPMENT dependency
|
9
|
+
# in the .gemspec)
|
10
|
+
begin
|
11
|
+
require "pry"
|
12
|
+
rescue LoadError
|
13
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'format_restricter_rails/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "format_restricter_rails"
|
8
|
+
spec.version = FormatRestricterRails::VERSION
|
9
|
+
spec.authors = ["Roberts"]
|
10
|
+
spec.email = ["roberts@corlewsolutions.com"]
|
11
|
+
|
12
|
+
spec.summary = "Restrict the formats that your Rails controller actions are allowed to process"
|
13
|
+
spec.description = "Restrict the formats that your Rails controller actions are allowed to process"
|
14
|
+
spec.homepage = "https://github.com/corlewsolutions/format_restricter_rails"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.4"
|
25
|
+
spec.add_development_dependency "pry", "~> 0.10.1"
|
26
|
+
|
27
|
+
spec.add_dependency "rails", ">= 4"
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: format_restricter_rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Roberts
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-05-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.4'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.4'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.10.1
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.10.1
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rails
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '4'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '4'
|
83
|
+
description: Restrict the formats that your Rails controller actions are allowed to
|
84
|
+
process
|
85
|
+
email:
|
86
|
+
- roberts@corlewsolutions.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".rspec"
|
93
|
+
- ".travis.yml"
|
94
|
+
- CHANGELOG.md
|
95
|
+
- Gemfile
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- bin/console
|
100
|
+
- bin/setup
|
101
|
+
- lib/format_restricter_rails.rb
|
102
|
+
- lib/format_restricter_rails/engine.rb
|
103
|
+
- lib/format_restricter_rails/includes.rb
|
104
|
+
- lib/format_restricter_rails/railtie.rb
|
105
|
+
- lib/format_restricter_rails/version.rb
|
106
|
+
- respond_to_by_controller.gemspec
|
107
|
+
homepage: https://github.com/corlewsolutions/format_restricter_rails
|
108
|
+
licenses:
|
109
|
+
- MIT
|
110
|
+
metadata: {}
|
111
|
+
post_install_message:
|
112
|
+
rdoc_options: []
|
113
|
+
require_paths:
|
114
|
+
- lib
|
115
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
requirements: []
|
126
|
+
rubyforge_project:
|
127
|
+
rubygems_version: 2.6.4
|
128
|
+
signing_key:
|
129
|
+
specification_version: 4
|
130
|
+
summary: Restrict the formats that your Rails controller actions are allowed to process
|
131
|
+
test_files: []
|