catch_box 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/.github/workflows/rspec.yml +24 -0
- data/.github/workflows/rubocop.yml +20 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.rubocop.yml +21 -0
- data/.simplecov +3 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +85 -0
- data/Rakefile +8 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/catch_box.gemspec +39 -0
- data/demo/mailgun.rb +70 -0
- data/demo/mailgun_legacy.rb +70 -0
- data/gemfiles/rubocop.gemfile +7 -0
- data/lib/catch_box.rb +8 -0
- data/lib/catch_box/auth.rb +21 -0
- data/lib/catch_box/emitter.rb +13 -0
- data/lib/catch_box/event.rb +19 -0
- data/lib/catch_box/fanout.rb +86 -0
- data/lib/catch_box/hook.rb +17 -0
- data/lib/catch_box/hook/all.rb +13 -0
- data/lib/catch_box/hook/factory.rb +18 -0
- data/lib/catch_box/hook/one.rb +19 -0
- data/lib/catch_box/middleware.rb +49 -0
- data/lib/catch_box/on.rb +17 -0
- data/lib/catch_box/version.rb +5 -0
- metadata +190 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a46fdb10f4317c96ce8af430e44d876e21e87fae72b02b102c92c0465ef3cc86
|
4
|
+
data.tar.gz: 0f4743e78985e39538ba5291843c04950a4ac8254108c0d7fa75f695b9d55907
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7d2641820d3b4f72a3ac3a93049c570382f34518904799a468b616e485a6466bf782b6ef0126776b09128f3c5250fdc5bf46af6d2990d646db680eea7a68b35c
|
7
|
+
data.tar.gz: f2f03fcb35178b1b11117163d7a0580becd791a82499282ed0f728a402e809c6a445a81ca61375e3bf61b60eceb31fab114f5d686166a616915e5f67b6cf3dd3
|
@@ -0,0 +1,24 @@
|
|
1
|
+
name: RSpec
|
2
|
+
|
3
|
+
on:
|
4
|
+
pull_request:
|
5
|
+
|
6
|
+
jobs:
|
7
|
+
default:
|
8
|
+
runs-on: ubuntu-latest
|
9
|
+
strategy:
|
10
|
+
fail-fast: false
|
11
|
+
matrix:
|
12
|
+
ruby: [2.4, 2.5, 2.6, 2.7]
|
13
|
+
steps:
|
14
|
+
- uses: actions/checkout@v2
|
15
|
+
- name: Set up Ruby
|
16
|
+
uses: actions/setup-ruby@v1
|
17
|
+
with:
|
18
|
+
ruby-version: ${{ matrix.ruby }}
|
19
|
+
- run: |
|
20
|
+
gem install bundler
|
21
|
+
- run: |
|
22
|
+
bundle install --jobs 4 --retry 3
|
23
|
+
- run: |
|
24
|
+
bundle exec rspec
|
@@ -0,0 +1,20 @@
|
|
1
|
+
name: RuboCop
|
2
|
+
|
3
|
+
on:
|
4
|
+
pull_request:
|
5
|
+
|
6
|
+
jobs:
|
7
|
+
default:
|
8
|
+
runs-on: ubuntu-latest
|
9
|
+
steps:
|
10
|
+
- uses: actions/checkout@v2
|
11
|
+
- name: Set up Ruby
|
12
|
+
uses: actions/setup-ruby@v1
|
13
|
+
with:
|
14
|
+
ruby-version: 2.4.x
|
15
|
+
- run: |
|
16
|
+
gem install bundler
|
17
|
+
- run: |
|
18
|
+
bundle install --gemfile gemfiles/rubocop.gemfile --jobs 4 --retry 3
|
19
|
+
- run: |
|
20
|
+
bundle exec --gemfile gemfiles/rubocop.gemfile rubocop
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require:
|
2
|
+
- standard/cop/semantic_blocks
|
3
|
+
- rubocop-rspec
|
4
|
+
|
5
|
+
inherit_gem:
|
6
|
+
standard: config/base.yml
|
7
|
+
|
8
|
+
AllCops:
|
9
|
+
Exclude:
|
10
|
+
- 'bin/*'
|
11
|
+
DisplayCopNames: true
|
12
|
+
TargetRubyVersion: 2.4
|
13
|
+
|
14
|
+
Style/FrozenStringLiteralComment:
|
15
|
+
Enabled: true
|
16
|
+
|
17
|
+
Style/TrailingCommaInArrayLiteral:
|
18
|
+
EnforcedStyleForMultiline: no_comma
|
19
|
+
|
20
|
+
Style/TrailingCommaInHashLiteral:
|
21
|
+
EnforcedStyleForMultiline: no_comma
|
data/.simplecov
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Artem Chubchenko
|
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,85 @@
|
|
1
|
+
[![Gem Version](https://badge.fury.io/rb/catch_box.svg)](https://badge.fury.io/rb/catch_box)
|
2
|
+
[![RSpec](https://github.com/chubchenko/catch_box/workflows/RSpec/badge.svg)](https://github.com/chubchenko/catch_box/actions)
|
3
|
+
|
4
|
+
# Catch Box
|
5
|
+
|
6
|
+
A lightweight and straightforward system for easy hooks set up.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's `Gemfile`:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem "catch_box", "~> 0.1.0"
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
```bash
|
19
|
+
bundle
|
20
|
+
```
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
### Base
|
25
|
+
|
26
|
+
> Define fanout
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
require "catch_box/fanout"
|
30
|
+
|
31
|
+
class Delivered
|
32
|
+
def initialize(logger: Logger.new)
|
33
|
+
@logger = logger
|
34
|
+
end
|
35
|
+
|
36
|
+
def call(payload)
|
37
|
+
@logger.info(payload)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class Fanout
|
42
|
+
extend ::CatchBox::Fanout
|
43
|
+
|
44
|
+
event "event-data.event"
|
45
|
+
|
46
|
+
auth do |payload, env|
|
47
|
+
# https://documentation.mailgun.com/en/latest/user_manual.html#webhooks
|
48
|
+
payload["signature"]["signature"] == \
|
49
|
+
::OpenSSL::HMAC.hexdigest(
|
50
|
+
::OpenSSL::Digest::SHA256.new,
|
51
|
+
ENV["MAILGUN_API_KEY"],
|
52
|
+
[payload["signature"]["timestamp"], payload["signature"]["token"]].join
|
53
|
+
)
|
54
|
+
end
|
55
|
+
|
56
|
+
on "delivered", Delivered.new
|
57
|
+
|
58
|
+
all do |payload|
|
59
|
+
::Logger.new(::STDOUT).info(payload)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
```
|
63
|
+
|
64
|
+
> Use middleware
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
require "sinatra/base"
|
68
|
+
require "catch_box/middleware"
|
69
|
+
|
70
|
+
class Application < Sinatra::Base
|
71
|
+
use ::CatchBox::Middleware, fanout: Fanout, endpoint: "/mailgun"
|
72
|
+
|
73
|
+
get "/" do
|
74
|
+
"index"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
```
|
78
|
+
|
79
|
+
## Contributing
|
80
|
+
|
81
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/chubchenko/catch_box.
|
82
|
+
|
83
|
+
## License
|
84
|
+
|
85
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "catch_box"
|
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
data/catch_box.gemspec
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path("lib", __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require "catch_box/version"
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "catch_box"
|
9
|
+
spec.version = CatchBox::VERSION
|
10
|
+
spec.authors = ["Artem Chubchenko"]
|
11
|
+
spec.email = ["artem.chubchenko@gmail.com"]
|
12
|
+
|
13
|
+
spec.summary = "A lightweight and straightforward system for easy hooks set up"
|
14
|
+
spec.description = spec.summary
|
15
|
+
spec.homepage = "https://github.com/chubchenko/catch_box"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
spec.metadata = {
|
19
|
+
"bug_tracker_uri" => "https://github.com/chubchenko/catch_box/issues",
|
20
|
+
"changelog_uri" => "https://github.com/chubchenko/catch_box/blob/master/CHANGELOG.md",
|
21
|
+
"source_code_uri" => "https://github.com/chubchenko/catch_box/tree/v#{spec.version}"
|
22
|
+
}
|
23
|
+
|
24
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
25
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
end
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
|
29
|
+
spec.required_ruby_version = ">= 2.4.0"
|
30
|
+
|
31
|
+
spec.add_dependency "dry-configurable", "~> 0.9.0"
|
32
|
+
spec.add_dependency "rack", "~> 2.1", ">= 2.1.1"
|
33
|
+
|
34
|
+
spec.add_development_dependency "bundler", "~> 2.1", ">= 2.1.4"
|
35
|
+
spec.add_development_dependency "rack-test", "~> 1.1"
|
36
|
+
spec.add_development_dependency "rake", "~> 13.0", ">= 13.0.1"
|
37
|
+
spec.add_development_dependency "rspec", "~> 3.9"
|
38
|
+
spec.add_development_dependency "simplecov", "~> 0.17.1"
|
39
|
+
end
|
data/demo/mailgun.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "catch_box"
|
4
|
+
require "logger"
|
5
|
+
require "openssl"
|
6
|
+
|
7
|
+
# ======================================================================================================================
|
8
|
+
|
9
|
+
module Mailgun
|
10
|
+
class Delivered
|
11
|
+
def initialize(logger: ::Logger.new(::STDOUT))
|
12
|
+
@logger = logger
|
13
|
+
end
|
14
|
+
|
15
|
+
def call(payload)
|
16
|
+
@logger.info(payload)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class Auth
|
21
|
+
def initialize(api_key: ENV["MAILGUN_API_KEY"])
|
22
|
+
@api_key = api_key
|
23
|
+
end
|
24
|
+
|
25
|
+
def call(payload, env)
|
26
|
+
payload["signature"]["signature"] == \
|
27
|
+
::OpenSSL::HMAC.hexdigest(
|
28
|
+
::OpenSSL::Digest::SHA256.new,
|
29
|
+
api_key,
|
30
|
+
[payload["signature"]["timestamp"], payload["signature"]["token"]].join
|
31
|
+
)
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
attr_reader :api_key
|
37
|
+
end
|
38
|
+
|
39
|
+
class Fanout
|
40
|
+
extend ::CatchBox::Fanout
|
41
|
+
|
42
|
+
event "event-data.event"
|
43
|
+
|
44
|
+
auth ::Mailgun::Auth.new
|
45
|
+
|
46
|
+
on "delivered", ::Mailgun::Delivered.new
|
47
|
+
|
48
|
+
all do |payload|
|
49
|
+
::Logger.new(::STDOUT).info(payload)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# ======================================================================================================================
|
55
|
+
|
56
|
+
require "rack/lobster"
|
57
|
+
|
58
|
+
Application = Rack::Builder.new {
|
59
|
+
use ::CatchBox::Middleware, fanout: ::Mailgun::Fanout, endpoint: "/mailgun"
|
60
|
+
|
61
|
+
map "/" do
|
62
|
+
run(
|
63
|
+
proc do
|
64
|
+
[200, {"Content-Type" => "text/plain"}, ["o_O"]]
|
65
|
+
end
|
66
|
+
)
|
67
|
+
end
|
68
|
+
}
|
69
|
+
|
70
|
+
Rack::Handler::WEBrick.run(Application)
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "catch_box"
|
4
|
+
require "logger"
|
5
|
+
require "openssl"
|
6
|
+
|
7
|
+
# ======================================================================================================================
|
8
|
+
|
9
|
+
module Mailgun
|
10
|
+
class Delivered
|
11
|
+
def initialize(logger: ::Logger.new(::STDOUT))
|
12
|
+
@logger = logger
|
13
|
+
end
|
14
|
+
|
15
|
+
def call(payload)
|
16
|
+
@logger.info(payload)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class Auth
|
21
|
+
def initialize(api_key: ENV["MAILGUN_API_KEY"])
|
22
|
+
@api_key = api_key
|
23
|
+
end
|
24
|
+
|
25
|
+
def call(payload, env)
|
26
|
+
payload["signature"] == \
|
27
|
+
::OpenSSL::HMAC.hexdigest(
|
28
|
+
::OpenSSL::Digest::SHA256.new,
|
29
|
+
api_key,
|
30
|
+
[payload["timestamp"], payload["token"]].join
|
31
|
+
)
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
attr_reader :api_key
|
37
|
+
end
|
38
|
+
|
39
|
+
class Fanout
|
40
|
+
extend ::CatchBox::Fanout
|
41
|
+
|
42
|
+
event "event"
|
43
|
+
|
44
|
+
auth ::Mailgun::Auth.new
|
45
|
+
|
46
|
+
on "delivered", ::Mailgun::Delivered.new
|
47
|
+
|
48
|
+
all do |payload|
|
49
|
+
::Logger.new(::STDOUT).info(payload)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# ======================================================================================================================
|
55
|
+
|
56
|
+
require "rack/lobster"
|
57
|
+
|
58
|
+
Application = Rack::Builder.new {
|
59
|
+
use ::CatchBox::Middleware, fanout: ::Mailgun::Fanout, endpoint: "/mailgun"
|
60
|
+
|
61
|
+
map "/" do
|
62
|
+
run(
|
63
|
+
proc do
|
64
|
+
[200, {"Content-Type" => "text/plain"}, ["o_O"]]
|
65
|
+
end
|
66
|
+
)
|
67
|
+
end
|
68
|
+
}
|
69
|
+
|
70
|
+
Rack::Handler::WEBrick.run(Application)
|
data/lib/catch_box.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CatchBox
|
4
|
+
NotAuthorized = ::Class.new(::StandardError)
|
5
|
+
|
6
|
+
class Auth
|
7
|
+
def initialize(auth: proc { false })
|
8
|
+
@auth = auth
|
9
|
+
end
|
10
|
+
|
11
|
+
def call(auth)
|
12
|
+
@auth = auth
|
13
|
+
end
|
14
|
+
|
15
|
+
def map(payload, env)
|
16
|
+
return if @auth.call(payload, env)
|
17
|
+
|
18
|
+
raise ::CatchBox::NotAuthorized
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CatchBox
|
4
|
+
class Event
|
5
|
+
DELIMITER = "."
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@name = nil
|
9
|
+
end
|
10
|
+
|
11
|
+
def call(name)
|
12
|
+
@name = name
|
13
|
+
end
|
14
|
+
|
15
|
+
def map(payload)
|
16
|
+
payload.dig(*@name.split(DELIMITER))
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "dry-configurable"
|
4
|
+
require "catch_box/event"
|
5
|
+
require "catch_box/auth"
|
6
|
+
require "catch_box/on"
|
7
|
+
require "catch_box/emitter"
|
8
|
+
|
9
|
+
module CatchBox
|
10
|
+
module Fanout
|
11
|
+
module Initialize
|
12
|
+
def initialize
|
13
|
+
@_fanout = []
|
14
|
+
|
15
|
+
super
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.included(descendant)
|
20
|
+
descendant.class_eval do
|
21
|
+
extend ::Dry::Configurable
|
22
|
+
prepend ::CatchBox::Fanout::Initialize
|
23
|
+
|
24
|
+
setting :event, ::CatchBox::Event.new
|
25
|
+
setting :auth, ::CatchBox::Auth.new
|
26
|
+
|
27
|
+
setting :on, ::CatchBox::On.new
|
28
|
+
setting :emitter, ::CatchBox::Emitter.new
|
29
|
+
|
30
|
+
def config
|
31
|
+
self.class.config
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.extended(descendant)
|
37
|
+
descendant.class_eval do
|
38
|
+
extend ::Dry::Configurable
|
39
|
+
|
40
|
+
setting :event, ::CatchBox::Event.new
|
41
|
+
setting :auth, ::CatchBox::Auth.new
|
42
|
+
|
43
|
+
setting :on, ::CatchBox::On.new
|
44
|
+
setting :emitter, ::CatchBox::Emitter.new
|
45
|
+
|
46
|
+
@_fanout = []
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def event(callable = nil, &block)
|
51
|
+
config.event.call(callable || block)
|
52
|
+
end
|
53
|
+
|
54
|
+
def auth(callable = nil, &block)
|
55
|
+
config.auth.call(callable || block)
|
56
|
+
end
|
57
|
+
|
58
|
+
def on(pattern, callable = nil, &block)
|
59
|
+
config.on.call(
|
60
|
+
_fanout, pattern, callable || block
|
61
|
+
)
|
62
|
+
end
|
63
|
+
|
64
|
+
def all(callable = nil, &block)
|
65
|
+
config.on.call(
|
66
|
+
_fanout, nil, callable || block
|
67
|
+
)
|
68
|
+
end
|
69
|
+
|
70
|
+
def emit(payload, env)
|
71
|
+
config.auth.map(payload, env)
|
72
|
+
|
73
|
+
pattern = config.event.map(payload)
|
74
|
+
|
75
|
+
config.emitter.call(
|
76
|
+
_fanout, pattern, payload
|
77
|
+
)
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
def _fanout
|
83
|
+
@_fanout
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "catch_box/hook/all"
|
4
|
+
require "catch_box/hook/one"
|
5
|
+
|
6
|
+
module CatchBox
|
7
|
+
class Hook
|
8
|
+
class Factory
|
9
|
+
def call(pattern, hook)
|
10
|
+
if pattern.nil?
|
11
|
+
::CatchBox::Hook::All.new(hook)
|
12
|
+
else
|
13
|
+
::CatchBox::Hook::One.new(pattern, hook)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "catch_box/hook"
|
4
|
+
|
5
|
+
module CatchBox
|
6
|
+
class Hook
|
7
|
+
class One < ::CatchBox::Hook
|
8
|
+
def initialize(pattern, hook)
|
9
|
+
@pattern = pattern
|
10
|
+
|
11
|
+
super(hook)
|
12
|
+
end
|
13
|
+
|
14
|
+
def match?(pattern)
|
15
|
+
@pattern == pattern
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rack/request"
|
4
|
+
require "rack/utils"
|
5
|
+
|
6
|
+
module CatchBox
|
7
|
+
class Middleware
|
8
|
+
def initialize(app, fanout:, endpoint:)
|
9
|
+
@app = app
|
10
|
+
@fanout = fanout
|
11
|
+
@endpoint = endpoint
|
12
|
+
end
|
13
|
+
|
14
|
+
def call(env)
|
15
|
+
dup.call!(env)
|
16
|
+
end
|
17
|
+
|
18
|
+
protected
|
19
|
+
|
20
|
+
def call!(env)
|
21
|
+
request = ::Rack::Request.new(env)
|
22
|
+
|
23
|
+
return @app.call(env) unless request.post?
|
24
|
+
return @app.call(env) unless request.path == @endpoint
|
25
|
+
|
26
|
+
body = (
|
27
|
+
request.body.rewind
|
28
|
+
request.body.read
|
29
|
+
)
|
30
|
+
|
31
|
+
payload = ::Rack::Utils.parse_query(body) if request.content_type == "application/x-www-form-urlencoded"
|
32
|
+
payload = ::JSON.parse(body) if %r{application/json}i.match?(request.content_type)
|
33
|
+
|
34
|
+
@fanout.emit(payload, request.env)
|
35
|
+
|
36
|
+
[
|
37
|
+
200,
|
38
|
+
{},
|
39
|
+
[]
|
40
|
+
]
|
41
|
+
rescue ::CatchBox::NotAuthorized
|
42
|
+
[
|
43
|
+
400,
|
44
|
+
{},
|
45
|
+
[]
|
46
|
+
]
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/lib/catch_box/on.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "catch_box/hook/factory"
|
4
|
+
|
5
|
+
module CatchBox
|
6
|
+
class On
|
7
|
+
def call(fanout, pattern, hook)
|
8
|
+
fanout << factory.call(pattern, hook)
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def factory
|
14
|
+
@factory ||= ::CatchBox::Hook::Factory.new
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,190 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: catch_box
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Artem Chubchenko
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-01-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: dry-configurable
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.9.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.9.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rack
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.1'
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: 2.1.1
|
37
|
+
type: :runtime
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - "~>"
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '2.1'
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 2.1.1
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: bundler
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '2.1'
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: 2.1.4
|
57
|
+
type: :development
|
58
|
+
prerelease: false
|
59
|
+
version_requirements: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - "~>"
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '2.1'
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: 2.1.4
|
67
|
+
- !ruby/object:Gem::Dependency
|
68
|
+
name: rack-test
|
69
|
+
requirement: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - "~>"
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '1.1'
|
74
|
+
type: :development
|
75
|
+
prerelease: false
|
76
|
+
version_requirements: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - "~>"
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '1.1'
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
name: rake
|
83
|
+
requirement: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - "~>"
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '13.0'
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: 13.0.1
|
91
|
+
type: :development
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - "~>"
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '13.0'
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: 13.0.1
|
101
|
+
- !ruby/object:Gem::Dependency
|
102
|
+
name: rspec
|
103
|
+
requirement: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - "~>"
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '3.9'
|
108
|
+
type: :development
|
109
|
+
prerelease: false
|
110
|
+
version_requirements: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - "~>"
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '3.9'
|
115
|
+
- !ruby/object:Gem::Dependency
|
116
|
+
name: simplecov
|
117
|
+
requirement: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - "~>"
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: 0.17.1
|
122
|
+
type: :development
|
123
|
+
prerelease: false
|
124
|
+
version_requirements: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - "~>"
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: 0.17.1
|
129
|
+
description: A lightweight and straightforward system for easy hooks set up
|
130
|
+
email:
|
131
|
+
- artem.chubchenko@gmail.com
|
132
|
+
executables: []
|
133
|
+
extensions: []
|
134
|
+
extra_rdoc_files: []
|
135
|
+
files:
|
136
|
+
- ".github/workflows/rspec.yml"
|
137
|
+
- ".github/workflows/rubocop.yml"
|
138
|
+
- ".gitignore"
|
139
|
+
- ".rspec"
|
140
|
+
- ".rubocop.yml"
|
141
|
+
- ".simplecov"
|
142
|
+
- Gemfile
|
143
|
+
- LICENSE.txt
|
144
|
+
- README.md
|
145
|
+
- Rakefile
|
146
|
+
- bin/console
|
147
|
+
- bin/setup
|
148
|
+
- catch_box.gemspec
|
149
|
+
- demo/mailgun.rb
|
150
|
+
- demo/mailgun_legacy.rb
|
151
|
+
- gemfiles/rubocop.gemfile
|
152
|
+
- lib/catch_box.rb
|
153
|
+
- lib/catch_box/auth.rb
|
154
|
+
- lib/catch_box/emitter.rb
|
155
|
+
- lib/catch_box/event.rb
|
156
|
+
- lib/catch_box/fanout.rb
|
157
|
+
- lib/catch_box/hook.rb
|
158
|
+
- lib/catch_box/hook/all.rb
|
159
|
+
- lib/catch_box/hook/factory.rb
|
160
|
+
- lib/catch_box/hook/one.rb
|
161
|
+
- lib/catch_box/middleware.rb
|
162
|
+
- lib/catch_box/on.rb
|
163
|
+
- lib/catch_box/version.rb
|
164
|
+
homepage: https://github.com/chubchenko/catch_box
|
165
|
+
licenses:
|
166
|
+
- MIT
|
167
|
+
metadata:
|
168
|
+
bug_tracker_uri: https://github.com/chubchenko/catch_box/issues
|
169
|
+
changelog_uri: https://github.com/chubchenko/catch_box/blob/master/CHANGELOG.md
|
170
|
+
source_code_uri: https://github.com/chubchenko/catch_box/tree/v0.1.0
|
171
|
+
post_install_message:
|
172
|
+
rdoc_options: []
|
173
|
+
require_paths:
|
174
|
+
- lib
|
175
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
176
|
+
requirements:
|
177
|
+
- - ">="
|
178
|
+
- !ruby/object:Gem::Version
|
179
|
+
version: 2.4.0
|
180
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
181
|
+
requirements:
|
182
|
+
- - ">="
|
183
|
+
- !ruby/object:Gem::Version
|
184
|
+
version: '0'
|
185
|
+
requirements: []
|
186
|
+
rubygems_version: 3.0.2
|
187
|
+
signing_key:
|
188
|
+
specification_version: 4
|
189
|
+
summary: A lightweight and straightforward system for easy hooks set up
|
190
|
+
test_files: []
|