controller_commands 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.ruby-version +1 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +150 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/controller_commands.gemspec +39 -0
- data/lib/controller_commands.rb +5 -0
- data/lib/controller_commands/command.rb +69 -0
- data/lib/controller_commands/concern.rb +56 -0
- data/lib/controller_commands/version.rb +3 -0
- metadata +128 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9ea58b30f1609f0e4d4c3987c6ba770d58372aa2
|
4
|
+
data.tar.gz: c10c17c3f353192893da6924e791b434aba0e193
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f533e1e86aa704390097e8515720b317d0c1ce014d4fcbce98e4d50223dfb3ad3a1a2a95d1cc3488e0e1ba69ecdfece864447b0735c14786df28cf4ed6817064
|
7
|
+
data.tar.gz: 21d38a657f02518fb84696c6557180bf305a07f553a62095edb94b44329f99dc44fb2541f42cc1fc1abac68c201a5c06616cd0040cf01949e21551994390cca0
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.4.2
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
controller_commands (0.1.0)
|
5
|
+
dry-validation (~> 0.11)
|
6
|
+
hash_key_transformer (~> 0.1)
|
7
|
+
rails (>= 4.2)
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
actioncable (5.1.6)
|
13
|
+
actionpack (= 5.1.6)
|
14
|
+
nio4r (~> 2.0)
|
15
|
+
websocket-driver (~> 0.6.1)
|
16
|
+
actionmailer (5.1.6)
|
17
|
+
actionpack (= 5.1.6)
|
18
|
+
actionview (= 5.1.6)
|
19
|
+
activejob (= 5.1.6)
|
20
|
+
mail (~> 2.5, >= 2.5.4)
|
21
|
+
rails-dom-testing (~> 2.0)
|
22
|
+
actionpack (5.1.6)
|
23
|
+
actionview (= 5.1.6)
|
24
|
+
activesupport (= 5.1.6)
|
25
|
+
rack (~> 2.0)
|
26
|
+
rack-test (>= 0.6.3)
|
27
|
+
rails-dom-testing (~> 2.0)
|
28
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
29
|
+
actionview (5.1.6)
|
30
|
+
activesupport (= 5.1.6)
|
31
|
+
builder (~> 3.1)
|
32
|
+
erubi (~> 1.4)
|
33
|
+
rails-dom-testing (~> 2.0)
|
34
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
35
|
+
activejob (5.1.6)
|
36
|
+
activesupport (= 5.1.6)
|
37
|
+
globalid (>= 0.3.6)
|
38
|
+
activemodel (5.1.6)
|
39
|
+
activesupport (= 5.1.6)
|
40
|
+
activerecord (5.1.6)
|
41
|
+
activemodel (= 5.1.6)
|
42
|
+
activesupport (= 5.1.6)
|
43
|
+
arel (~> 8.0)
|
44
|
+
activesupport (5.1.6)
|
45
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
46
|
+
i18n (>= 0.7, < 2)
|
47
|
+
minitest (~> 5.1)
|
48
|
+
tzinfo (~> 1.1)
|
49
|
+
arel (8.0.0)
|
50
|
+
builder (3.2.3)
|
51
|
+
concurrent-ruby (1.0.5)
|
52
|
+
crass (1.0.3)
|
53
|
+
dry-configurable (0.7.0)
|
54
|
+
concurrent-ruby (~> 1.0)
|
55
|
+
dry-container (0.6.0)
|
56
|
+
concurrent-ruby (~> 1.0)
|
57
|
+
dry-configurable (~> 0.1, >= 0.1.3)
|
58
|
+
dry-core (0.4.5)
|
59
|
+
concurrent-ruby (~> 1.0)
|
60
|
+
dry-equalizer (0.2.0)
|
61
|
+
dry-logic (0.4.2)
|
62
|
+
dry-container (~> 0.2, >= 0.2.6)
|
63
|
+
dry-core (~> 0.2)
|
64
|
+
dry-equalizer (~> 0.2)
|
65
|
+
dry-types (0.12.2)
|
66
|
+
concurrent-ruby (~> 1.0)
|
67
|
+
dry-configurable (~> 0.1)
|
68
|
+
dry-container (~> 0.3)
|
69
|
+
dry-core (~> 0.2, >= 0.2.1)
|
70
|
+
dry-equalizer (~> 0.2)
|
71
|
+
dry-logic (~> 0.4, >= 0.4.2)
|
72
|
+
inflecto (~> 0.0.0, >= 0.0.2)
|
73
|
+
dry-validation (0.11.1)
|
74
|
+
concurrent-ruby (~> 1.0)
|
75
|
+
dry-configurable (~> 0.1, >= 0.1.3)
|
76
|
+
dry-core (~> 0.2, >= 0.2.1)
|
77
|
+
dry-equalizer (~> 0.2)
|
78
|
+
dry-logic (~> 0.4, >= 0.4.0)
|
79
|
+
dry-types (~> 0.12.0)
|
80
|
+
erubi (1.7.1)
|
81
|
+
globalid (0.4.1)
|
82
|
+
activesupport (>= 4.2.0)
|
83
|
+
hash_key_transformer (0.1.2)
|
84
|
+
i18n (1.0.0)
|
85
|
+
concurrent-ruby (~> 1.0)
|
86
|
+
inflecto (0.0.2)
|
87
|
+
loofah (2.2.2)
|
88
|
+
crass (~> 1.0.2)
|
89
|
+
nokogiri (>= 1.5.9)
|
90
|
+
mail (2.7.0)
|
91
|
+
mini_mime (>= 0.1.1)
|
92
|
+
method_source (0.9.0)
|
93
|
+
mini_mime (1.0.0)
|
94
|
+
mini_portile2 (2.3.0)
|
95
|
+
minitest (5.11.3)
|
96
|
+
nio4r (2.3.0)
|
97
|
+
nokogiri (1.8.2)
|
98
|
+
mini_portile2 (~> 2.3.0)
|
99
|
+
rack (2.0.4)
|
100
|
+
rack-test (1.0.0)
|
101
|
+
rack (>= 1.0, < 3)
|
102
|
+
rails (5.1.6)
|
103
|
+
actioncable (= 5.1.6)
|
104
|
+
actionmailer (= 5.1.6)
|
105
|
+
actionpack (= 5.1.6)
|
106
|
+
actionview (= 5.1.6)
|
107
|
+
activejob (= 5.1.6)
|
108
|
+
activemodel (= 5.1.6)
|
109
|
+
activerecord (= 5.1.6)
|
110
|
+
activesupport (= 5.1.6)
|
111
|
+
bundler (>= 1.3.0)
|
112
|
+
railties (= 5.1.6)
|
113
|
+
sprockets-rails (>= 2.0.0)
|
114
|
+
rails-dom-testing (2.0.3)
|
115
|
+
activesupport (>= 4.2.0)
|
116
|
+
nokogiri (>= 1.6)
|
117
|
+
rails-html-sanitizer (1.0.4)
|
118
|
+
loofah (~> 2.2, >= 2.2.2)
|
119
|
+
railties (5.1.6)
|
120
|
+
actionpack (= 5.1.6)
|
121
|
+
activesupport (= 5.1.6)
|
122
|
+
method_source
|
123
|
+
rake (>= 0.8.7)
|
124
|
+
thor (>= 0.18.1, < 2.0)
|
125
|
+
rake (12.3.1)
|
126
|
+
sprockets (3.7.1)
|
127
|
+
concurrent-ruby (~> 1.0)
|
128
|
+
rack (> 1, < 3)
|
129
|
+
sprockets-rails (3.2.1)
|
130
|
+
actionpack (>= 4.0)
|
131
|
+
activesupport (>= 4.0)
|
132
|
+
sprockets (>= 3.0.0)
|
133
|
+
thor (0.20.0)
|
134
|
+
thread_safe (0.3.6)
|
135
|
+
tzinfo (1.2.5)
|
136
|
+
thread_safe (~> 0.1)
|
137
|
+
websocket-driver (0.6.5)
|
138
|
+
websocket-extensions (>= 0.1.0)
|
139
|
+
websocket-extensions (0.1.3)
|
140
|
+
|
141
|
+
PLATFORMS
|
142
|
+
ruby
|
143
|
+
|
144
|
+
DEPENDENCIES
|
145
|
+
bundler (~> 1.16)
|
146
|
+
controller_commands!
|
147
|
+
rake (~> 12.3)
|
148
|
+
|
149
|
+
BUNDLED WITH
|
150
|
+
1.16.1
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "controller_commands"
|
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
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "controller_commands/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "controller_commands"
|
8
|
+
spec.version = ControllerCommands::VERSION
|
9
|
+
spec.authors = ["Kevin Rood"]
|
10
|
+
spec.email = ["kevin.rood@accelecode.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A Rails controller concern which makes it easy to encapsulate validation and processing of complex incoming data into command classes.}
|
13
|
+
spec.description = %q{A Rails controller concern which makes it easy to encapsulate validation and processing of complex incoming data into command classes.}
|
14
|
+
spec.homepage = "https://github.com/accelecode/controller_commands"
|
15
|
+
spec.license = "Apache-2.0"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
"public gem pushes."
|
24
|
+
end
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
f.match(%r{^(test|spec|features)/})
|
28
|
+
end
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
34
|
+
spec.add_development_dependency 'rake', '~> 12.3'
|
35
|
+
|
36
|
+
spec.add_runtime_dependency 'dry-validation', '~> 0.11'
|
37
|
+
spec.add_runtime_dependency 'hash_key_transformer', '~> 0.1'
|
38
|
+
spec.add_runtime_dependency 'rails', '>= 4.2'
|
39
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module ControllerCommands
|
2
|
+
module Command
|
3
|
+
|
4
|
+
module ClassMethods
|
5
|
+
def validation_schema(&block)
|
6
|
+
@validation_schema_provider = block
|
7
|
+
end
|
8
|
+
|
9
|
+
def validate(context, incoming_params)
|
10
|
+
validation_schema =
|
11
|
+
@validation_schema_provider ?
|
12
|
+
@validation_schema_provider.call(context) :
|
13
|
+
Dry::Validation.Schema # provide a default, empty validation schema if none was defined for the command
|
14
|
+
validation_schema.call(incoming_params)
|
15
|
+
end
|
16
|
+
|
17
|
+
def success_message(&block)
|
18
|
+
@success_message_block = block
|
19
|
+
end
|
20
|
+
|
21
|
+
def get_success_message
|
22
|
+
@success_message_block&.call
|
23
|
+
end
|
24
|
+
|
25
|
+
def handle_command(&block)
|
26
|
+
@perform_block = block
|
27
|
+
end
|
28
|
+
|
29
|
+
def perform(context, validated_params)
|
30
|
+
@perform_block.call(context, validated_params)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.included(base)
|
35
|
+
base.extend(ClassMethods)
|
36
|
+
end
|
37
|
+
|
38
|
+
def initialize(incoming_params, context)
|
39
|
+
@incoming_params = incoming_params
|
40
|
+
@context = context
|
41
|
+
end
|
42
|
+
|
43
|
+
def validated?
|
44
|
+
!!@result
|
45
|
+
end
|
46
|
+
|
47
|
+
def errors
|
48
|
+
@result.messages
|
49
|
+
end
|
50
|
+
|
51
|
+
def validated_params
|
52
|
+
@result.output
|
53
|
+
end
|
54
|
+
|
55
|
+
def validate_params
|
56
|
+
@result = self.class.validate(@context, @incoming_params)
|
57
|
+
@result.messages.count == 0
|
58
|
+
end
|
59
|
+
|
60
|
+
def success_message
|
61
|
+
self.class.get_success_message
|
62
|
+
end
|
63
|
+
|
64
|
+
def perform
|
65
|
+
self.class.perform(@context, validated_params)
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
require 'action_controller/metal/exceptions'
|
3
|
+
require 'dry/validation'
|
4
|
+
require 'hash_key_transformer'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
module ControllerCommands
|
8
|
+
module Concern
|
9
|
+
extend ActiveSupport::Concern
|
10
|
+
|
11
|
+
def handle_command(options = {})
|
12
|
+
command_params = parse_params(options.fetch(:incoming_key_transformer, :transform_camel_to_underscore))
|
13
|
+
context = options.fetch(:context, {})
|
14
|
+
command = construct_command(command_params, context, options[:command_klass])
|
15
|
+
|
16
|
+
# Validate & potentially execute the command
|
17
|
+
is_command_valid = command.validate_params
|
18
|
+
result =
|
19
|
+
if is_command_valid
|
20
|
+
flash[:notice] = command.success_message
|
21
|
+
{data: command.perform}
|
22
|
+
else
|
23
|
+
{errors: command.errors}
|
24
|
+
end
|
25
|
+
|
26
|
+
# Render the results
|
27
|
+
validation_failed_status_code = options.fetch(:validation_failed_status_code, :ok)
|
28
|
+
status_code = (is_command_valid ? :ok : validation_failed_status_code)
|
29
|
+
render status: status_code, json: HashKeyTransformer.transform_underscore_to_camel(result)
|
30
|
+
end
|
31
|
+
|
32
|
+
def parse_params(key_transformer_strategy)
|
33
|
+
# We need to be cautious accepting array fields as input. Refer to CVE-2013-0155 for more details about the danger
|
34
|
+
# of malicious users crafting JSON using an array for a where clause field:
|
35
|
+
#
|
36
|
+
# https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2013-0155
|
37
|
+
#
|
38
|
+
# The default JSON parsing in Rails replaces empty arrays with nil as a part of their solution to the CVE above.
|
39
|
+
# This is the reason we are manually parsing the JSON request body. Consistent use of dry-validation schema types
|
40
|
+
# should protect us from this CVE and also provide the same protection as Rails strong parameters.
|
41
|
+
parsed_params = JSON.parse(request.body.read)
|
42
|
+
HashKeyTransformer.send(key_transformer_strategy, parsed_params)
|
43
|
+
end
|
44
|
+
|
45
|
+
def construct_command(incoming_params, context, command_klass = nil)
|
46
|
+
unless command_klass
|
47
|
+
# this should be fine given that the permissible action names are controlled by routing definitions
|
48
|
+
command_klass_name = params.fetch(:action).camelize
|
49
|
+
command_klass =
|
50
|
+
"#{self.class.name}::#{command_klass_name}".safe_constantize or
|
51
|
+
raise ActionController::RoutingError.new('Invalid Command')
|
52
|
+
end
|
53
|
+
command_klass.new(incoming_params, context)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: controller_commands
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kevin Rood
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-04-06 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.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '12.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '12.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: dry-validation
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.11'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.11'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: hash_key_transformer
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.1'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.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.2'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '4.2'
|
83
|
+
description: A Rails controller concern which makes it easy to encapsulate validation
|
84
|
+
and processing of complex incoming data into command classes.
|
85
|
+
email:
|
86
|
+
- kevin.rood@accelecode.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".ruby-version"
|
92
|
+
- Gemfile
|
93
|
+
- Gemfile.lock
|
94
|
+
- Rakefile
|
95
|
+
- bin/console
|
96
|
+
- bin/setup
|
97
|
+
- controller_commands.gemspec
|
98
|
+
- lib/controller_commands.rb
|
99
|
+
- lib/controller_commands/command.rb
|
100
|
+
- lib/controller_commands/concern.rb
|
101
|
+
- lib/controller_commands/version.rb
|
102
|
+
homepage: https://github.com/accelecode/controller_commands
|
103
|
+
licenses:
|
104
|
+
- Apache-2.0
|
105
|
+
metadata:
|
106
|
+
allowed_push_host: https://rubygems.org
|
107
|
+
post_install_message:
|
108
|
+
rdoc_options: []
|
109
|
+
require_paths:
|
110
|
+
- lib
|
111
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 2.6.13
|
124
|
+
signing_key:
|
125
|
+
specification_version: 4
|
126
|
+
summary: A Rails controller concern which makes it easy to encapsulate validation
|
127
|
+
and processing of complex incoming data into command classes.
|
128
|
+
test_files: []
|