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



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

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



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

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



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

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



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

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



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

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