Class: OpenapiFirst::DefaultOperationResolver
- Inherits:
-
Object
- Object
- OpenapiFirst::DefaultOperationResolver
- Defined in:
- lib/openapi_first/default_operation_resolver.rb
Instance Method Summary collapse
- #call(operation) ⇒ Object
- #find_class_method_handler(name) ⇒ Object
- #find_const(parent, name) ⇒ Object
- #find_handler(operation_id) ⇒ Object
- #find_instance_method_handler(name) ⇒ Object
-
#initialize(namespace) ⇒ DefaultOperationResolver
constructor
A new instance of DefaultOperationResolver.
Constructor Details
#initialize(namespace) ⇒ DefaultOperationResolver
7 8 9 10 |
# File 'lib/openapi_first/default_operation_resolver.rb', line 7 def initialize(namespace) @namespace = namespace @handlers = {} end |
Instance Method Details
#call(operation) ⇒ Object
12 13 14 |
# 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) ⇒ Object
30 31 32 33 34 |
# 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) ⇒ Object
45 46 47 48 49 50 |
# 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) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# 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) ⇒ Object
36 37 38 39 40 41 42 43 |
# 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 |