Class: Svelte::OperationBuilder

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

Overview

Dynamically builds Swagger API operations on top of a given module

Class Method Summary collapse

Class Method Details

.build(operation:, module_constant:, configuration:) ⇒ Object

Builds an operation on top of module_constant

Parameters:

  • operation (Svete::Operation)

    operation to build

  • module_constant (Module)

    operation to build

  • configuration (Configuration)

    Swagger API configuration



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.options(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.

Parameters:

  • full_parameters (Array)

    array with the arguments passed into the method call

Returns:

  • (Hash)

    Hash with all the options to be sent as part of the request



52
53
54
# File 'lib/svelte/operation_builder.rb', line 52

def options(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.

Parameters:

  • full_parameters (Array)

    array with the arguments passed into the method call

Returns:

  • (Hash)

    Hash with all the parameters to be sent as part of the request



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.

Parameters:

  • operation_parameters (Array)

    The parameters defined by the operation

  • request_parameters (Hash)

    The parameters given by the caller

Returns:

  • (Hash)

    The headers for GenericOperation to build the request



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