voom-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/.circleci/config.yml +60 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.ruby-version +1 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +35 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/voom/commands/aggregate_validations.rb +32 -0
- data/lib/voom/commands/base.rb +42 -0
- data/lib/voom/commands/codes.rb +8 -0
- data/lib/voom/commands/controller_response.rb +53 -0
- data/lib/voom/commands/extract_errors.rb +22 -0
- data/lib/voom/commands/messages.rb +21 -0
- data/lib/voom/commands/namespace.rb +25 -0
- data/lib/voom/commands/rescue_logical_errors.rb +34 -0
- data/lib/voom/commands/response.rb +60 -0
- data/lib/voom/commands/success_and_fail.rb +25 -0
- data/lib/voom/commands/validate_params.rb +32 -0
- data/lib/voom/commands/version.rb +5 -0
- data/lib/voom/commands.rb +8 -0
- data/lib/voom/errors/logical_error.rb +6 -0
- data/lib/voom/errors/parameter_validation.rb +15 -0
- data/voom-commands.gemspec +28 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d515b626343bc5ba84a9fffdbeeb1bd129ba85ceeaa11ef35a4b2a1238302964
|
4
|
+
data.tar.gz: c37e2f6cf270e0dcce0b7a635a56b02bc37aba778fbdba33e74beb8d6986d400
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8c31eecefc16b5c52dc3b8346eeac9f0fa588acdcdb67213cb4a3f2a37cd2ae4d494500ee77110af320de7241c4287962108ec21a967c292ba3a415a63f0ec8f
|
7
|
+
data.tar.gz: a6cc40aa440ceb5708cef4a70353d355d4d192c53d0079ba98b6420f9ab87558a56e14866fb57dc0e3cbc9055a41bd5a22b111324cc479f1363f3e22b98740f8
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# Ruby CircleCI 2.0 configuration file
|
2
|
+
#
|
3
|
+
# Check https://circleci.com/docs/2.0/language-ruby/ for more details
|
4
|
+
#
|
5
|
+
version: 2
|
6
|
+
jobs:
|
7
|
+
build:
|
8
|
+
docker:
|
9
|
+
# specify the version you desire here
|
10
|
+
- image: circleci/ruby:2.5-node-browsers
|
11
|
+
|
12
|
+
# Specify service dependencies here if necessary
|
13
|
+
# CircleCI maintains a library of pre-built images
|
14
|
+
# documented at https://circleci.com/docs/2.0/circleci-images/
|
15
|
+
# - image: circleci/postgres:9.4
|
16
|
+
|
17
|
+
working_directory: ~/repo
|
18
|
+
|
19
|
+
steps:
|
20
|
+
- checkout
|
21
|
+
|
22
|
+
# Download and cache dependencies
|
23
|
+
- restore_cache:
|
24
|
+
keys:
|
25
|
+
- v1-dependencies-{{ checksum "Gemfile.lock" }}
|
26
|
+
# fallback to using the latest cache if no exact match is found
|
27
|
+
- v1-dependencies-
|
28
|
+
|
29
|
+
- run:
|
30
|
+
name: install dependencies
|
31
|
+
command: |
|
32
|
+
bundle install --jobs=4 --retry=3 --path vendor/bundle
|
33
|
+
|
34
|
+
- save_cache:
|
35
|
+
paths:
|
36
|
+
- ./vendor/bundle
|
37
|
+
key: v1-dependencies-{{ checksum "Gemfile.lock" }}
|
38
|
+
|
39
|
+
# run tests!
|
40
|
+
- run:
|
41
|
+
name: run specs
|
42
|
+
command: |
|
43
|
+
mkdir /tmp/test-results
|
44
|
+
TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)"
|
45
|
+
|
46
|
+
bundle exec rspec --format progress \
|
47
|
+
--format RspecJunitFormatter \
|
48
|
+
--out /tmp/test-results/rspec.xml \
|
49
|
+
--format progress \
|
50
|
+
$TEST_FILES
|
51
|
+
|
52
|
+
# collect reports
|
53
|
+
- store_test_results:
|
54
|
+
path: /tmp/test-results
|
55
|
+
- store_artifacts:
|
56
|
+
path: /tmp/test-results
|
57
|
+
destination: test-results
|
58
|
+
- store_artifacts:
|
59
|
+
path: ./coverage
|
60
|
+
destination: code-coverage
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.5.3
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at rx@voomify.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
voom-commands (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.3)
|
10
|
+
rake (10.5.0)
|
11
|
+
rspec (3.8.0)
|
12
|
+
rspec-core (~> 3.8.0)
|
13
|
+
rspec-expectations (~> 3.8.0)
|
14
|
+
rspec-mocks (~> 3.8.0)
|
15
|
+
rspec-core (3.8.0)
|
16
|
+
rspec-support (~> 3.8.0)
|
17
|
+
rspec-expectations (3.8.2)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.8.0)
|
20
|
+
rspec-mocks (3.8.0)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.8.0)
|
23
|
+
rspec-support (3.8.0)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
bundler (~> 1.17)
|
30
|
+
rake (~> 10.0)
|
31
|
+
rspec (~> 3.0)
|
32
|
+
voom-commands!
|
33
|
+
|
34
|
+
BUNDLED WITH
|
35
|
+
1.17.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Russell Edens
|
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,41 @@
|
|
1
|
+
# Voom::Commands
|
2
|
+
|
3
|
+
A lightweight easily composable functional object command layer to sit between contollers and models.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'voom-commands'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install voom-commands
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
25
|
+
## Development
|
26
|
+
|
27
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
28
|
+
|
29
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/rx/commands. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
34
|
+
|
35
|
+
## License
|
36
|
+
|
37
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
38
|
+
|
39
|
+
## Code of Conduct
|
40
|
+
|
41
|
+
Everyone interacting in the Voom::Commands project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/rx/commands/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "voom/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,32 @@
|
|
1
|
+
require_relative 'extract_errors'
|
2
|
+
|
3
|
+
module Voom
|
4
|
+
module Commands
|
5
|
+
module AggregateValidations
|
6
|
+
include Commands::ExtractErrors
|
7
|
+
# This method will combine all parameter validation errors into a single error hash
|
8
|
+
# Warning: If the same key exists in two validations the last one wins
|
9
|
+
# You pass it a lambda that validates parameters.
|
10
|
+
# EXAMPLE:
|
11
|
+
# errors = validate_workflows(-> {@params = validate_params(params_)},
|
12
|
+
# -> {@update_location_wf = update_location_wf.new(params_)})
|
13
|
+
#
|
14
|
+
def aggregate_validations(*lambdas)
|
15
|
+
lambdas.reduce({}) do |errors, lambda|
|
16
|
+
begin
|
17
|
+
lambda.call
|
18
|
+
rescue Errors::ParameterValidation => e
|
19
|
+
errors.merge!(extract_errors(e))
|
20
|
+
end
|
21
|
+
errors
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def aggregate_validations!(*lambdas)
|
26
|
+
errors = aggregate_validations(*lambdas)
|
27
|
+
trace {errors.inspect} if errors.any?
|
28
|
+
raise Errors::ParameterValidation.new('Form validation failed.', errors) if errors.any?
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require_relative 'success_and_fail'
|
2
|
+
require_relative 'validate_params'
|
3
|
+
require_relative 'rescue_logical_errors'
|
4
|
+
require_relative 'aggregate_validations'
|
5
|
+
require_relative 'namespace'
|
6
|
+
|
7
|
+
module Voom
|
8
|
+
module Commands
|
9
|
+
class Base
|
10
|
+
include Commands::SuccessAndFail
|
11
|
+
include Commands::ValidateParams
|
12
|
+
include Commands::AggregateValidations
|
13
|
+
include Commands::Namespace
|
14
|
+
class << self
|
15
|
+
include Commands::RescueLogicalErrors
|
16
|
+
end
|
17
|
+
|
18
|
+
attr_reader :params
|
19
|
+
|
20
|
+
def initialize(*args, **params_, &block)
|
21
|
+
@params = params_.any? ? validate_params(**params_) : {}
|
22
|
+
@block = block
|
23
|
+
end
|
24
|
+
|
25
|
+
def call
|
26
|
+
response = self.perform
|
27
|
+
response = response.respond_to?(:success?) ? response : success
|
28
|
+
response2 = @block.call(response, self) if @block
|
29
|
+
response = response2.respond_to?(:success?) ? response2 : response
|
30
|
+
response.respond_to?(:success?) ? response : success
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.call(*args, **params, &block)
|
34
|
+
rescue_logical_errors {new(*args, **params, &block).call(&block)}
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.call!(*args, **params, &block)
|
38
|
+
self.new(*args, **params, &block).call(&block)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Voom
|
2
|
+
module Commands
|
3
|
+
module ControllerResponse
|
4
|
+
def handle_response(response, &block)
|
5
|
+
if response.success?
|
6
|
+
redirect_url = redirect_url(response, &block)
|
7
|
+
if redirect_url
|
8
|
+
redirect_to redirect_url, status: :see_other
|
9
|
+
else
|
10
|
+
render json: response.data, status: 200
|
11
|
+
end
|
12
|
+
else
|
13
|
+
render json: response.messages, status: response.status
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# If you pass a block then return a hash for any parameters you want to add to the redirect
|
18
|
+
# If you want to forward some parameters from the response, pass the parameter names in pluck
|
19
|
+
def redirect_url(response = nil, pluck: [], &block)
|
20
|
+
redirect_url = params.fetch(:redirect) {nil}
|
21
|
+
return unless redirect_url
|
22
|
+
|
23
|
+
data = response ? response.data : {}
|
24
|
+
data = block.call(data) if block
|
25
|
+
|
26
|
+
data = data.to_hash if data.respond_to?(:to_hash)
|
27
|
+
data = data.attributes if data.respond_to?(:attributes)
|
28
|
+
data = data.deep_symbolize_keys if data.respond_to?(:deep_symbolize_keys)
|
29
|
+
trace {"Data: #{data.inspect}"}
|
30
|
+
|
31
|
+
pluck = Array(params.fetch(:pluck) {pluck}).map(&:to_sym)
|
32
|
+
trace {"Plucking: #{pluck.inspect}"}
|
33
|
+
data = pluck.map do |key|
|
34
|
+
[key, data.fetch(key) {params[key]}]
|
35
|
+
end.to_h
|
36
|
+
|
37
|
+
data[:snackbar] = response.messages.snackbar if response&.messages&.snackbar
|
38
|
+
|
39
|
+
query = data.to_query
|
40
|
+
trace {"Query: #{query.inspect}"}
|
41
|
+
|
42
|
+
redirect_url = "#{redirect_url}#{url_seperator(redirect_url)}#{query}" unless query.empty?
|
43
|
+
redirect_url
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def url_seperator(redirect_url)
|
49
|
+
redirect_url.include?('?') ? '&' : '?'
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Voom
|
2
|
+
module Commands
|
3
|
+
module ExtractErrors
|
4
|
+
def extract_fk_errors(e)
|
5
|
+
errmsg = e.message
|
6
|
+
table, _foreign_key, foreign_table = e.message.match(/PG::ForeignKeyViolation: ERROR:.*\"(.*)\".*\"(.*)\"[\n].*\"(.*)\"/)&.captures
|
7
|
+
if table && _foreign_key && foreign_table
|
8
|
+
errmsg = "Unable to delete or update #{table.singularize.humanize.titleize}. "\
|
9
|
+
"It is referred to from one or more #{foreign_table.humanize.titleize}. "\
|
10
|
+
"You will need to update or remove any #{foreign_table.humanize.titleize} that refer to this "\
|
11
|
+
"#{table.singularize.humanize.titleize} first."
|
12
|
+
end
|
13
|
+
{exception: errmsg}
|
14
|
+
end
|
15
|
+
|
16
|
+
def extract_errors(e)
|
17
|
+
return e.form_errors if e.respond_to?(:form_errors) && e.form_errors.any?
|
18
|
+
{exception: e.message}
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Voom
|
2
|
+
module Commands
|
3
|
+
class Messages
|
4
|
+
attr_reader :warnings, :errors, :snackbar
|
5
|
+
|
6
|
+
def initialize(errors: {}, warnings: {}, snackbar: [])
|
7
|
+
@errors = errors
|
8
|
+
@warnings = warnings
|
9
|
+
@snackbar = snackbar
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_h
|
13
|
+
{
|
14
|
+
errors: errors,
|
15
|
+
warnings: warnings,
|
16
|
+
snackbar: snackbar
|
17
|
+
}
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Voom
|
2
|
+
module Commands
|
3
|
+
module Namespace
|
4
|
+
include ExtractErrors
|
5
|
+
# Will put the commands errors into a nested namespace in the error hash
|
6
|
+
# This allows you to use a top-level command and then nest its errors into another model
|
7
|
+
# This example the cart has shipping and billing addresses, so it does not make sense to bubble the address fields and errors up to the top level.
|
8
|
+
def namespace_errors(lambda, namespace)
|
9
|
+
begin
|
10
|
+
lambda.call
|
11
|
+
rescue Errors::ParameterValidation => e
|
12
|
+
{namespace => extract_errors(e)}
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def namespace_errors!(lambda, namespace)
|
17
|
+
response = namespace_errors(lambda, namespace)
|
18
|
+
raise Errors::ParameterValidation.new('Form validation failed.',
|
19
|
+
response) if response.respond_to?(:include?) &&
|
20
|
+
response.include?(namespace)
|
21
|
+
response
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require_relative 'success_and_fail'
|
2
|
+
require_relative 'extract_errors'
|
3
|
+
require 'voom/errors/logical_error'
|
4
|
+
|
5
|
+
module Voom
|
6
|
+
module Commands
|
7
|
+
module RescueLogicalErrors
|
8
|
+
include Commands::SuccessAndFail
|
9
|
+
include Commands::ExtractErrors
|
10
|
+
|
11
|
+
# A logical error is one that we can notify the user about and they can use that information to fix their request
|
12
|
+
# ParameterValidation is a logical error
|
13
|
+
# UnableToFind or AR::RecordNotFound is a logical error
|
14
|
+
# Foreign key violations - might be logical so we handle them as such
|
15
|
+
# Anything else is logged, honey badger is notified and a 500 is returned.
|
16
|
+
def rescue_logical_errors(&block)
|
17
|
+
begin
|
18
|
+
block.call
|
19
|
+
rescue Errors::LogicalError => e
|
20
|
+
fail(errors: extract_errors(e), status: 422)
|
21
|
+
rescue ActiveRecord::RecordNotFound => e
|
22
|
+
fail(errors: extract_errors(e), status: 404)
|
23
|
+
rescue ActiveRecord::InvalidForeignKey => e
|
24
|
+
fail(errors: extract_fk_errors(e), status: 422)
|
25
|
+
rescue StandardError => e
|
26
|
+
Rails.logger.error {e.message}
|
27
|
+
Rails.logger.error {e.backtrace.join("\n")}
|
28
|
+
Honeybadger.notify(e) if defined?(Honeybadger)
|
29
|
+
fail(errors: e.message, status: 500)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require_relative 'messages'
|
2
|
+
|
3
|
+
module Voom
|
4
|
+
module Commands
|
5
|
+
class Response
|
6
|
+
attr_reader :data, :status, :messages
|
7
|
+
SUCCESS = 0
|
8
|
+
FAILURE = 1
|
9
|
+
|
10
|
+
def initialize(data: [],
|
11
|
+
status: SUCCESS,
|
12
|
+
messages: {})
|
13
|
+
@data = data
|
14
|
+
@status = status
|
15
|
+
@messages = Messages.new(messages)
|
16
|
+
end
|
17
|
+
|
18
|
+
# If your data either is a hash or is an array containing one hash
|
19
|
+
# This helper allows you to treat the result object as a hash
|
20
|
+
# returns nil if there is more than one element in the data
|
21
|
+
# Or if the data is empty
|
22
|
+
def [](key)
|
23
|
+
return data[key] if data.respond_to?(:key?) #quacks like a hash
|
24
|
+
return data.send(key.to_sym) if data.respond_to?(key.to_sym) #behaves like a model/entity
|
25
|
+
return nil if data.empty? or data.size > 1
|
26
|
+
data.first[key] if data.first.respond_to?(:key?)
|
27
|
+
end
|
28
|
+
|
29
|
+
def <<(output)
|
30
|
+
@data << output
|
31
|
+
end
|
32
|
+
|
33
|
+
def succeeded?
|
34
|
+
@status == SUCCESS
|
35
|
+
end
|
36
|
+
|
37
|
+
alias success? succeeded?
|
38
|
+
|
39
|
+
def failed?
|
40
|
+
!succeeded?
|
41
|
+
end
|
42
|
+
|
43
|
+
alias fail? failed?
|
44
|
+
|
45
|
+
def to_h
|
46
|
+
{data: data,
|
47
|
+
status: status,
|
48
|
+
message: messages.to_h}
|
49
|
+
end
|
50
|
+
|
51
|
+
def errors
|
52
|
+
@messages.errors
|
53
|
+
end
|
54
|
+
|
55
|
+
def warnings
|
56
|
+
@messages.warnings
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require_relative 'codes'
|
2
|
+
require_relative 'response'
|
3
|
+
|
4
|
+
module Voom
|
5
|
+
module Commands
|
6
|
+
module SuccessAndFail
|
7
|
+
def success(data: [], warnings: {}, snackbar: [])
|
8
|
+
SuccessAndFail.success(data: data, warnings: warnings, snackbar: snackbar)
|
9
|
+
end
|
10
|
+
|
11
|
+
def fail(status: Commands::Codes::FAILURE, errors: {}, warnings: {}, data: [])
|
12
|
+
SuccessAndFail.fail(status: status, errors: errors, warnings: warnings, data: data)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.success(data: [], warnings: {}, snackbar: [])
|
16
|
+
Response.new(data: data, status: Commands::Codes::SUCCESS, messages: {errors: {}, warnings: warnings, snackbar: snackbar})
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.fail(status: Commands::Codes::FAILURE, errors: {}, warnings: {}, data: [])
|
20
|
+
Response.new(data: data, status: status, messages: {errors: errors, warnings: warnings})
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'voom/errors/parameter_validation'
|
2
|
+
|
3
|
+
module Voom
|
4
|
+
module Commands
|
5
|
+
module ValidateParams
|
6
|
+
def schema
|
7
|
+
raise 'You must implement schema!'
|
8
|
+
end
|
9
|
+
|
10
|
+
def validate_params(**params_)
|
11
|
+
result = schema.call(params_.symbolize_keys)
|
12
|
+
# trace { result.inspect }
|
13
|
+
raise Errors::ParameterValidation.new("Form validation failed: #{humanize(result.messages(full: true))}",
|
14
|
+
humanize(result.messages(full: true))) if result.failure?
|
15
|
+
result.to_h
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def humanize(messages)
|
21
|
+
messages.map do |key, messages|
|
22
|
+
[
|
23
|
+
key,
|
24
|
+
messages.map do |msg|
|
25
|
+
msg.humanize
|
26
|
+
end
|
27
|
+
]
|
28
|
+
end.to_h
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require_relative 'logical_error'
|
2
|
+
|
3
|
+
module Voom
|
4
|
+
module Errors
|
5
|
+
class ParameterValidation < Errors::LogicalError
|
6
|
+
attr_reader :form_errors
|
7
|
+
|
8
|
+
def initialize(msg, form_errors = {})
|
9
|
+
@form_errors = form_errors
|
10
|
+
super(msg)
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "voom/commands/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "voom-commands"
|
8
|
+
spec.version = Voom::Commands::VERSION
|
9
|
+
spec.authors = ["Russell Edens"]
|
10
|
+
spec.email = ["rx@voomify.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A lightweight easily composable functional object command layer to sit between contollers and models.}
|
13
|
+
spec.homepage = "https://github.com/rx/commands"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
# Specify which files should be added to the gem when it is released.
|
17
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
18
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
19
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
end
|
21
|
+
spec.bindir = "exe"
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: voom-commands
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Russell Edens
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-11-15 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.17'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.17'
|
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.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- rx@voomify.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".circleci/config.yml"
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- ".ruby-version"
|
66
|
+
- ".travis.yml"
|
67
|
+
- CODE_OF_CONDUCT.md
|
68
|
+
- Gemfile
|
69
|
+
- Gemfile.lock
|
70
|
+
- LICENSE.txt
|
71
|
+
- README.md
|
72
|
+
- Rakefile
|
73
|
+
- bin/console
|
74
|
+
- bin/setup
|
75
|
+
- lib/voom/commands.rb
|
76
|
+
- lib/voom/commands/aggregate_validations.rb
|
77
|
+
- lib/voom/commands/base.rb
|
78
|
+
- lib/voom/commands/codes.rb
|
79
|
+
- lib/voom/commands/controller_response.rb
|
80
|
+
- lib/voom/commands/extract_errors.rb
|
81
|
+
- lib/voom/commands/messages.rb
|
82
|
+
- lib/voom/commands/namespace.rb
|
83
|
+
- lib/voom/commands/rescue_logical_errors.rb
|
84
|
+
- lib/voom/commands/response.rb
|
85
|
+
- lib/voom/commands/success_and_fail.rb
|
86
|
+
- lib/voom/commands/validate_params.rb
|
87
|
+
- lib/voom/commands/version.rb
|
88
|
+
- lib/voom/errors/logical_error.rb
|
89
|
+
- lib/voom/errors/parameter_validation.rb
|
90
|
+
- voom-commands.gemspec
|
91
|
+
homepage: https://github.com/rx/commands
|
92
|
+
licenses:
|
93
|
+
- MIT
|
94
|
+
metadata: {}
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options: []
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
requirements: []
|
110
|
+
rubyforge_project:
|
111
|
+
rubygems_version: 2.7.6
|
112
|
+
signing_key:
|
113
|
+
specification_version: 4
|
114
|
+
summary: A lightweight easily composable functional object command layer to sit between
|
115
|
+
contollers and models.
|
116
|
+
test_files: []
|