interceptors 0.1.1 → 1.0.2
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 +4 -4
- data/CHANGELOG.md +4 -0
- data/Gemfile +3 -2
- data/{LICENSE.txt → LICENSE} +6 -6
- data/README.md +121 -28
- data/Rakefile +0 -5
- data/lib/interceptors/app_error.rb +23 -0
- data/lib/interceptors/auth_error.rb +9 -0
- data/lib/interceptors/idempotency_interceptor.rb +75 -0
- data/lib/interceptors/interceptor.rb +13 -0
- data/lib/interceptors/logging_interceptor.rb +32 -0
- data/lib/interceptors/pipeline.rb +23 -0
- data/lib/interceptors/rails/use_case_responder.rb +24 -0
- data/lib/interceptors/result.rb +39 -0
- data/lib/interceptors/retry_interceptor.rb +36 -0
- data/lib/interceptors/timeout_interceptor.rb +19 -0
- data/lib/interceptors/transaction_interceptor.rb +26 -0
- data/lib/interceptors/use_case.rb +117 -0
- data/lib/interceptors/validation_error.rb +19 -0
- data/lib/interceptors/validation_interceptor.rb +27 -0
- data/lib/interceptors/version.rb +3 -1
- data/lib/interceptors.rb +53 -9
- metadata +37 -35
- data/.gitignore +0 -13
- data/.rspec +0 -3
- data/.travis.yml +0 -5
- data/CODE_OF_CONDUCT.md +0 -74
- data/Gemfile.lock +0 -35
- data/bin/console +0 -14
- data/bin/setup +0 -8
- data/interceptors.gemspec +0 -36
- data/lib/interceptors/base.rb +0 -17
- data/lib/interceptors/context.rb +0 -6
- data/lib/interceptors/ds/errors.rb +0 -15
- data/lib/interceptors/ds/node.rb +0 -12
- data/lib/interceptors/ds/stack.rb +0 -30
- data/lib/interceptors/executor.rb +0 -45
- data/lib/interceptors/middleware.rb +0 -28
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a4c31ab76d3b09a2a51c674ceb27aaaaf82cda31e6267b56be86b67ee77a9155
|
|
4
|
+
data.tar.gz: f1a4039246aa384126f1391faa4e879d3f4a8d3b98900f95f88e5b5bbe7aa60e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8a5edaa5997b949864f547e50d2226b75af99fd9bc214cbff7b8e4eec4f23e6546e104edc46978aed03d6ae0b860d1955f2125a92b5b7c2a1105d9ceef3ab9fb
|
|
7
|
+
data.tar.gz: 942e90230e3bd6ecbff48ab19d2bbb81543cf09e805c3dec779731fbab1515537270d522172a97c08c03e7efb9c69031b9920245272beb1c89844e3458fc5842
|
data/CHANGELOG.md
ADDED
data/Gemfile
CHANGED
data/{LICENSE.txt → LICENSE}
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c)
|
|
3
|
+
Copyright (c) 2024 Interceptors Maintainers
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
|
@@ -9,13 +9,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
9
9
|
copies of the Software, and to permit persons to whom the Software is
|
|
10
10
|
furnished to do so, subject to the following conditions:
|
|
11
11
|
|
|
12
|
-
The above copyright notice and this permission notice shall be included in
|
|
13
|
-
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
14
|
|
|
15
15
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
16
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
17
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
18
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
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
|
-
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
CHANGED
|
@@ -1,52 +1,145 @@
|
|
|
1
1
|
# Interceptors
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Interceptor-driven use case toolkit for Ruby and Rails applications.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
- Consistent `Result` object with `ok?`/`err?` helpers and metadata.
|
|
7
|
+
- Base `UseCase` class with interceptor pipeline support.
|
|
8
|
+
- Built-in interceptors for logging, validation, retries, timeouts, transactions, and idempotency.
|
|
9
|
+
- Uniform error taxonomy (`AppError`, `ValidationError`, `AuthError`) with HTTP semantics.
|
|
10
|
+
- Instrumentation via `ActiveSupport::Notifications`.
|
|
11
|
+
- Optional Rails responder helper for controllers or jobs.
|
|
4
12
|
|
|
5
13
|
## Installation
|
|
6
14
|
|
|
7
|
-
Add
|
|
15
|
+
Add the gem to your bundle:
|
|
8
16
|
|
|
9
17
|
```ruby
|
|
10
|
-
gem
|
|
18
|
+
gem "interceptors"
|
|
11
19
|
```
|
|
12
20
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
$ bundle
|
|
21
|
+
## Usage
|
|
16
22
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
23
|
+
```ruby
|
|
24
|
+
class CreateUser < Interceptors::UseCase
|
|
25
|
+
use Interceptors::LoggingInterceptor.new
|
|
26
|
+
use Interceptors::ValidationInterceptor.new do |ctx|
|
|
27
|
+
errors = {}
|
|
28
|
+
errors[:email] = "is required" if ctx[:email].to_s.strip.empty?
|
|
29
|
+
errors
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def execute(ctx)
|
|
35
|
+
user = User.create!(email: ctx[:email])
|
|
36
|
+
Interceptors::Result.ok(user)
|
|
37
|
+
rescue ActiveRecord::RecordInvalid => e
|
|
38
|
+
Interceptors::Result.err(Interceptors::ValidationError.new(e.record.errors.to_hash))
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
result = CreateUser.call(email: "user@example.com")
|
|
43
|
+
result.ok? #=> true
|
|
44
|
+
```
|
|
20
45
|
|
|
21
|
-
|
|
46
|
+
### Richer example: checkout flow
|
|
22
47
|
|
|
23
|
-
|
|
48
|
+
```ruby
|
|
49
|
+
class CheckoutOrder < Interceptors::UseCase
|
|
50
|
+
use Interceptors::LoggingInterceptor.new
|
|
51
|
+
use Interceptors::TimeoutInterceptor.new(seconds: 3)
|
|
52
|
+
use Interceptors::RetryInterceptor.new(tries: 3, on: [ActiveRecord::Deadlocked])
|
|
53
|
+
use Interceptors::TransactionInterceptor.new
|
|
54
|
+
use Interceptors::IdempotencyInterceptor.new(key_proc: ->(ctx) { "checkout:#{ctx[:idempotency_key]}" })
|
|
55
|
+
|
|
56
|
+
use Interceptors::ValidationInterceptor.new do |ctx|
|
|
57
|
+
errors = {}
|
|
58
|
+
errors[:cart_id] = "is required" if ctx[:cart_id].to_s.empty?
|
|
59
|
+
errors[:payment_token] = "is required" if ctx[:payment_token].to_s.empty?
|
|
60
|
+
errors
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
private
|
|
64
|
+
|
|
65
|
+
def execute(ctx)
|
|
66
|
+
guard_policy!(ctx[:actor], ctx[:cart_id])
|
|
67
|
+
|
|
68
|
+
cart = Cart.lock.find(ctx[:cart_id])
|
|
69
|
+
payment = charge_payment!(ctx[:payment_token], cart.total_cents)
|
|
70
|
+
|
|
71
|
+
order = persist_order!(cart, payment, ctx)
|
|
72
|
+
|
|
73
|
+
Interceptors::Result.ok(order, meta: { order_id: order.id, payment_id: payment.id })
|
|
74
|
+
rescue PaymentGateway::Error => e
|
|
75
|
+
Interceptors::Result.err(
|
|
76
|
+
Interceptors::AppError.new(e.message, code: "payment_failed", http_status: 422, details: { gateway: e.code })
|
|
77
|
+
)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def guard_policy!(actor, cart_id)
|
|
81
|
+
allowed = actor&.can?(:checkout, cart_id)
|
|
82
|
+
raise Interceptors::AuthError.new unless allowed
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def charge_payment!(token, amount_cents)
|
|
86
|
+
PaymentGateway.charge!(token: token, amount_cents: amount_cents)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def persist_order!(cart, payment, ctx)
|
|
90
|
+
Order.create!(
|
|
91
|
+
user: ctx[:actor].user,
|
|
92
|
+
total_cents: cart.total_cents,
|
|
93
|
+
payment_reference: payment.id,
|
|
94
|
+
shipping_address: ctx[:shipping_address]
|
|
95
|
+
).tap do |order|
|
|
96
|
+
cart.line_items.each do |line_item|
|
|
97
|
+
order.order_lines.create!(sku: line_item.sku, qty: line_item.quantity, price_cents: line_item.price_cents)
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
result = CheckoutOrder.call(
|
|
104
|
+
cart_id: params[:cart_id],
|
|
105
|
+
payment_token: params[:payment_token],
|
|
106
|
+
shipping_address: params[:shipping_address],
|
|
107
|
+
idempotency_key: request.headers["Idempotency-Key"],
|
|
108
|
+
actor: Current.session
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
if result.ok?
|
|
112
|
+
render json: { order_id: result.value.id }, status: :created
|
|
113
|
+
else
|
|
114
|
+
err = result.error
|
|
115
|
+
render json: { error: err.code, message: err.message, details: err.details }, status: err.http_status
|
|
116
|
+
end
|
|
117
|
+
```
|
|
24
118
|
|
|
25
|
-
|
|
119
|
+
Instrument use cases with ActiveSupport:
|
|
26
120
|
|
|
27
|
-
|
|
121
|
+
```ruby
|
|
122
|
+
ActiveSupport::Notifications.subscribe("use_case.finish") do |_name, _start, _finish, _id, payload|
|
|
123
|
+
Rails.logger.info("[UseCase] #{payload[:name]} ok=#{payload[:ok]}")
|
|
124
|
+
end
|
|
125
|
+
```
|
|
28
126
|
|
|
127
|
+
For Rails controllers, include the responder helper:
|
|
29
128
|
|
|
30
129
|
```ruby
|
|
130
|
+
class UsersController < ApplicationController
|
|
131
|
+
include Interceptors::Rails::UseCaseResponder
|
|
31
132
|
|
|
32
|
-
|
|
133
|
+
def create
|
|
134
|
+
respond_with_use_case(CreateUser.call(user_params))
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
```
|
|
33
138
|
|
|
34
139
|
## Development
|
|
35
140
|
|
|
36
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then
|
|
37
|
-
|
|
38
|
-
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).
|
|
39
|
-
|
|
40
|
-
## Contributing
|
|
41
|
-
|
|
42
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/laertispappas/interceptors. 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.
|
|
141
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then run `bundle exec rspec` to run the tests.
|
|
43
142
|
|
|
44
143
|
## License
|
|
45
144
|
|
|
46
|
-
The gem is available as open source under the terms of the [MIT License](
|
|
47
|
-
|
|
48
|
-
## Code of Conduct
|
|
49
|
-
|
|
50
|
-
Everyone interacting in the Interceptors project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/interceptors/blob/master/CODE_OF_CONDUCT.md).
|
|
51
|
-
|
|
52
|
-
## Acknowledgments
|
|
145
|
+
The gem is available as open source under the terms of the [MIT License](LICENSE).
|
data/Rakefile
CHANGED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Interceptors
|
|
4
|
+
class AppError < StandardError
|
|
5
|
+
attr_reader :code, :details, :http_status
|
|
6
|
+
|
|
7
|
+
def initialize(message = "Application error", code: "app_error", http_status: 400, details: {})
|
|
8
|
+
super(message)
|
|
9
|
+
@code = code
|
|
10
|
+
@http_status = http_status
|
|
11
|
+
@details = details || {}
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def to_h
|
|
15
|
+
{
|
|
16
|
+
message: message,
|
|
17
|
+
code: code,
|
|
18
|
+
http_status: http_status,
|
|
19
|
+
details: details
|
|
20
|
+
}
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Interceptors
|
|
4
|
+
class AuthError < AppError
|
|
5
|
+
def initialize(message: "Unauthorized", code: "unauthorized", http_status: 401, details: {})
|
|
6
|
+
super(message, code: code, http_status: http_status, details: details)
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "thread"
|
|
4
|
+
|
|
5
|
+
module Interceptors
|
|
6
|
+
class IdempotencyInterceptor < Interceptor
|
|
7
|
+
class MemoryStore
|
|
8
|
+
def initialize
|
|
9
|
+
@data = {}
|
|
10
|
+
@mutex = Mutex.new
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def read(key)
|
|
14
|
+
@mutex.synchronize { @data[key] }
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def write(key, value, ttl: nil)
|
|
18
|
+
@mutex.synchronize { @data[key] = value }
|
|
19
|
+
value
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def delete(key)
|
|
23
|
+
@mutex.synchronize { @data.delete(key) }
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def clear
|
|
27
|
+
@mutex.synchronize { @data.clear }
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
DEFAULT_STORE = MemoryStore.new
|
|
32
|
+
|
|
33
|
+
def initialize(key_proc:, ttl: 300, store: DEFAULT_STORE)
|
|
34
|
+
raise ArgumentError, "key_proc must be callable" unless key_proc.respond_to?(:call)
|
|
35
|
+
|
|
36
|
+
@key_proc = key_proc
|
|
37
|
+
@ttl = ttl.to_i
|
|
38
|
+
@store = store
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def around(ctx)
|
|
42
|
+
key = safe_key(ctx)
|
|
43
|
+
return yield ctx unless key
|
|
44
|
+
|
|
45
|
+
cached = @store.read(key)
|
|
46
|
+
return cached if fresh?(cached)
|
|
47
|
+
|
|
48
|
+
result = yield ctx
|
|
49
|
+
return result unless result.is_a?(Result)
|
|
50
|
+
return result unless result.ok?
|
|
51
|
+
|
|
52
|
+
stored = result.merge_meta(stored_at: Time.now.to_i)
|
|
53
|
+
@store.write(key, stored, ttl: @ttl)
|
|
54
|
+
stored
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
private
|
|
58
|
+
|
|
59
|
+
def safe_key(ctx)
|
|
60
|
+
@key_proc.call(ctx)
|
|
61
|
+
rescue StandardError
|
|
62
|
+
nil
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def fresh?(result)
|
|
66
|
+
return false unless result.is_a?(Result)
|
|
67
|
+
return false unless @ttl.positive?
|
|
68
|
+
|
|
69
|
+
stored_at = result.meta[:stored_at]
|
|
70
|
+
return false unless stored_at
|
|
71
|
+
|
|
72
|
+
(Time.now.to_i - stored_at.to_i) < @ttl
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Interceptors
|
|
4
|
+
class LoggingInterceptor < Interceptor
|
|
5
|
+
def initialize(logger: nil)
|
|
6
|
+
@logger = logger
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def before(ctx)
|
|
10
|
+
log(:before, ctx: ctx)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def after(_ctx, result)
|
|
14
|
+
log(:after, ok: result.ok?, error: result.error&.message)
|
|
15
|
+
result
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
private
|
|
19
|
+
|
|
20
|
+
def log(stage, payload)
|
|
21
|
+
event = event_name("log")
|
|
22
|
+
Interceptors.instrument(event, payload.merge(stage: stage))
|
|
23
|
+
return unless @logger
|
|
24
|
+
|
|
25
|
+
@logger.info("[Interceptors] stage=#{stage} payload=#{payload}")
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def event_name(suffix)
|
|
29
|
+
"#{Interceptors.configuration.notification_namespace}.#{suffix}"
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Interceptors
|
|
4
|
+
class Pipeline
|
|
5
|
+
def initialize(interceptors)
|
|
6
|
+
@interceptors = Array(interceptors)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def call(ctx, &final_handler)
|
|
10
|
+
raise ArgumentError, "a final handler block is required" unless final_handler
|
|
11
|
+
|
|
12
|
+
chain = @interceptors.reverse.inject(final_handler) do |acc, interceptor|
|
|
13
|
+
proc do |inner_ctx|
|
|
14
|
+
interceptor.before(inner_ctx)
|
|
15
|
+
result = interceptor.around(inner_ctx) { |next_ctx| acc.call(next_ctx) }
|
|
16
|
+
interceptor.after(inner_ctx, result) || result
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
chain.call(ctx)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Interceptors
|
|
4
|
+
module Rails
|
|
5
|
+
module UseCaseResponder
|
|
6
|
+
private
|
|
7
|
+
|
|
8
|
+
def respond_with_use_case(result, serializer: nil, status_ok: 200, **render_options)
|
|
9
|
+
if result.ok?
|
|
10
|
+
body = serializer ? serializer.new(result.value) : result.value
|
|
11
|
+
render({ json: body, status: status_ok }.merge(render_options))
|
|
12
|
+
else
|
|
13
|
+
error = result.error
|
|
14
|
+
payload = {
|
|
15
|
+
error: error.code,
|
|
16
|
+
message: error.message,
|
|
17
|
+
details: error.details
|
|
18
|
+
}
|
|
19
|
+
render({ json: payload, status: error.http_status }.merge(render_options))
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Interceptors
|
|
4
|
+
class Result
|
|
5
|
+
attr_reader :value, :error, :meta
|
|
6
|
+
|
|
7
|
+
def initialize(value: nil, error: nil, meta: {})
|
|
8
|
+
@value = value
|
|
9
|
+
@error = error
|
|
10
|
+
@meta = meta || {}
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def ok?
|
|
14
|
+
error.nil?
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def err?
|
|
18
|
+
!ok?
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def self.ok(value = nil, meta: {})
|
|
22
|
+
new(value: value, meta: meta)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def self.err(error, meta: {})
|
|
26
|
+
raise ArgumentError, "error must be provided" if error.nil?
|
|
27
|
+
|
|
28
|
+
new(error: error, meta: meta)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def merge_meta(extra)
|
|
32
|
+
self.class.new(value: value, error: error, meta: meta.merge(extra))
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def to_h
|
|
36
|
+
{ value: value, error: error, meta: meta }
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Interceptors
|
|
4
|
+
class RetryInterceptor < Interceptor
|
|
5
|
+
def initialize(tries: 3, on: [StandardError], base_delay: 0.05, max_delay: 0.5)
|
|
6
|
+
raise ArgumentError, "tries must be >= 1" unless tries.to_i >= 1
|
|
7
|
+
|
|
8
|
+
@tries = tries.to_i
|
|
9
|
+
@exceptions = Array(on)
|
|
10
|
+
@base_delay = base_delay.to_f
|
|
11
|
+
@max_delay = max_delay.to_f
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def around(ctx)
|
|
15
|
+
attempt = 0
|
|
16
|
+
|
|
17
|
+
begin
|
|
18
|
+
attempt += 1
|
|
19
|
+
yield ctx
|
|
20
|
+
rescue => e
|
|
21
|
+
raise unless @exceptions.any? { |klass| e.is_a?(klass) }
|
|
22
|
+
|
|
23
|
+
return Result.err(e) if attempt >= @tries
|
|
24
|
+
|
|
25
|
+
Kernel.sleep(delay_for_attempt(attempt))
|
|
26
|
+
retry
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
def delay_for_attempt(attempt)
|
|
33
|
+
[@base_delay * (2**(attempt - 1)), @max_delay].min
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "timeout"
|
|
4
|
+
|
|
5
|
+
module Interceptors
|
|
6
|
+
class TimeoutInterceptor < Interceptor
|
|
7
|
+
def initialize(seconds:)
|
|
8
|
+
raise ArgumentError, "seconds must be positive" unless seconds.to_f.positive?
|
|
9
|
+
|
|
10
|
+
@seconds = seconds.to_f
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def around(ctx)
|
|
14
|
+
Timeout.timeout(@seconds) { yield ctx }
|
|
15
|
+
rescue Timeout::Error
|
|
16
|
+
Result.err(AppError.new("Timeout", code: "timeout", http_status: 504))
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Interceptors
|
|
4
|
+
class TransactionInterceptor < Interceptor
|
|
5
|
+
def initialize(adapter: default_adapter)
|
|
6
|
+
@adapter = adapter
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def around(ctx)
|
|
10
|
+
return yield ctx unless @adapter
|
|
11
|
+
|
|
12
|
+
@adapter.call { yield ctx }
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
private
|
|
16
|
+
|
|
17
|
+
def default_adapter
|
|
18
|
+
return nil unless defined?(ActiveRecord::Base)
|
|
19
|
+
return nil unless ActiveRecord::Base.respond_to?(:connection)
|
|
20
|
+
|
|
21
|
+
lambda do |&block|
|
|
22
|
+
ActiveRecord::Base.transaction(&block)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Interceptors
|
|
4
|
+
class UseCase
|
|
5
|
+
class << self
|
|
6
|
+
def inherited(subclass)
|
|
7
|
+
super
|
|
8
|
+
subclass.instance_variable_set(:@interceptors, interceptors.dup)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def interceptors
|
|
12
|
+
@interceptors ||= []
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def use(interceptor)
|
|
16
|
+
interceptors << interceptor
|
|
17
|
+
self
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def call(input = {}, **kwargs)
|
|
21
|
+
new.call(input, **kwargs)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def call(input = {}, **kwargs)
|
|
26
|
+
ctx = build_context(input, **kwargs)
|
|
27
|
+
|
|
28
|
+
instrument(event_name("start"), name: self.class.name, ctx: ctx)
|
|
29
|
+
|
|
30
|
+
result = pipeline.call(ctx) { |context| normalize_result(execute(context)) }
|
|
31
|
+
result = normalize_result(result)
|
|
32
|
+
|
|
33
|
+
instrument(event_name("finish"),
|
|
34
|
+
name: self.class.name,
|
|
35
|
+
ctx: ctx,
|
|
36
|
+
ok: result.ok?,
|
|
37
|
+
error: result.error&.message)
|
|
38
|
+
|
|
39
|
+
result
|
|
40
|
+
rescue AppError => e
|
|
41
|
+
instrument(event_name("error"),
|
|
42
|
+
name: self.class.name,
|
|
43
|
+
ctx: ctx,
|
|
44
|
+
code: e.code,
|
|
45
|
+
message: e.message)
|
|
46
|
+
|
|
47
|
+
Result.err(e, meta: base_meta)
|
|
48
|
+
rescue StandardError => e
|
|
49
|
+
instrument(event_name("error"),
|
|
50
|
+
name: self.class.name,
|
|
51
|
+
ctx: ctx,
|
|
52
|
+
code: "unhandled_exception",
|
|
53
|
+
message: e.message,
|
|
54
|
+
error_class: e.class.name)
|
|
55
|
+
|
|
56
|
+
err = AppError.new("Unhandled exception",
|
|
57
|
+
code: "unhandled_exception",
|
|
58
|
+
http_status: 500,
|
|
59
|
+
details: { cause: e.class.name })
|
|
60
|
+
Result.err(err, meta: base_meta.merge(error_class: e.class.name))
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
private
|
|
64
|
+
|
|
65
|
+
def execute(_ctx)
|
|
66
|
+
raise NotImplementedError, "#{self.class} must implement #execute"
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def normalize_result(result)
|
|
70
|
+
case result
|
|
71
|
+
when Result
|
|
72
|
+
result
|
|
73
|
+
when nil
|
|
74
|
+
Result.ok
|
|
75
|
+
else
|
|
76
|
+
Result.ok(result)
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def base_meta
|
|
81
|
+
{ use_case: self.class.name }
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def build_context(input, **kwargs)
|
|
85
|
+
ctx = default_context.merge(normalize_input(input))
|
|
86
|
+
ctx.merge!(kwargs) unless kwargs.empty?
|
|
87
|
+
ctx.with_indifferent_access
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def normalize_input(input)
|
|
91
|
+
return input if input.is_a?(Hash)
|
|
92
|
+
return input.to_h if input.respond_to?(:to_h)
|
|
93
|
+
|
|
94
|
+
raise ArgumentError, "use case input must be a Hash or respond to #to_h (got #{input.class})"
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def default_context
|
|
98
|
+
{}
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def pipeline
|
|
102
|
+
Pipeline.new(self.class.interceptors)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def notification_namespace
|
|
106
|
+
Interceptors.configuration.notification_namespace
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def instrument(event_name, payload, &block)
|
|
110
|
+
Interceptors.instrument(event_name, payload, &block)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def event_name(suffix)
|
|
114
|
+
"#{notification_namespace}.#{suffix}"
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Interceptors
|
|
4
|
+
class ValidationError < AppError
|
|
5
|
+
def initialize(details = nil, message: "Validation failed", code: "validation_failed", http_status: 422, **keywords)
|
|
6
|
+
payload = if details.nil?
|
|
7
|
+
{}
|
|
8
|
+
elsif details.respond_to?(:to_hash)
|
|
9
|
+
details.to_hash
|
|
10
|
+
else
|
|
11
|
+
{ base: Array(details) }
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
payload.merge!(keywords) unless keywords.empty?
|
|
15
|
+
|
|
16
|
+
super(message, code: code, http_status: http_status, details: payload)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Interceptors
|
|
4
|
+
class ValidationInterceptor < Interceptor
|
|
5
|
+
def initialize(&block)
|
|
6
|
+
raise ArgumentError, "validation block is required" unless block
|
|
7
|
+
|
|
8
|
+
@validator = block
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def before(ctx)
|
|
12
|
+
errors = normalize_errors(@validator.call(ctx))
|
|
13
|
+
raise ValidationError.new(errors) if errors.any?
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
def normalize_errors(value)
|
|
19
|
+
return {} if value.nil?
|
|
20
|
+
return value if value.is_a?(Hash)
|
|
21
|
+
return value.to_hash if value.respond_to?(:to_hash)
|
|
22
|
+
return value.to_h if value.respond_to?(:to_h)
|
|
23
|
+
|
|
24
|
+
{ base: Array(value) }
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
data/lib/interceptors/version.rb
CHANGED
data/lib/interceptors.rb
CHANGED
|
@@ -1,14 +1,58 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "
|
|
4
|
-
require "
|
|
5
|
-
require "
|
|
6
|
-
require "
|
|
7
|
-
require "interceptors/base"
|
|
8
|
-
require "interceptors/context"
|
|
3
|
+
require "zeitwerk"
|
|
4
|
+
require "active_support/notifications"
|
|
5
|
+
require "active_support/core_ext/hash/indifferent_access"
|
|
6
|
+
require "active_support/isolated_execution_state"
|
|
9
7
|
|
|
10
8
|
module Interceptors
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
class << self
|
|
10
|
+
def loader
|
|
11
|
+
@loader ||= Zeitwerk::Loader.for_gem.tap do |loader|
|
|
12
|
+
loader.inflector.inflect("version" => "VERSION")
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def configure
|
|
17
|
+
yield configuration
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def configuration
|
|
21
|
+
@configuration ||= Configuration.new
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def instrument(event_name, payload)
|
|
25
|
+
if block_given?
|
|
26
|
+
ActiveSupport::Notifications.instrument(event_name, payload) { yield }
|
|
27
|
+
else
|
|
28
|
+
ActiveSupport::Notifications.instrument(event_name, payload)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def log_instrumentation_failure(exception, event_name, payload)
|
|
35
|
+
message = "[Interceptors] instrumentation failure for #{event_name}: #{exception.class} - #{exception.message}"
|
|
36
|
+
logger = configuration.logger
|
|
37
|
+
if logger&.respond_to?(:error)
|
|
38
|
+
logger.error(message)
|
|
39
|
+
else
|
|
40
|
+
Kernel.warn(message)
|
|
41
|
+
end
|
|
42
|
+
if logger&.respond_to?(:debug)
|
|
43
|
+
logger.debug("payload=#{payload.inspect}")
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
class Configuration
|
|
49
|
+
attr_accessor :notification_namespace, :logger
|
|
50
|
+
|
|
51
|
+
def initialize
|
|
52
|
+
@notification_namespace = "use_case"
|
|
53
|
+
@logger = nil
|
|
54
|
+
end
|
|
13
55
|
end
|
|
14
56
|
end
|
|
57
|
+
|
|
58
|
+
Interceptors.loader.setup
|
metadata
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: interceptors
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 1.0.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
|
-
- Laerti
|
|
8
|
-
autorequire:
|
|
9
|
-
bindir:
|
|
7
|
+
- Laerti papa
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2025-10-15 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
|
-
name:
|
|
14
|
+
name: activesupport
|
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
|
16
16
|
requirements:
|
|
17
17
|
- - ">="
|
|
18
18
|
- !ruby/object:Gem::Version
|
|
19
19
|
version: '0'
|
|
20
|
-
type: :
|
|
20
|
+
type: :runtime
|
|
21
21
|
prerelease: false
|
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
23
|
requirements:
|
|
@@ -25,13 +25,13 @@ dependencies:
|
|
|
25
25
|
- !ruby/object:Gem::Version
|
|
26
26
|
version: '0'
|
|
27
27
|
- !ruby/object:Gem::Dependency
|
|
28
|
-
name:
|
|
28
|
+
name: zeitwerk
|
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
|
30
30
|
requirements:
|
|
31
31
|
- - ">="
|
|
32
32
|
- !ruby/object:Gem::Version
|
|
33
33
|
version: '0'
|
|
34
|
-
type: :
|
|
34
|
+
type: :runtime
|
|
35
35
|
prerelease: false
|
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
|
37
37
|
requirements:
|
|
@@ -52,41 +52,43 @@ dependencies:
|
|
|
52
52
|
- - ">="
|
|
53
53
|
- !ruby/object:Gem::Version
|
|
54
54
|
version: '0'
|
|
55
|
-
description:
|
|
56
|
-
|
|
55
|
+
description: Interceptors provides a production-ready pattern for building use case
|
|
56
|
+
objects with consistent results, interceptor pipelines, and instrumentation.
|
|
57
57
|
email:
|
|
58
|
-
-
|
|
58
|
+
- laertis.pappas@gmail.com
|
|
59
59
|
executables: []
|
|
60
60
|
extensions: []
|
|
61
61
|
extra_rdoc_files: []
|
|
62
62
|
files:
|
|
63
|
-
-
|
|
64
|
-
- ".rspec"
|
|
65
|
-
- ".travis.yml"
|
|
66
|
-
- CODE_OF_CONDUCT.md
|
|
63
|
+
- CHANGELOG.md
|
|
67
64
|
- Gemfile
|
|
68
|
-
-
|
|
69
|
-
- LICENSE.txt
|
|
65
|
+
- LICENSE
|
|
70
66
|
- README.md
|
|
71
67
|
- Rakefile
|
|
72
|
-
- bin/console
|
|
73
|
-
- bin/setup
|
|
74
|
-
- interceptors.gemspec
|
|
75
68
|
- lib/interceptors.rb
|
|
76
|
-
- lib/interceptors/
|
|
77
|
-
- lib/interceptors/
|
|
78
|
-
- lib/interceptors/
|
|
79
|
-
- lib/interceptors/
|
|
80
|
-
- lib/interceptors/
|
|
81
|
-
- lib/interceptors/
|
|
82
|
-
- lib/interceptors/
|
|
69
|
+
- lib/interceptors/app_error.rb
|
|
70
|
+
- lib/interceptors/auth_error.rb
|
|
71
|
+
- lib/interceptors/idempotency_interceptor.rb
|
|
72
|
+
- lib/interceptors/interceptor.rb
|
|
73
|
+
- lib/interceptors/logging_interceptor.rb
|
|
74
|
+
- lib/interceptors/pipeline.rb
|
|
75
|
+
- lib/interceptors/rails/use_case_responder.rb
|
|
76
|
+
- lib/interceptors/result.rb
|
|
77
|
+
- lib/interceptors/retry_interceptor.rb
|
|
78
|
+
- lib/interceptors/timeout_interceptor.rb
|
|
79
|
+
- lib/interceptors/transaction_interceptor.rb
|
|
80
|
+
- lib/interceptors/use_case.rb
|
|
81
|
+
- lib/interceptors/validation_error.rb
|
|
82
|
+
- lib/interceptors/validation_interceptor.rb
|
|
83
83
|
- lib/interceptors/version.rb
|
|
84
|
-
homepage: https://
|
|
84
|
+
homepage: https://github.com/laertispappas/interceptors
|
|
85
85
|
licenses:
|
|
86
86
|
- MIT
|
|
87
87
|
metadata:
|
|
88
|
-
|
|
89
|
-
|
|
88
|
+
homepage_uri: https://github.com/laertispappas/interceptors
|
|
89
|
+
source_code_uri: https://github.com/laertispappas/interceptors
|
|
90
|
+
changelog_uri: https://github.com/laertispappas/interceptors/CHANGELOG.md
|
|
91
|
+
post_install_message:
|
|
90
92
|
rdoc_options: []
|
|
91
93
|
require_paths:
|
|
92
94
|
- lib
|
|
@@ -94,15 +96,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
94
96
|
requirements:
|
|
95
97
|
- - ">="
|
|
96
98
|
- !ruby/object:Gem::Version
|
|
97
|
-
version: '0'
|
|
99
|
+
version: '3.0'
|
|
98
100
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
99
101
|
requirements:
|
|
100
102
|
- - ">="
|
|
101
103
|
- !ruby/object:Gem::Version
|
|
102
104
|
version: '0'
|
|
103
105
|
requirements: []
|
|
104
|
-
rubygems_version: 3.
|
|
105
|
-
signing_key:
|
|
106
|
+
rubygems_version: 3.5.23
|
|
107
|
+
signing_key:
|
|
106
108
|
specification_version: 4
|
|
107
|
-
summary:
|
|
109
|
+
summary: Interceptor-driven use case toolkit for Ruby and Rails applications.
|
|
108
110
|
test_files: []
|
data/.gitignore
DELETED
data/.rspec
DELETED
data/.travis.yml
DELETED
data/CODE_OF_CONDUCT.md
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
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 laerti.papa@xing.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.lock
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
PATH
|
|
2
|
-
remote: .
|
|
3
|
-
specs:
|
|
4
|
-
interceptors (0.1.1)
|
|
5
|
-
|
|
6
|
-
GEM
|
|
7
|
-
remote: https://rubygems.org/
|
|
8
|
-
specs:
|
|
9
|
-
diff-lcs (1.3)
|
|
10
|
-
rake (12.3.2)
|
|
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.3)
|
|
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
|
|
30
|
-
interceptors!
|
|
31
|
-
rake
|
|
32
|
-
rspec
|
|
33
|
-
|
|
34
|
-
BUNDLED WITH
|
|
35
|
-
1.17.3
|
data/bin/console
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env ruby
|
|
2
|
-
|
|
3
|
-
require "bundler/setup"
|
|
4
|
-
require "interceptors"
|
|
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
DELETED
data/interceptors.gemspec
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
lib = File.expand_path("../lib", __FILE__)
|
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
-
require "interceptors/version"
|
|
5
|
-
|
|
6
|
-
Gem::Specification.new do |spec|
|
|
7
|
-
spec.name = "interceptors"
|
|
8
|
-
spec.version = Interceptors::VERSION
|
|
9
|
-
spec.authors = ["Laerti Papa"]
|
|
10
|
-
spec.email = ["laerti.papa@xing.com"]
|
|
11
|
-
|
|
12
|
-
spec.summary = %q{Pedestal like interceptors service objects in Ruby}
|
|
13
|
-
spec.description = %q{Pedestal like interceptors service objects in Ruby, aka pipe and filter architecture}
|
|
14
|
-
spec.homepage = "https://www.github.com/laertispappas/interceptors"
|
|
15
|
-
spec.license = "MIT"
|
|
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"
|
|
34
|
-
spec.add_development_dependency "rake"
|
|
35
|
-
spec.add_development_dependency "rspec"
|
|
36
|
-
end
|
data/lib/interceptors/base.rb
DELETED
data/lib/interceptors/context.rb
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
module Interceptors
|
|
2
|
-
module DS
|
|
3
|
-
module Errors
|
|
4
|
-
Error = Class.new(StandardError)
|
|
5
|
-
class NullValueNotAllowedError < Error; end
|
|
6
|
-
class NoSuchElementException < Error; end
|
|
7
|
-
|
|
8
|
-
private
|
|
9
|
-
|
|
10
|
-
def assert!(item)
|
|
11
|
-
raise NullValueNotAllowedError, "A value must not be null in the dequeue" if item.nil?
|
|
12
|
-
end
|
|
13
|
-
end
|
|
14
|
-
end
|
|
15
|
-
end
|
data/lib/interceptors/ds/node.rb
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
require_relative "./errors"
|
|
2
|
-
|
|
3
|
-
module Interceptors
|
|
4
|
-
module DS
|
|
5
|
-
class Stack
|
|
6
|
-
include DS::Errors
|
|
7
|
-
|
|
8
|
-
def initialize
|
|
9
|
-
@head = nil
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
def push(item)
|
|
13
|
-
assert!(item)
|
|
14
|
-
old_head = @head
|
|
15
|
-
@head = Node.new(item)
|
|
16
|
-
@head.next = old_head
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
def pop
|
|
20
|
-
raise NoSuchElementException if empty?
|
|
21
|
-
|
|
22
|
-
@head.item.tap { @head = @head.next }
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
def empty?
|
|
26
|
-
@head.nil?
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
end
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
module Interceptors
|
|
2
|
-
class Executor
|
|
3
|
-
attr_reader :middleware, :context
|
|
4
|
-
|
|
5
|
-
def initialize(context = Context.new)
|
|
6
|
-
@middleware = Middleware.new
|
|
7
|
-
@context = context
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
def register(interceptor)
|
|
11
|
-
middleware.enqueue(interceptor)
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
def call
|
|
15
|
-
while (interceptor, step = fetch_next)
|
|
16
|
-
with_exception_handling(interceptor) do
|
|
17
|
-
interceptor.public_send(step, context)
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
context
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
private
|
|
25
|
-
|
|
26
|
-
def fetch_next
|
|
27
|
-
if context[:error]
|
|
28
|
-
[middleware.pop, :on_error] unless middleware.on_leave.empty?
|
|
29
|
-
elsif !middleware.on_enter.empty?
|
|
30
|
-
interceptor = middleware.dequeue
|
|
31
|
-
middleware.push(interceptor)
|
|
32
|
-
[interceptor, :on_enter]
|
|
33
|
-
elsif !middleware.on_leave.empty?
|
|
34
|
-
[middleware.pop, :on_leave]
|
|
35
|
-
end
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
def with_exception_handling(interceptor)
|
|
39
|
-
yield
|
|
40
|
-
rescue StandardError => e
|
|
41
|
-
context[:error] = e
|
|
42
|
-
context[:error_raised_at] = interceptor.class.name
|
|
43
|
-
end
|
|
44
|
-
end
|
|
45
|
-
end
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
module Interceptors
|
|
2
|
-
class Middleware
|
|
3
|
-
attr_reader :on_enter, :on_leave
|
|
4
|
-
|
|
5
|
-
def initialize
|
|
6
|
-
@on_enter = Queue.new
|
|
7
|
-
@on_leave = DS::Stack.new
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
# Queue operations
|
|
11
|
-
def enqueue(element)
|
|
12
|
-
on_enter.push(element)
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
def dequeue
|
|
16
|
-
on_enter.pop
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
# Stack operations
|
|
20
|
-
def push(element)
|
|
21
|
-
on_leave.push(element)
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
def pop
|
|
25
|
-
on_leave.pop
|
|
26
|
-
end
|
|
27
|
-
end
|
|
28
|
-
end
|