puma_after_reply 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bee192bb4c46f6a208857d12dcf9e808032394a8562a0792e0a62d1660bf8215
4
+ data.tar.gz: 3a6a13aede612171335bdc8906190a3253370f36bf38644f31b23b4ffeee8ca3
5
+ SHA512:
6
+ metadata.gz: 6114777e7788216154be73e94cc37234a11951bb453fe86aa8bedde5d67cc491cf91cb276e9ddc9d4f30a686d0abdaf0c5cd8a58c6ba539cf25b1cfc73155a39
7
+ data.tar.gz: b0d397d25d636e0426660508d54205f898d887b4fec2b7c5cd51436ab58a584b593d949dcabd9ebf9dc724ffdae64c30b4d2fb31840db562aea519c8074ab8d7
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format progress
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,16 @@
1
+ inherit_gem:
2
+ armitage-rubocop:
3
+ - lib/rubocop.general.yml
4
+ - lib/rubocop.rake.yml
5
+ - lib/rubocop.rspec.yml
6
+
7
+ AllCops:
8
+ TargetRubyVersion: 3.1
9
+ NewCops: enable
10
+ Include:
11
+ - lib/**/*.rb
12
+ - spec/**/*.rb
13
+ - Gemfile
14
+ - Rakefile
15
+ - puma_after_reply.gemspec
16
+ - bin/console
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.3.3
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-06-30
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at iamdaiver@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Rustam Ibragimov
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,216 @@
1
+ # puma_after_reply ![build](https://github.com/0exp/puma_after_reply/actions/workflows/build.yml/badge.svg??branch=master)
2
+
3
+ Puma's `"rack.after_reply"` integration for your Rack-applications. Provides #call-able reply
4
+ abstraction and configurable non-threaded/threaded invocation flow with before/on_error/after hooks for each added reply.
5
+
6
+ ---
7
+
8
+ ## Table of Contents
9
+
10
+ - [Requirements](#requirements)
11
+ - [Installation](#installation)
12
+ - [Usage](#usage)
13
+ - [Algorithm](#algorithm)
14
+ - [Configuration](#configuration)
15
+ - [Adding replies](#adding-replies-add_replycond_reply)
16
+ - [Some debugging methods](#some-debigging-methods)
17
+ - [Test environments](#test-environments-and-other-rack-apps)
18
+ - [Contributing](#contributing)
19
+ - [License](#license)
20
+ - [Authors](#authors)
21
+
22
+ ---
23
+
24
+ ### Requirements
25
+
26
+ <sup>\[[back to top](#table-of-contents)\]</sup>
27
+
28
+ - `concurrent-ruby` (`~> 1.3`)
29
+
30
+ ---
31
+
32
+ ### Installation
33
+
34
+ <sup>\[[back to top](#table-of-contents)\]</sup>
35
+
36
+
37
+ ```ruby
38
+ gem 'puma_after_reply'
39
+ ```
40
+
41
+ ```shell
42
+ bundle install
43
+ # --- or ---
44
+ gem install puma_after_reply
45
+ ```
46
+
47
+ ```ruby
48
+ require 'puma_after_reply'
49
+ ```
50
+
51
+ ---
52
+
53
+ ### Usage
54
+
55
+ <sup>\[[back to top](#table-of-contents)\]</sup>
56
+
57
+ - [Algorithm](#algorithm)
58
+ - [Configuration](#configuration)
59
+ - [Adding replies](#adding-replies-add_replycond_reply)
60
+ - [Some debugging methods](#some-debigging-methods)
61
+ - [Test environments](#test-environments-and-other-rack-apps)
62
+
63
+ ---
64
+
65
+ #### Algorithm
66
+
67
+ - every Puma's worker gets own reply collector;
68
+ - during the Puma's request your logic adds replies to the worker's reply collector;
69
+ - after processing the request, Puma's worker returns a response to the browser;
70
+ - then Puma's worker launches accumulated replies:
71
+ - threaded replies are launched in separated threads;
72
+ - non-threaded replies are launched sequentially;
73
+ - after processing all replies, the worker's reply collector is cleared;
74
+
75
+ Each separated reply is launched according to the following invocation flow:
76
+ - **before_reply** hook (`config.before_reply`);
77
+ - reply invocation (`reply.call`);
78
+ - if `reply.call` failed with an error:
79
+ - **log_error** hook (`config.log_error`);
80
+ - **on_error** hook (`config.on_error`);
81
+ - **raise_on_error** fail check (`config.fail_on_error`)
82
+ - (ensure): **after_reply** hook (`config.after_reply`);
83
+
84
+ ---
85
+
86
+ #### Configuration
87
+
88
+ <sup>\[[back to top](#usage)\]</sup>
89
+
90
+ ```ruby
91
+ PumaAfterReply.configure do |config|
92
+ # default values:
93
+ config.fail_on_error = false # expects: <Boolean>
94
+ config.log_error = nil # expects: <nil,#call,Proc> (receives: error object)
95
+ config.on_error = nil # expects: <nil,#call,Proc> (receives: error object)
96
+ config.before_reply = nil # expects: <nil,#call,Proc> (receives: nothing)
97
+ config.after_reply = nil # expects: <nil,#call,Proc> (receives: nothing)
98
+ config.run_anyway = false # expects: <Boolean>
99
+ config.thread_pool_size = 10 # expects: <Integer>
100
+ end
101
+
102
+ # get configs as a hash:
103
+ PumaAfterReply.cofnig.to_h
104
+
105
+ # get configs directly:
106
+ PumaAfterRepy.config.fail_on_error # and etc;
107
+ ```
108
+
109
+ ```ruby
110
+ # (IMPORTANT): register the middleware (Rails example)
111
+ Rails.configuration.middleware.use(PumaAfterReply::Middleware)
112
+ ```
113
+
114
+ ---
115
+
116
+ #### Adding replies (`add_reply`/`cond_reply`)
117
+
118
+ <sup>\[[back to top](#usage)\]</sup>
119
+
120
+ - non-threaded way (this reply will be processed sequentially):
121
+
122
+ ```ruby
123
+ # non-threaded way:
124
+ PumaAfterReply.add_reply { your_code }
125
+ ```
126
+
127
+ - threaded-way (this reply will be processed in a separated thread):
128
+
129
+ ```ruby
130
+ # threaded way:
131
+ PumaAfterReply.add_reply(threaded: true) { your_code }
132
+ ```
133
+
134
+ - conditional reply adding:
135
+ - `reply(condition, threaded: false, &reply)` (`threaded: false` by default);
136
+ - when condition is `true` - your reply will be pushed to the reply queue;
137
+ - when condition is `false` - your reply will be processed immediately;
138
+ - condition can be represented as callable object (`#call`/`Proc`);
139
+
140
+ ```ruby
141
+ # with a boolean value:
142
+ PumaAfterReply.cond_reply(!Rails.env.test?) { your_code }
143
+ ```
144
+
145
+ ```ruby
146
+ # with a callabale value:
147
+ is_puma_request = proc { check_that_we_are_inside_a_request }
148
+ PumaAfterReply.cond_reply(is_puma_request) { your_code }
149
+ ```
150
+
151
+ ```ruby
152
+ # add threaded reply:
153
+ PumaAfterReply.cond_reply(some_condition, threaded: true) { your_code }
154
+ ```
155
+
156
+ ---
157
+
158
+ #### Some debigging methods
159
+
160
+ <sup>\[[back to top](#usage)\]</sup>
161
+
162
+ ```ruby
163
+ # the count of the added replies:
164
+ PumaAfterReply.count # or .size
165
+ ```
166
+
167
+ ```ruby
168
+ # replies collections:
169
+ PumaAfterReply.replies # all added replies
170
+ PumaAfterReply.inline_replies # all added non-threaded replies
171
+ PumaAfterReply.threaded_replies # all added threaded replies
172
+ ```
173
+
174
+ ```ruby
175
+ # manual replies running:
176
+ PumaAfterReply.call # or .run
177
+ ```
178
+
179
+ ```ruby
180
+ # reset configs to default values:
181
+ PumaAfterReply.config.__reset!
182
+ ```
183
+
184
+ ---
185
+
186
+ #### Test environments (and other Rack apps)
187
+
188
+ <sup>\[[back to top](#usage)\]</sup>
189
+
190
+ In some cases and Rack applications you can have no `"rack.after_reply"` key in your Rack env
191
+ (request environments in your tests, for example). For this cases you can use `config.run_anyway = true`:
192
+ on each of your rack request all accumulated replies will be processed and cleared in the same way as for Puma.
193
+
194
+ ---
195
+
196
+ ## Contributing
197
+
198
+ <sup>\[[back to top](#table-of-contents)\]</sup>
199
+
200
+ - Fork it ( https://github.com/0exp/puma_after_reply )
201
+ - Create your feature branch (`git checkout -b feature/my-new-feature`)
202
+ - Commit your changes (`git commit -am '[feature_context] Add some feature'`)
203
+ - Push to the branch (`git push origin feature/my-new-feature`)
204
+ - Create new Pull Request
205
+
206
+ ## License
207
+
208
+ <sup>\[[back to top](#table-of-contents)\]</sup>
209
+
210
+ Released under MIT License.
211
+
212
+ ## Authors
213
+
214
+ <sup>\[[back to top](#table-of-contents)\]</sup>
215
+
216
+ [Rustam Ibragimov](https://github.com/0exp)
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+ require 'rubocop'
6
+ require 'rubocop/rake_task'
7
+ require 'rubocop-performance'
8
+ require 'rubocop-rspec'
9
+ require 'rubocop-rake'
10
+
11
+ RuboCop::RakeTask.new(:rubocop) do |t|
12
+ config_path = File.expand_path(File.join('.rubocop.yml'), __dir__)
13
+ t.options = ['--config', config_path]
14
+ t.requires << 'rubocop-rspec'
15
+ t.requires << 'rubocop-performance'
16
+ t.requires << 'rubocop-rake'
17
+ end
18
+
19
+ RSpec::Core::RakeTask.new(:rspec)
20
+
21
+ task default: :rspec
@@ -0,0 +1,97 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api public
4
+ # @since 0.1.0
5
+ module PumaAfterReply::Config
6
+ class << self
7
+ # @return [Boolean]
8
+ #
9
+ # @api public
10
+ # @since 0.1.0
11
+ attr_accessor :fail_on_error
12
+
13
+ # @return [nil,#call,Proc]
14
+ #
15
+ # @api public
16
+ # @since 0.1.0
17
+ attr_accessor :log_error
18
+
19
+ # @return [nil,#call,Proc]
20
+ #
21
+ # @api public
22
+ # @since 0.1.0
23
+ attr_accessor :on_error
24
+
25
+ # @return [nil,#call,Proc]
26
+ #
27
+ # @api public
28
+ # @since 0.1.0
29
+ attr_accessor :before_reply
30
+
31
+ # @return [nil,#call,Proc]
32
+ #
33
+ # @api public
34
+ # @since 0.1.0
35
+ attr_accessor :after_reply
36
+
37
+ # @return [Boolean]
38
+ #
39
+ # @api public
40
+ # @since 0.1.0
41
+ attr_accessor :run_anyway
42
+
43
+ # @return [Integer]
44
+ #
45
+ # @api public
46
+ # @since 0.1.0
47
+ attr_accessor :thread_pool_size
48
+
49
+ # @param configuration [Block]
50
+ # @return [void]
51
+ #
52
+ # @api public
53
+ # @since 0.1.0
54
+ def configure(&configuration)
55
+ instance_eval(&configuration)
56
+ end
57
+
58
+ # @return [Hash<Symbol,Any>]
59
+ #
60
+ # @api public
61
+ # @since 0.1.0
62
+ # rubocop:disable Style/EndlessMethod
63
+ def to_h = {
64
+ fail_on_error:,
65
+ log_error:,
66
+ on_error:,
67
+ before_reply:,
68
+ after_reply:,
69
+ run_anyway:,
70
+ thread_pool_size:
71
+ }
72
+ # rubocop:enable Style/EndlessMethod
73
+
74
+ # @return [String]
75
+ #
76
+ # @api public
77
+ # @since 0.1.0
78
+ def to_s = to_h.inspect
79
+
80
+ # @return [void]
81
+ #
82
+ # @api public
83
+ # @since 0.1.0
84
+ def __reset!
85
+ self.fail_on_error = false
86
+ self.log_error = nil
87
+ self.on_error = nil
88
+ self.before_reply = nil
89
+ self.after_reply = nil
90
+ self.run_anyway = false
91
+ self.thread_pool_size = 10
92
+ end
93
+ end
94
+
95
+ # NOTE: set default configs
96
+ __reset!
97
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api public
4
+ # @since 0.1.0
5
+ class PumaAfterReply::Middleware
6
+ # @param app [?]
7
+ # @return [void]
8
+ #
9
+ # @api private
10
+ # @since 0.1.0
11
+ def initialize(app)
12
+ @app = app
13
+ end
14
+
15
+ # PumaAfterReply.call integration to the puma's `rack.after_reply` env variable that is used
16
+ # for the "after reply" hook inside the puma's request dispatching.
17
+ #
18
+ # @param env [?]
19
+ # @return [Array]
20
+ #
21
+ # @api private
22
+ # @since 0.1.0
23
+ def call(env)
24
+ @app.call(env).tap do
25
+ if env.key?('rack.after_reply')
26
+ env['rack.after_reply'] << proc { PumaAfterReply.call }
27
+ elsif PumaAfterReply::Config.run_anyway
28
+ PumaAfterReply.call # NOTE: may be usefull for/from-inside test environments
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.1.0
5
+ module PumaAfterReply::RepliesRunner
6
+ class << self
7
+ # @api private
8
+ # @since 0.1.0
9
+ def call
10
+ threaded_executions = execute_threaded
11
+ execute_inline
12
+ # NOTE:
13
+ # wait for all replies to be completed (an analogue of Thread#join)
14
+ # in order to keep busy the current puma worker for the duration of the reply logic
15
+ # and to prevent any memory bloat;
16
+ threaded_executions.each(&:value)
17
+ end
18
+
19
+ private
20
+
21
+ # @return [Concurrent::Array]
22
+ #
23
+ # @api private
24
+ # @since 0.1.0
25
+ def execute_threaded
26
+ Concurrent::Array.new.tap do |reply_executions|
27
+ PumaAfterReply::ReplyCollector.current.threaded__each_and_flush do |reply|
28
+ reply_executions << Concurrent::Future.execute({ executor: thread_pool }) do
29
+ call_reply(reply)
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ # @return [void]
36
+ #
37
+ # @api private
38
+ # @since 0.1.0
39
+ def execute_inline
40
+ PumaAfterReply::ReplyCollector.current.inline__each_and_flush do |reply|
41
+ call_reply(reply)
42
+ end
43
+ end
44
+
45
+ # @return [Concurrent::FixedThreadPool]
46
+ #
47
+ # @api private
48
+ # @since 0.1.0
49
+ def thread_pool
50
+ Thread.current[:puma_after_reply_runner_thread_pool] ||=
51
+ Concurrent::FixedThreadPool.new(PumaAfterReply::Config.thread_pool_size)
52
+ end
53
+
54
+ # @param reply [#call,Proc]
55
+ # @return [void]
56
+ #
57
+ # @api private
58
+ # @since 0.1.0
59
+ def call_reply(reply)
60
+ PumaAfterReply::Config.before_reply&.call
61
+ reply.call
62
+ rescue => error
63
+ # :nocov:
64
+ # NOTE: it is covered in specs but still showed as "uncovered"
65
+ PumaAfterReply::Config.log_error&.call(error)
66
+ PumaAfterReply::Config.on_error&.call(error)
67
+ # :nocov:
68
+ raise(error) if PumaAfterReply::Config.fail_on_error
69
+ ensure
70
+ PumaAfterReply::Config.after_reply&.call(reply)
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,115 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.1.0
5
+ class PumaAfterReply::ReplyCollector
6
+ class << self
7
+ # @return [PumaAfterReply::ReplyCollector]
8
+ #
9
+ # @api private
10
+ # @since 0.1.0
11
+ def current
12
+ Thread.current[:puma_after_reply_replies_collector] ||= new
13
+ end
14
+ end
15
+
16
+ # @return [Array<#call|Proc>]
17
+ #
18
+ # @api private
19
+ # @since 0.1.0
20
+ attr_reader :inline_replies
21
+
22
+ # @return [ARray<#call|Proc>]
23
+ #
24
+ # @api private
25
+ # @since 0.1.0
26
+ attr_reader :threaded_replies
27
+
28
+ # @return [void]
29
+ #
30
+ # @api private
31
+ # @since 0.1.0
32
+ def initialize
33
+ @inline_replies = []
34
+ @threaded_replies = []
35
+ end
36
+
37
+ # @param reply [#call,Proc]
38
+ # @option threaded [Boolean]
39
+ # @return [void]
40
+ #
41
+ # @api private
42
+ # @since 0.1.0
43
+ def add_reply(reply, threaded:)
44
+ threaded ? threaded_replies << reply : inline_replies << reply
45
+ end
46
+
47
+ # @return [Array<#call,Proc>]
48
+ #
49
+ # @api private
50
+ # @since 0.1.0
51
+ def replies
52
+ threaded_replies + inline_replies
53
+ end
54
+
55
+ # @return [Integer]
56
+ #
57
+ # @api private
58
+ # @since 0.1.0
59
+ def size
60
+ inline_replies.size + threaded_replies.size
61
+ end
62
+
63
+ # @return [void]
64
+ #
65
+ # @api private
66
+ # @since 0.1.0
67
+ def clear
68
+ inline_replies.clear
69
+ threaded_replies.clear
70
+ end
71
+
72
+ # @param block [Block]
73
+ # @return [void]
74
+ #
75
+ # @api private
76
+ # @since 0.1.0
77
+ def threaded__each(&block)
78
+ # :nocov:
79
+ threaded_replies.each(&block)
80
+ # :nocov:
81
+ end
82
+
83
+ # @param block [Block]
84
+ # @return [void]
85
+ #
86
+ # @api private
87
+ # @since 0.1.0
88
+ def threaded__each_and_flush(&block)
89
+ threaded_replies.each(&block)
90
+ ensure
91
+ threaded_replies.clear
92
+ end
93
+
94
+ # @param block [Block]
95
+ # @return [void]
96
+ #
97
+ # @api private
98
+ # @since 0.1.0
99
+ def inline__each(&block)
100
+ # :nocov:
101
+ inline_replies.each(&block)
102
+ # :nocov:
103
+ end
104
+
105
+ # @param block [Block]
106
+ # @return [void]
107
+ #
108
+ # @api private
109
+ # @since 0.1.0
110
+ def inline__each_and_flush(&block)
111
+ inline_replies.each(&block)
112
+ ensure
113
+ inline_replies.clear
114
+ end
115
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PumaAfterReply
4
+ # @return [String]
5
+ #
6
+ # @api public
7
+ # @since 0.1.0
8
+ VERSION = '0.1.0'
9
+ end
@@ -0,0 +1,128 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'concurrent-ruby'
4
+
5
+ # @api public
6
+ # @since 0.1.0
7
+ module PumaAfterReply
8
+ require_relative 'puma_after_reply/version'
9
+ require_relative 'puma_after_reply/config'
10
+ require_relative 'puma_after_reply/reply_collector'
11
+ require_relative 'puma_after_reply/replies_runner'
12
+ require_relative 'puma_after_reply/middleware'
13
+
14
+ class << self
15
+ # Add reply to the reply collector, without any conditions.
16
+ #
17
+ # @option threaded [Boolean]
18
+ # @param reply [Block]
19
+ # @return [void]
20
+ #
21
+ # @api public
22
+ # @since 0.1.0
23
+ def add_reply(threaded: false, &reply)
24
+ ReplyCollector.current.add_reply(reply, threaded:)
25
+ end
26
+
27
+ # Add reply to the reply collector condotionally using the boolean factor.
28
+ # If a factor is a callable object - it will be called and the result will be
29
+ # threatet as a factor.
30
+ # If a factor is a true value => your reply block will be added to the replies collector.
31
+ # If a factor is a false value => your reply block will be yielded immediatly.
32
+ #
33
+ # @param factor [Boolean,#call]
34
+ # @option threaded [Boolean]
35
+ # @param reply [Block]
36
+ # @return [void,Any]
37
+ #
38
+ # @pai public
39
+ # @since 0.1.0
40
+ def cond_reply(factor, threaded: false, &reply)
41
+ is_addable = factor.respond_to?(:call) ? factor.call : factor
42
+ is_addable ? add_reply(threaded:, &reply) : yield
43
+ end
44
+
45
+ # @param block [Block]
46
+ # @return [void]
47
+ #
48
+ # @api public
49
+ # @since 0.1.0
50
+ def configure(&block)
51
+ Config.configure(&block)
52
+ end
53
+
54
+ # @return [PumaAfterReply::Config]
55
+ #
56
+ # @api public
57
+ # @since 0.1.0
58
+ def config
59
+ PumaAfterReply::Config
60
+ end
61
+
62
+ # Return the count of currently added replies.
63
+ #
64
+ # @return [Integer]
65
+ #
66
+ # @api public
67
+ # @since 0.1.0
68
+ def count
69
+ ReplyCollector.current.size
70
+ end
71
+ alias_method :size, :count
72
+
73
+ # [DEBUGGING METHOD]
74
+ #
75
+ # @return [Array<#call|Proc>]
76
+ #
77
+ # @api private
78
+ # @since 0.1.0
79
+ def replies
80
+ ReplyCollector.current.replies
81
+ end
82
+
83
+ # [DEBUGGING METHOD]
84
+ #
85
+ # @return [Array<#call|Proc>]
86
+ #
87
+ # @api private
88
+ # @since 0.1.0
89
+ def inline_replies
90
+ ReplyCollector.current.inline_replies
91
+ end
92
+
93
+ # [DEBUGGING METHOD]
94
+ #
95
+ # @return [Array<#call|Proc>]
96
+ #
97
+ # @api private
98
+ # @since 0.1.0
99
+ def threaded_replies
100
+ ReplyCollector.current.threaded_replies
101
+ end
102
+
103
+ # [DEBUGGING METHOD]
104
+ #
105
+ # Should not be invoked directly in the business code!
106
+ # Run all currently added replies directly. Imporant: remember that reply collector will
107
+ # be flushed.
108
+ #
109
+ # @return [void]
110
+ #
111
+ # @api private
112
+ # @since 0.1.0
113
+ def call
114
+ RepliesRunner.call
115
+ end
116
+ alias_method :run, :call
117
+
118
+ # [DEBUGGING METHOD]
119
+ #
120
+ # @return [void]
121
+ #
122
+ # @api private
123
+ # @since 0.1.0
124
+ def clear
125
+ ReplyCollector.current.clear
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/puma_after_reply/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.required_ruby_version = '>= 3.1'
7
+
8
+ spec.name = 'puma_after_reply'
9
+ spec.version = PumaAfterReply::VERSION
10
+ spec.authors = ['Rustam Ibragimov']
11
+ spec.email = ['iamdaiver@gmail.com']
12
+
13
+ spec.summary =
14
+ "PumaAfterReply: Puma's \"rack.after_reply\" integration for your Rack-applications."
15
+ spec.description =
16
+ "PumaAfterReply: Puma's \"rack.after_reply\" integration for your Rack-applications. " \
17
+ "Provides #call-able reply abstraction and " \
18
+ "configurable non-threaded/threaded invocation flow " \
19
+ "before/on_error/after hooks for each added reply."
20
+
21
+ spec.homepage = 'https://github.com/0exp/puma_after_reply'
22
+ spec.license = 'MIT'
23
+
24
+ spec.metadata['homepage_uri'] = spec.homepage
25
+ spec.metadata['source_code_uri'] = "#{spec.homepage}/blob/master"
26
+ spec.metadata['changelog_uri'] = "#{spec.homepage}/blob/master/CHANGELOG.md"
27
+
28
+ spec.files = Dir.chdir(__dir__) do
29
+ `git ls-files -z`.split("\x0").reject do |f|
30
+ (File.expand_path(f) == __FILE__) ||
31
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git appveyor Gemfile])
32
+ end
33
+ end
34
+
35
+ spec.bindir = 'exe'
36
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
37
+ spec.require_paths = ['lib']
38
+
39
+ spec.add_dependency 'concurrent-ruby', '~> 1.3'
40
+
41
+ spec.add_development_dependency 'armitage-rubocop', '~> 1.59'
42
+ spec.add_development_dependency 'bundler', '>= 2'
43
+ spec.add_development_dependency 'pry', '~> 0.14'
44
+ spec.add_development_dependency 'rake', '~> 13'
45
+ spec.add_development_dependency 'rspec', '~> 3.13'
46
+ spec.add_development_dependency 'simplecov', '~> 0.22'
47
+ spec.add_development_dependency 'rack', '~> 3.1'
48
+ spec.add_development_dependency 'rack-test', '~> 2.1'
49
+ end
metadata ADDED
@@ -0,0 +1,189 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: puma_after_reply
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Rustam Ibragimov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-07-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: concurrent-ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: armitage-rubocop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.59'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.59'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.14'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.14'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '13'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '13'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.13'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.13'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.22'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.22'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rack
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '3.1'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '3.1'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rack-test
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '2.1'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '2.1'
139
+ description: 'PumaAfterReply: Puma''s "rack.after_reply" integration for your Rack-applications.
140
+ Provides #call-able reply abstraction and configurable non-threaded/threaded invocation
141
+ flow before/on_error/after hooks for each added reply.'
142
+ email:
143
+ - iamdaiver@gmail.com
144
+ executables: []
145
+ extensions: []
146
+ extra_rdoc_files: []
147
+ files:
148
+ - ".rspec"
149
+ - ".rubocop.yml"
150
+ - ".ruby-version"
151
+ - CHANGELOG.md
152
+ - CODE_OF_CONDUCT.md
153
+ - LICENSE.txt
154
+ - README.md
155
+ - Rakefile
156
+ - lib/puma_after_reply.rb
157
+ - lib/puma_after_reply/config.rb
158
+ - lib/puma_after_reply/middleware.rb
159
+ - lib/puma_after_reply/replies_runner.rb
160
+ - lib/puma_after_reply/reply_collector.rb
161
+ - lib/puma_after_reply/version.rb
162
+ - puma_after_reply.gemspec
163
+ homepage: https://github.com/0exp/puma_after_reply
164
+ licenses:
165
+ - MIT
166
+ metadata:
167
+ homepage_uri: https://github.com/0exp/puma_after_reply
168
+ source_code_uri: https://github.com/0exp/puma_after_reply/blob/master
169
+ changelog_uri: https://github.com/0exp/puma_after_reply/blob/master/CHANGELOG.md
170
+ post_install_message:
171
+ rdoc_options: []
172
+ require_paths:
173
+ - lib
174
+ required_ruby_version: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - ">="
177
+ - !ruby/object:Gem::Version
178
+ version: '3.1'
179
+ required_rubygems_version: !ruby/object:Gem::Requirement
180
+ requirements:
181
+ - - ">="
182
+ - !ruby/object:Gem::Version
183
+ version: '0'
184
+ requirements: []
185
+ rubygems_version: 3.5.11
186
+ signing_key:
187
+ specification_version: 4
188
+ summary: 'PumaAfterReply: Puma''s "rack.after_reply" integration for your Rack-applications.'
189
+ test_files: []