Class: SimpleEndpoint::Endpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_endpoint.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(operation, handler, cases, renderer_options = {}, before_response = {}, **args) ⇒ Object



39
40
41
42
# File 'lib/simple_endpoint.rb', line 39

def self.call(operation, handler, cases, renderer_options = {}, before_response = {}, **args)
  result = operation.call(**args)
  new.call(result, cases, renderer_options, handler, before_response)
end

Instance Method Details

#call(result, cases, renderer_options = {}, handler = {}, before_response = {}) ⇒ Object



44
45
46
47
48
# File 'lib/simple_endpoint.rb', line 44

def call(result, cases, renderer_options = {}, handler = {}, before_response = {})
  matched_case = matched_case(cases, result)
  procees_handler(matched_case, before_response, result, renderer_options) unless before_response.empty?
  procees_handler(matched_case, handler, result, renderer_options, UnhadledResultError)
end

#matched_case(cases, result) ⇒ Object



50
51
52
53
54
55
# File 'lib/simple_endpoint.rb', line 50

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



65
66
67
# File 'lib/simple_endpoint.rb', line 65

def obtain_matched_case(cases, result)
  cases.detect { |_kase, condition| condition.call(result) }&.first
end

#procees_handler(matched_case, handler, result, renderer_options, exception_class = nil) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/simple_endpoint.rb', line 57

def procees_handler(matched_case, handler, result, renderer_options, exception_class = nil)
  if handler.key?(matched_case)
    handler.dig(matched_case)&.(result, **renderer_options)
  elsif exception_class
    raise exception_class, "Key: #{matched_case} is not present at #{handler}"
  end
end