Class: Svelte::OperationBuilder
- Inherits:
-
Object
- Object
- Svelte::OperationBuilder
- Defined in:
- lib/svelte/operation_builder.rb
Overview
Dynamically builds Swagger API operations on top of a given module
Class Method Summary collapse
-
.build(operation:, module_constant:, configuration:) ⇒ Object
Builds an operation on top of
module_constant
. -
.options(full_parameters:) ⇒ Hash
Returns the options that are to be sent as part of the request to the API endpoint.
-
.request_parameters(full_parameters:) ⇒ Hash
Returns the parameters that are to be sent as part of the request to the API endpoint.
-
.strip_headers!(operation_parameters:, request_parameters:) ⇒ Hash
Strips headers from parameters and returns them.
Class Method Details
.build(operation:, module_constant:, configuration:) ⇒ Object
Builds an operation on top of module_constant
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/svelte/operation_builder.rb', line 11 def build(operation:, module_constant:, configuration:) builder = self method_name = StringManipulator.method_name_for(operation.id) module_constant.define_singleton_method(method_name) do |*parameters| request_parameters = builder.request_parameters(full_parameters: parameters) headers = builder.strip_headers!( operation_parameters: operation.properties["parameters"], request_parameters: request_parameters) GenericOperation.call( verb: operation.verb, path: operation.path, configuration: configuration, headers: headers, parameters: request_parameters, options: builder.(full_parameters: parameters) ) end end |
.options(full_parameters:) ⇒ Hash
Returns the options that are to be sent as part of the request to the API endpoint.
52 53 54 |
# File 'lib/svelte/operation_builder.rb', line 52 def (full_parameters:) full_parameters[1] || {} end |
.request_parameters(full_parameters:) ⇒ Hash
Note:
All keys will be transformed from Symbol
to String
Returns the parameters that are to be sent as part of the request to the API endpoint.
38 39 40 41 42 43 44 |
# File 'lib/svelte/operation_builder.rb', line 38 def request_parameters(full_parameters:) return {} if full_parameters.compact.empty? full_parameters.first.inject({}) do |memo, (k, v)| memo.merge!(k.to_s => v) end end |
.strip_headers!(operation_parameters:, request_parameters:) ⇒ Hash
Strips headers from parameters and returns them.
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/svelte/operation_builder.rb', line 60 def strip_headers!(operation_parameters:, request_parameters:) header_names = operation_parameters.reduce([]) do |memo, param| memo.push(param["name"].downcase) if param["in"] == 'header' memo end headers = request_parameters.select { |key, val| header_names.include?(key)} request_parameters.reject! { |key, val| header_names.include?(key) } headers.empty? ? nil : headers end |