Class: OpenapiSchema::PathObject::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi_schema/path_object/builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interpol_schema) ⇒ Builder

Returns a new instance of Builder.



8
9
10
# File 'lib/openapi_schema/path_object/builder.rb', line 8

def initialize(interpol_schema)
  @interpol_schema = interpol_schema
end

Instance Attribute Details

#interpol_schemaObject (readonly)

Returns the value of attribute interpol_schema.



6
7
8
# File 'lib/openapi_schema/path_object/builder.rb', line 6

def interpol_schema
  @interpol_schema
end

Instance Method Details

#base_templateObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/openapi_schema/path_object/builder.rb', line 16

def base_template
  {
    "#{interpol_schema.http_method.downcase}" => {
      "summary" => "summary",
      "description" => "description",
      "parameters" => request_params,
      "responses" => {
        "#{interpol_schema.response_code}" => {
          "description" => "response description",
          "schema" => interpol_schema.response_params
        }
      }
    }
  }
end

#buildObject



12
13
14
# File 'lib/openapi_schema/path_object/builder.rb', line 12

def build
  base_template.to_yaml
end

#empty_param?(target) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/openapi_schema/path_object/builder.rb', line 83

def empty_param?(target)
  target.nil? || target === {}
end

#enum_params_template(target) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/openapi_schema/path_object/builder.rb', line 67

def enum_params_template(target)
  return {} if target["enum"].nil?

  {
    "enum" => target["enum"],
  }
end

#request_paramsObject



32
33
34
35
36
37
# File 'lib/openapi_schema/path_object/builder.rb', line 32

def request_params
  [
    request_params_in_path_template,
    request_params_in_query_template,
  ].flatten.compact
end

#request_params_in_path_templateObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/openapi_schema/path_object/builder.rb', line 39

def request_params_in_path_template
  return if empty_param?(interpol_schema.request_params_in_path)

  interpol_schema.request_params_in_path["properties"].map do |name, detail|
    {
      "in" => "path",
      "name" => name,
      "type" => detail["type"],
      "required" => true,
    }.merge(
      enum_params_template(detail)
    )
  end
end

#request_params_in_query_templateObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/openapi_schema/path_object/builder.rb', line 54

def request_params_in_query_template
  return if empty_param?(interpol_schema.request_params_in_query)

  interpol_schema.request_params_in_query["properties"].map do |name, detail|
    {
      "in" => "query",
      "name" => name,
      "type" => detail["type"],
    }.merge(enum_params_template(detail))
      .merge(required_param_template(detail))
  end
end

#required_param_template(target) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/openapi_schema/path_object/builder.rb', line 75

def required_param_template(target)
  return {} if target["optional"].nil? || target["optional"] === true

  {
    "required" => true
  }
end