Class: OpenAPIParser::PathItemFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi_parser/path_item_finder.rb

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Constructor Details

#initialize(paths) ⇒ PathItemFinder

Returns a new instance of PathItemFinder.

Parameters:



3
4
5
# File 'lib/openapi_parser/path_item_finder.rb', line 3

def initialize(paths)
  @paths = paths
end

Instance Method Details

#operation_object(http_method, request_path) ⇒ Result?

find operation object and if not exist return nil

Parameters:

  • http_method (String, Symbol)

    like (get, post .… allow symbol)

  • request_path (String)

Returns:



11
12
13
14
15
16
17
18
19
20
# File 'lib/openapi_parser/path_item_finder.rb', line 11

def operation_object(http_method, request_path)
  if (path_item_object = @paths.path[request_path])
    if (op = path_item_object.operation(http_method))
      return Result.new(path_item_object, op, request_path, {}) # find no path_params path
    end
  end

  # check with path_params
  parse_request_path(http_method, request_path)
end

#parse_path_parameters(schema_path, request_path) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/openapi_parser/path_item_finder.rb', line 41

def parse_path_parameters(schema_path, request_path)
  parameters = path_parameters(schema_path)
  return nil if parameters.empty?

  # If there are regex special characters in the path, the regex will
  # be too permissive, so escape the non-parameter parts.
  components = []
  unprocessed = schema_path.dup
  parameters.each do |parameter|
    parts = unprocessed.partition(parameter)
    components << Regexp.escape(parts[0]) unless parts[0] == ''
    components << "(?<#{param_name(parameter)}>.+)"
    unprocessed = parts[2]
  end
  components << Regexp.escape(unprocessed) unless unprocessed == ''

  regex = components.join('')
  matches = request_path.match(regex)
  return nil unless matches

  # Match up the captured names with the captured values as a hash
  matches.names.zip(matches.captures).to_h
end