Class: SimpleEndpoint::Endpoint
- Inherits:
-
Object
- Object
- SimpleEndpoint::Endpoint
- Defined in:
- lib/simple_endpoint.rb
Class Method Summary collapse
Instance Method Summary collapse
- #call(result, cases, handler = {}, before_response = {}) ⇒ Object
- #matched_case(cases, result) ⇒ Object
- #obtain_matched_case(cases, result) ⇒ Object
- #procees_handler(matched_case, handler, result, exception_class = nil) ⇒ Object
Class Method Details
.call(operation, handler, cases, before_response = {}, **args) ⇒ Object
33 34 35 36 |
# File 'lib/simple_endpoint.rb', line 33 def self.call(operation, handler, cases, before_response = {}, **args) result = operation.call(**args) new.call(result, cases, handler, before_response) end |
Instance Method Details
#call(result, cases, handler = {}, before_response = {}) ⇒ Object
38 39 40 41 42 |
# File 'lib/simple_endpoint.rb', line 38 def call(result, cases, handler = {}, before_response = {}) matched_case = matched_case(cases, result) procees_handler(matched_case, before_response, result) unless before_response.empty? procees_handler(matched_case, handler, result, UnhadledResultError) end |
#matched_case(cases, result) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/simple_endpoint.rb', line 44 def matched_case(cases, result) matched_case = obtain_matched_case(cases, result) raise OperationIsNotHandled, OPERATION_IS_NOT_HANDLER_ERROR unless matched_case matched_case end |
#obtain_matched_case(cases, result) ⇒ Object
59 60 61 62 63 64 |
# File 'lib/simple_endpoint.rb', line 59 def obtain_matched_case(cases, result) matched_case = cases.each { |kase, condition| break kase if condition.call(result) } return if matched_case.is_a?(Hash) matched_case end |
#procees_handler(matched_case, handler, result, exception_class = nil) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/simple_endpoint.rb', line 51 def procees_handler(matched_case, handler, result, exception_class = nil) if handler.key?(matched_case) handler.dig(matched_case)&.(result) elsif exception_class raise exception_class, "Key: #{matched_case} is not present at #{handler}" end end |