Class: OpenapiContracts::Doc::Path
- Inherits:
-
Object
- Object
- OpenapiContracts::Doc::Path
- 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
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #dynamic? ⇒ Boolean
-
#initialize(path, spec) ⇒ Path
constructor
A new instance of Path.
- #operations ⇒ Object
- #path_regexp ⇒ Object
- #static? ⇒ Boolean
- #supports_method?(method) ⇒ Boolean
- #with_method(method) ⇒ Object
Methods included from WithParameters
Constructor Details
#initialize(path, spec) ⇒ Path
Returns a new instance of Path.
9 10 11 12 13 |
# File 'lib/openapi_contracts/doc/path.rb', line 9 def initialize(path, spec) @path = path @spec = spec @supported_methods = HTTP_METHODS & @spec.keys end |
Instance Attribute Details
#path ⇒ Object (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
15 16 17 |
# File 'lib/openapi_contracts/doc/path.rb', line 15 def dynamic? @path.include?('{') end |
#operations ⇒ Object
19 20 21 |
# File 'lib/openapi_contracts/doc/path.rb', line 19 def operations @supported_methods.each.lazy.map { |m| Doc::Operation.new(self, @spec.navigate(m)) } end |
#path_regexp ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/openapi_contracts/doc/path.rb', line 23 def path_regexp @path_regexp ||= begin re = /\{(\S+)\}/ @path.gsub(re) { |placeholder| placeholder.match(re) { |m| "(?<#{m[1]}>[^/]*)" } }.then { |str| Regexp.new(str) } end end |
#static? ⇒ Boolean
32 33 34 |
# File 'lib/openapi_contracts/doc/path.rb', line 32 def static? !dynamic? end |
#supports_method?(method) ⇒ Boolean
36 37 38 |
# File 'lib/openapi_contracts/doc/path.rb', line 36 def supports_method?(method) @supported_methods.include?(method) end |