the_help 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 +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +14 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +51 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +8 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/the_help.rb +7 -0
- data/lib/the_help/errors.rb +7 -0
- data/lib/the_help/provides_callbacks.rb +70 -0
- data/lib/the_help/service.rb +197 -0
- data/lib/the_help/version.rb +5 -0
- data/the_help.gemspec +28 -0
- metadata +104 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: fda021c7ce6ae5e97854b12c2743fbba2a70fcfd
|
|
4
|
+
data.tar.gz: dd1fc76b50edeaafb2b66883508e9872a3a27489
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: abe91a6085dc0a63c3010a68466b42e8379fa96edd342c30fa86040dae5210b505b33fb3f9dd2c90963234664442d5eda7686b41c89ef6bb2c6ff140f2c62544
|
|
7
|
+
data.tar.gz: b7de07b572853f4dafd09a380d75c37cc8fa25fa27f14baed8274f3a7b81ea32492a1f42e80e64c28a3747edfebb076ccc24752d8ead7c958217b71048146cab
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
AllCops:
|
|
2
|
+
TargetRubyVersion: 2.3
|
|
3
|
+
|
|
4
|
+
Style/BlockDelimiters:
|
|
5
|
+
EnforcedStyle: semantic
|
|
6
|
+
|
|
7
|
+
Style/Lambda:
|
|
8
|
+
EnforcedStyle: literal
|
|
9
|
+
|
|
10
|
+
Metrics/BlockLength:
|
|
11
|
+
ExcludedMethods: context, describe, shared_examples_for
|
|
12
|
+
|
|
13
|
+
Layout/MultilineMethodCallIndentation:
|
|
14
|
+
EnforcedStyle: indented_relative_to_receiver
|
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 john@johnwilger.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,51 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
the_help (1.0.0)
|
|
5
|
+
|
|
6
|
+
GEM
|
|
7
|
+
remote: https://rubygems.org/
|
|
8
|
+
specs:
|
|
9
|
+
ast (2.3.0)
|
|
10
|
+
diff-lcs (1.3)
|
|
11
|
+
parallel (1.12.0)
|
|
12
|
+
parser (2.4.0.0)
|
|
13
|
+
ast (~> 2.2)
|
|
14
|
+
powerpack (0.1.1)
|
|
15
|
+
rainbow (2.2.2)
|
|
16
|
+
rake
|
|
17
|
+
rake (10.5.0)
|
|
18
|
+
rspec (3.6.0)
|
|
19
|
+
rspec-core (~> 3.6.0)
|
|
20
|
+
rspec-expectations (~> 3.6.0)
|
|
21
|
+
rspec-mocks (~> 3.6.0)
|
|
22
|
+
rspec-core (3.6.0)
|
|
23
|
+
rspec-support (~> 3.6.0)
|
|
24
|
+
rspec-expectations (3.6.0)
|
|
25
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
26
|
+
rspec-support (~> 3.6.0)
|
|
27
|
+
rspec-mocks (3.6.0)
|
|
28
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
29
|
+
rspec-support (~> 3.6.0)
|
|
30
|
+
rspec-support (3.6.0)
|
|
31
|
+
rubocop (0.50.0)
|
|
32
|
+
parallel (~> 1.10)
|
|
33
|
+
parser (>= 2.3.3.1, < 3.0)
|
|
34
|
+
powerpack (~> 0.1)
|
|
35
|
+
rainbow (>= 2.2.2, < 3.0)
|
|
36
|
+
ruby-progressbar (~> 1.7)
|
|
37
|
+
unicode-display_width (~> 1.0, >= 1.0.1)
|
|
38
|
+
ruby-progressbar (1.8.1)
|
|
39
|
+
unicode-display_width (1.3.0)
|
|
40
|
+
|
|
41
|
+
PLATFORMS
|
|
42
|
+
ruby
|
|
43
|
+
|
|
44
|
+
DEPENDENCIES
|
|
45
|
+
rake (~> 10.0)
|
|
46
|
+
rspec (~> 3.0)
|
|
47
|
+
rubocop (~> 0.50)
|
|
48
|
+
the_help!
|
|
49
|
+
|
|
50
|
+
BUNDLED WITH
|
|
51
|
+
1.16.0.pre.2
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017 John Wilger
|
|
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,43 @@
|
|
|
1
|
+
# TheHelp
|
|
2
|
+
|
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/the_help`. To experiment with that code, run `bin/console` for an interactive prompt.
|
|
4
|
+
|
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
Add this line to your application's Gemfile:
|
|
10
|
+
|
|
11
|
+
```ruby
|
|
12
|
+
gem 'the_help'
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
And then execute:
|
|
16
|
+
|
|
17
|
+
$ bundle
|
|
18
|
+
|
|
19
|
+
Or install it yourself as:
|
|
20
|
+
|
|
21
|
+
$ gem install the_help
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
TODO: Write usage instructions here
|
|
26
|
+
|
|
27
|
+
## Development
|
|
28
|
+
|
|
29
|
+
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.
|
|
30
|
+
|
|
31
|
+
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).
|
|
32
|
+
|
|
33
|
+
## Contributing
|
|
34
|
+
|
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/jwilger/the_help. 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.
|
|
36
|
+
|
|
37
|
+
## License
|
|
38
|
+
|
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
40
|
+
|
|
41
|
+
## Code of Conduct
|
|
42
|
+
|
|
43
|
+
Everyone interacting in the TheHelp project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/jwilger/the_help/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require 'bundler/setup'
|
|
5
|
+
require 'the_help'
|
|
6
|
+
|
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
9
|
+
|
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
11
|
+
# require "pry"
|
|
12
|
+
# Pry.start
|
|
13
|
+
|
|
14
|
+
require 'irb'
|
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/the_help.rb
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module TheHelp
|
|
4
|
+
# Adds a callback DSL to including classes
|
|
5
|
+
#
|
|
6
|
+
# @example
|
|
7
|
+
# class Foo
|
|
8
|
+
# attr_accessor :collaborator
|
|
9
|
+
#
|
|
10
|
+
# def do_something
|
|
11
|
+
# collaborator.do_some_other_thing(when_done: callback(:it_was_done))
|
|
12
|
+
# end
|
|
13
|
+
#
|
|
14
|
+
# callback(:it_was_done) do |some_arg:|
|
|
15
|
+
# puts "Yay! #{some_arg}"
|
|
16
|
+
# end
|
|
17
|
+
# end
|
|
18
|
+
#
|
|
19
|
+
# class Bar
|
|
20
|
+
# def do_some_other_thing(when_done:)
|
|
21
|
+
# when_done.call('done by Bar')
|
|
22
|
+
# end
|
|
23
|
+
# end
|
|
24
|
+
#
|
|
25
|
+
# f = Foo.new
|
|
26
|
+
# f.collaborator = Bar.new
|
|
27
|
+
# f.do_something
|
|
28
|
+
# # STDOUT: "Yay! done by Bar"
|
|
29
|
+
#
|
|
30
|
+
# Callbacks can be given to collaborating objects, but the actual methods are
|
|
31
|
+
# defined as private methods. This allows the object to control which other
|
|
32
|
+
# objects are able to invoke the callbacks (at least to the extent that Ruby
|
|
33
|
+
# lets you do so.)
|
|
34
|
+
#
|
|
35
|
+
# If the including class defines a #logger instance method, a debug-level
|
|
36
|
+
# message will be logged indicating that the callback was invoked.
|
|
37
|
+
module ProvidesCallbacks
|
|
38
|
+
def self.included(other)
|
|
39
|
+
other.class_eval do
|
|
40
|
+
extend TheHelp::ProvidesCallbacks::ClassMethods
|
|
41
|
+
alias_method :callback, :method
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Classes that include ProvidesCallbacks are extended with these
|
|
46
|
+
# ClassMethods
|
|
47
|
+
module ClassMethods
|
|
48
|
+
# Defines a callback method on the class
|
|
49
|
+
#
|
|
50
|
+
# The provided block will be used to define an instance method. This
|
|
51
|
+
# behaves similarly to #define_method, however it will ensure that
|
|
52
|
+
# callbacks are logged if the object has a #logger method defined.
|
|
53
|
+
#
|
|
54
|
+
# @param name [Symbol] The name of the callback
|
|
55
|
+
# @param block [Proc] The code that will be executed in the context of the
|
|
56
|
+
# object when the callback is invoked.
|
|
57
|
+
def callback(name, &block)
|
|
58
|
+
define_method("#{name}_without_logging", &block)
|
|
59
|
+
define_method(name) do
|
|
60
|
+
if defined?(logger)
|
|
61
|
+
logger.debug("#{inspect} received callback :#{name}.")
|
|
62
|
+
end
|
|
63
|
+
send("#{name}_without_logging")
|
|
64
|
+
end
|
|
65
|
+
private name
|
|
66
|
+
self
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'logger'
|
|
4
|
+
require 'the_help/errors'
|
|
5
|
+
require 'the_help/provides_callbacks'
|
|
6
|
+
|
|
7
|
+
module TheHelp
|
|
8
|
+
# An Abstract Service Class with Authorization and Logging
|
|
9
|
+
#
|
|
10
|
+
# Define subclasses of Service to build out the service layer of your
|
|
11
|
+
# application.
|
|
12
|
+
#
|
|
13
|
+
# @example
|
|
14
|
+
# class CreateNewUserAccount < TheHelp::Service
|
|
15
|
+
# input :user
|
|
16
|
+
# input :send_welcome_message, default: true
|
|
17
|
+
#
|
|
18
|
+
# authorization_policy do
|
|
19
|
+
# authorized = false
|
|
20
|
+
# call_service(Authorize, permission: :admin_users,
|
|
21
|
+
# allowed: ->() { authorized = true })
|
|
22
|
+
# authorized
|
|
23
|
+
# end
|
|
24
|
+
#
|
|
25
|
+
# main do
|
|
26
|
+
# # do something to create the user account
|
|
27
|
+
# if send_welcome_message
|
|
28
|
+
# call_service(SendWelcomeMessage, user: user,
|
|
29
|
+
# success: callback(:message_sent))
|
|
30
|
+
# end
|
|
31
|
+
# end
|
|
32
|
+
#
|
|
33
|
+
# callback(:message_sent) do
|
|
34
|
+
# # do something really important, I'm sure
|
|
35
|
+
# end
|
|
36
|
+
# end
|
|
37
|
+
#
|
|
38
|
+
# class Authorize < TheHelp::Service
|
|
39
|
+
# input :permission
|
|
40
|
+
# input :allowed
|
|
41
|
+
#
|
|
42
|
+
# authorization_policy allow_all: true
|
|
43
|
+
#
|
|
44
|
+
# main do
|
|
45
|
+
# if user_has_permission?
|
|
46
|
+
# allowed.call
|
|
47
|
+
# end
|
|
48
|
+
# end
|
|
49
|
+
# end
|
|
50
|
+
#
|
|
51
|
+
# class SendWelcomeMessage < TheHelp::Service
|
|
52
|
+
# input :user
|
|
53
|
+
# input :success, default: ->() { }
|
|
54
|
+
#
|
|
55
|
+
# main do
|
|
56
|
+
# # whatever
|
|
57
|
+
# success.call
|
|
58
|
+
# end
|
|
59
|
+
# end
|
|
60
|
+
#
|
|
61
|
+
# CreateNewUserAccount.(context: current_user, user: new_user_object)
|
|
62
|
+
class Service
|
|
63
|
+
include ProvidesCallbacks
|
|
64
|
+
|
|
65
|
+
# The default :not_authorized callback
|
|
66
|
+
#
|
|
67
|
+
# It will raise a TheHelp::NotAuthorizedError when the context is not
|
|
68
|
+
# authorized to perform the service.
|
|
69
|
+
CB_NOT_AUTHORIZED = ->(service:, context:) {
|
|
70
|
+
raise NotAuthorizedError,
|
|
71
|
+
"Not authorized to access #{service.name} as #{context.inspect}."
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
class << self
|
|
75
|
+
# Convenience method to instantiate the service and immediately call it
|
|
76
|
+
#
|
|
77
|
+
# Any arguments are passed to #initialize
|
|
78
|
+
#
|
|
79
|
+
# @return [Class] Returns the receiver
|
|
80
|
+
def call(*args)
|
|
81
|
+
new(*args).call
|
|
82
|
+
self
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# :nodoc:
|
|
86
|
+
def inherited(other)
|
|
87
|
+
other.instance_variable_set(:@required_inputs, required_inputs.dup)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# :nodoc:
|
|
91
|
+
# instances need access to this, otherwise it would be made private
|
|
92
|
+
def required_inputs
|
|
93
|
+
@required_inputs ||= Set.new
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Defines the primary routine of the service
|
|
97
|
+
#
|
|
98
|
+
# The code that will be run when the service is called, assuming it is
|
|
99
|
+
# unauthorized.
|
|
100
|
+
def main(&block)
|
|
101
|
+
define_method(:main, &block)
|
|
102
|
+
private :main
|
|
103
|
+
self
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# Defines the service authorization policy
|
|
107
|
+
#
|
|
108
|
+
# If allow_all is set to true, or if the provided block (executed in the
|
|
109
|
+
# context of the service object) returns true, then the service will be
|
|
110
|
+
# run when called. Otherwise, the not_authorized callback will be invoked.
|
|
111
|
+
#
|
|
112
|
+
# @param allow_all [Boolean]
|
|
113
|
+
# @param block [Proc] executed in the context of the service instance (and
|
|
114
|
+
# can therefore access all inputs to the service)
|
|
115
|
+
def authorization_policy(allow_all: false, &block)
|
|
116
|
+
if allow_all
|
|
117
|
+
define_method(:authorized?) { true }
|
|
118
|
+
else
|
|
119
|
+
define_method(:authorized?, &block)
|
|
120
|
+
end
|
|
121
|
+
private :authorized?
|
|
122
|
+
self
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def input(name, **options)
|
|
126
|
+
attr_accessor name
|
|
127
|
+
if options.key?(:default)
|
|
128
|
+
required_inputs.delete(name)
|
|
129
|
+
define_method(name) do
|
|
130
|
+
instance_variable_get("@#{name}") || options[:default]
|
|
131
|
+
end
|
|
132
|
+
else
|
|
133
|
+
required_inputs << name
|
|
134
|
+
end
|
|
135
|
+
private name, "#{name}="
|
|
136
|
+
self
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def initialize(context:, logger: Logger.new($stdout),
|
|
141
|
+
not_authorized: CB_NOT_AUTHORIZED, **inputs)
|
|
142
|
+
self.context = context
|
|
143
|
+
self.logger = logger
|
|
144
|
+
self.not_authorized = not_authorized
|
|
145
|
+
self.inputs = inputs
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def call
|
|
149
|
+
validate_service_definition
|
|
150
|
+
catch(:stop) do
|
|
151
|
+
authorize
|
|
152
|
+
log_service_call
|
|
153
|
+
main
|
|
154
|
+
end
|
|
155
|
+
self
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
private
|
|
159
|
+
|
|
160
|
+
attr_accessor :context, :logger, :not_authorized
|
|
161
|
+
attr_reader :inputs
|
|
162
|
+
|
|
163
|
+
def inputs=(inputs)
|
|
164
|
+
@inputs = inputs
|
|
165
|
+
inputs.each { |name, value| send("#{name}=", value) }
|
|
166
|
+
validate_inputs
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
def validate_inputs
|
|
170
|
+
self.class.required_inputs.each do |r_input_name|
|
|
171
|
+
next if inputs.key?(r_input_name)
|
|
172
|
+
raise ArgumentError, "Missing required input: #{r_input_name}."
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def validate_service_definition
|
|
177
|
+
raise AbstractClassError if self.class == TheHelp::Service
|
|
178
|
+
raise ServiceNotImplementedError unless defined?(main)
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
def log_service_call
|
|
182
|
+
logger.info("Service call to #{self.class.name} for #{context.inspect}")
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def authorized?
|
|
186
|
+
false
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
def authorize
|
|
190
|
+
return if authorized?
|
|
191
|
+
logger.warn("Unauthorized attempt to access #{self.class.name} " \
|
|
192
|
+
"as #{context.inspect}")
|
|
193
|
+
not_authorized.call(service: self.class, context: context)
|
|
194
|
+
throw :stop
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
end
|
data/the_help.gemspec
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
5
|
+
require 'the_help/version'
|
|
6
|
+
|
|
7
|
+
Gem::Specification.new do |spec|
|
|
8
|
+
spec.name = 'the_help'
|
|
9
|
+
spec.version = TheHelp::VERSION
|
|
10
|
+
spec.authors = ['John Wilger']
|
|
11
|
+
spec.email = ['john@johnwilger.com']
|
|
12
|
+
|
|
13
|
+
spec.summary = 'A service layer framework'
|
|
14
|
+
spec.description = 'A service layer framework'
|
|
15
|
+
spec.homepage = 'https://github.com/jwilger/the_help'
|
|
16
|
+
spec.license = 'MIT'
|
|
17
|
+
|
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
|
19
|
+
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 'rake', '~> 10.0'
|
|
26
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
|
27
|
+
spec.add_development_dependency 'rubocop', '~> 0.50'
|
|
28
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: the_help
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- John Wilger
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2017-10-13 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: rake
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '10.0'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '10.0'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rspec
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '3.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '3.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rubocop
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0.50'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0.50'
|
|
55
|
+
description: A service layer framework
|
|
56
|
+
email:
|
|
57
|
+
- john@johnwilger.com
|
|
58
|
+
executables: []
|
|
59
|
+
extensions: []
|
|
60
|
+
extra_rdoc_files: []
|
|
61
|
+
files:
|
|
62
|
+
- ".gitignore"
|
|
63
|
+
- ".rspec"
|
|
64
|
+
- ".rubocop.yml"
|
|
65
|
+
- ".travis.yml"
|
|
66
|
+
- CODE_OF_CONDUCT.md
|
|
67
|
+
- Gemfile
|
|
68
|
+
- Gemfile.lock
|
|
69
|
+
- LICENSE.txt
|
|
70
|
+
- README.md
|
|
71
|
+
- Rakefile
|
|
72
|
+
- bin/console
|
|
73
|
+
- bin/setup
|
|
74
|
+
- lib/the_help.rb
|
|
75
|
+
- lib/the_help/errors.rb
|
|
76
|
+
- lib/the_help/provides_callbacks.rb
|
|
77
|
+
- lib/the_help/service.rb
|
|
78
|
+
- lib/the_help/version.rb
|
|
79
|
+
- the_help.gemspec
|
|
80
|
+
homepage: https://github.com/jwilger/the_help
|
|
81
|
+
licenses:
|
|
82
|
+
- MIT
|
|
83
|
+
metadata: {}
|
|
84
|
+
post_install_message:
|
|
85
|
+
rdoc_options: []
|
|
86
|
+
require_paths:
|
|
87
|
+
- lib
|
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
89
|
+
requirements:
|
|
90
|
+
- - ">="
|
|
91
|
+
- !ruby/object:Gem::Version
|
|
92
|
+
version: '0'
|
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
94
|
+
requirements:
|
|
95
|
+
- - ">="
|
|
96
|
+
- !ruby/object:Gem::Version
|
|
97
|
+
version: '0'
|
|
98
|
+
requirements: []
|
|
99
|
+
rubyforge_project:
|
|
100
|
+
rubygems_version: 2.6.12
|
|
101
|
+
signing_key:
|
|
102
|
+
specification_version: 4
|
|
103
|
+
summary: A service layer framework
|
|
104
|
+
test_files: []
|