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
- #to_s ⇒ Object
- #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 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
#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
18 19 20 |
# File 'lib/openapi_contracts/doc/path.rb', line 18 def dynamic? @path.include?('{') end |
#operations ⇒ Object
22 23 24 |
# File 'lib/openapi_contracts/doc/path.rb', line 22 def operations @operations.each_value end |
#path_regexp ⇒ Object
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
35 36 37 |
# File 'lib/openapi_contracts/doc/path.rb', line 35 def static? !dynamic? end |
#supports_method?(method) ⇒ Boolean
39 40 41 |
# File 'lib/openapi_contracts/doc/path.rb', line 39 def supports_method?(method) @operations.key?(method) end |
#to_s ⇒ Object
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 |