Class: GrapeOAS::ApiModelBuilders::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/grape_oas/api_model_builders/path.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api:, routes:, app: nil, namespace_filter: nil) ⇒ Path

Returns a new instance of Path.



18
19
20
21
22
23
# File 'lib/grape_oas/api_model_builders/path.rb', line 18

def initialize(api:, routes:, app: nil, namespace_filter: nil)
  @api = api
  @routes = routes
  @app = app
  @namespace_filter = namespace_filter
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



16
17
18
# File 'lib/grape_oas/api_model_builders/path.rb', line 16

def api
  @api
end

#appObject (readonly)

Returns the value of attribute app.



16
17
18
# File 'lib/grape_oas/api_model_builders/path.rb', line 16

def app
  @app
end

#namespace_filterObject (readonly)

Returns the value of attribute namespace_filter.



16
17
18
# File 'lib/grape_oas/api_model_builders/path.rb', line 16

def namespace_filter
  @namespace_filter
end

#routesObject (readonly)

Returns the value of attribute routes.



16
17
18
# File 'lib/grape_oas/api_model_builders/path.rb', line 16

def routes
  @routes
end

Instance Method Details

#buildObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/grape_oas/api_model_builders/path.rb', line 25

def build
  canonical_paths = {}

  @routes.each_with_object({}) do |route, api_routes|
    next if skip_route?(route)

    route_path = sanitize_path(route.path)
    normalized = normalize_template(route_path)

    canonical_info = canonical_paths[normalized]
    path_param_name_map = nil

    if canonical_info
      path_param_name_map = map_param_names(canonical_info[:template], route_path)
      route_path = canonical_info[:template]
    else
      canonical_paths[normalized] = { template: route_path }
    end

    api_path = api_routes.fetch(route_path) do
      path = GrapeOAS::ApiModel::Path.new(template: route_path)
      api_routes[route_path] = path

      api.add_path(path)

      path
    end

    operation = build_operation(route, path_param_name_map: path_param_name_map, template_override: route_path)

    api_path.add_operation(operation)
  end
end