class OpenapiFirst::DefaultOperationResolver

Public Class Methods

new(namespace) click to toggle source
# File lib/openapi_first/default_operation_resolver.rb, line 7
def initialize(namespace)
  @namespace = namespace
  @handlers = {}
end

Public Instance Methods

call(operation) click to toggle source
# File lib/openapi_first/default_operation_resolver.rb, line 12
def call(operation)
  @handlers[operation.name] ||= find_handler(operation['x-handler'] || operation['operationId'])
end
find_class_method_handler(name) click to toggle source
# File lib/openapi_first/default_operation_resolver.rb, line 30
def find_class_method_handler(name)
  module_name, method_name = name.split('.')
  klass = find_const(@namespace, module_name)
  klass.method(Utils.underscore(method_name))
end
find_const(parent, name) click to toggle source
# File lib/openapi_first/default_operation_resolver.rb, line 45
def find_const(parent, name)
  name = Utils.classify(name)
  throw :halt unless parent.const_defined?(name, false)

  parent.const_get(name, false)
end
find_handler(operation_id) click to toggle source
# File lib/openapi_first/default_operation_resolver.rb, line 16
def find_handler(operation_id)
  name = operation_id.match(/:*(.*)/)&.to_a&.at(1)
  return if name.nil?

  catch :halt do
    return find_class_method_handler(name) if name.include?('.')
    return find_instance_method_handler(name) if name.include?('#')
  end
  method_name = Utils.underscore(name)
  return unless @namespace.respond_to?(method_name)

  @namespace.method(method_name)
end
find_instance_method_handler(name) click to toggle source
# File lib/openapi_first/default_operation_resolver.rb, line 36
def find_instance_method_handler(name)
  module_name, klass_name = name.split('#')
  const = find_const(@namespace, module_name)
  klass = find_const(const, klass_name)
  return ->(params, res) { klass.new.call(params, res) } if klass.instance_method(:initialize).arity.zero?

  ->(params, res) { klass.new(params.env).call(params, res) }
end