Class: OpenapiContracts::Doc::Path

Inherits:
Object
  • Object
show all
Includes:
WithParameters
Defined in:
lib/openapi_contracts/doc/path.rb

Constant Summary collapse

HTTP_METHODS =
%w(get head post put delete connect options trace patch).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from WithParameters

#parameters

Constructor Details

#initialize(path, spec) ⇒ Path

Returns a new instance of Path.



9
10
11
12
13
14
15
16
# File 'lib/openapi_contracts/doc/path.rb', line 9

def initialize(path, spec)
  @path = path
  @spec = spec
  @supported_methods = HTTP_METHODS & @spec.keys
  @operations = @supported_methods.to_h do |verb|
    [verb, Doc::Operation.new(self, @spec.navigate(verb))]
  end
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/openapi_contracts/doc/path.rb', line 7

def path
  @path
end

Instance Method Details

#dynamic?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/openapi_contracts/doc/path.rb', line 18

def dynamic?
  @path.include?('{')
end

#operationsObject



22
23
24
# File 'lib/openapi_contracts/doc/path.rb', line 22

def operations
  @operations.each_value
end

#path_regexpObject



26
27
28
29
30
31
32
33
# File 'lib/openapi_contracts/doc/path.rb', line 26

def path_regexp
  @path_regexp ||= begin
    re = /\{([^\}]+)\}/
    @path.gsub(re) { |placeholder|
      placeholder.match(re) { |m| "(?<#{m[1]}>[^/]*)" }
    }.then { |str| Regexp.new("^#{str}$") }
  end
end

#static?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/openapi_contracts/doc/path.rb', line 35

def static?
  !dynamic?
end

#supports_method?(method) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/openapi_contracts/doc/path.rb', line 39

def supports_method?(method)
  @operations.key?(method)
end

#to_sObject



43
44
45
# File 'lib/openapi_contracts/doc/path.rb', line 43

def to_s
  @path
end

#with_method(method) ⇒ Object



47
48
49
# File 'lib/openapi_contracts/doc/path.rb', line 47

def with_method(method)
  @operations[method]
end