Class: OneMoreTime::IdempotentRequest
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- OneMoreTime::IdempotentRequest
- Defined in:
- lib/one_more_time/idempotent_request.rb
Defined Under Namespace
Classes: PermanentError
Instance Method Summary collapse
- #execute ⇒ Object
- #failure!(exception: nil, response_code: nil, response_body: nil) ⇒ Object
- #failure_attributes(&block) ⇒ Object
- #finished? ⇒ Boolean
- #success ⇒ Object
- #success_attributes(&block) ⇒ Object
Instance Method Details
#execute ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/one_more_time/idempotent_request.rb', line 13 def execute # No-op the request if we already have a saved response. return if finished? begin yield if block_given? rescue PermanentError # Something has called .failure!, so we assume there is now a saved response # and can just no-op rescue StandardError update_and_unlock raise end # TODO: raise unless finished? end |
#failure!(exception: nil, response_code: nil, response_body: nil) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/one_more_time/idempotent_request.rb', line 39 def failure!(exception: nil, response_code: nil, response_body: nil) attrs = (exception.present? && @failure_attributes_block&.call(exception)) || {} attrs.merge!({response_code: response_code, response_body: response_body}.compact) update_and_unlock(attrs) raise PermanentError end |
#failure_attributes(&block) ⇒ Object
9 10 11 |
# File 'lib/one_more_time/idempotent_request.rb', line 9 def failure_attributes(&block) @failure_attributes_block = block end |
#finished? ⇒ Boolean
46 47 48 |
# File 'lib/one_more_time/idempotent_request.rb', line 46 def finished? response_code.present? || response_body.present? end |
#success ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/one_more_time/idempotent_request.rb', line 29 def success ActiveRecord::Base.transaction do result = yield if block_given? attrs = @success_attributes_block&.call(result) || {} update_and_unlock(attrs) end rescue StandardError => exception failure!(exception: exception) end |
#success_attributes(&block) ⇒ Object
5 6 7 |
# File 'lib/one_more_time/idempotent_request.rb', line 5 def success_attributes(&block) @success_attributes_block = block end |