Class: OpenapiContracts::Doc

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi_contracts/doc.rb,
lib/openapi_contracts/doc/with_parameters.rb

Defined Under Namespace

Modules: WithParameters Classes: Header, Operation, Parameter, Path, Pointer, Request, Response, Schema

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Doc

Returns a new instance of Doc.



19
20
21
22
23
24
25
# File 'lib/openapi_contracts/doc.rb', line 19

def initialize(raw)
  @schema = Schema.new(raw)
  @paths = @schema['paths'].to_h do |path, _|
    [path, Path.new(path, @schema.at_pointer(Doc::Pointer['paths', path]))]
  end
  @dynamic_paths = paths.select(&:dynamic?)
end

Instance Attribute Details

#schemaObject (readonly)

Returns the value of attribute schema.



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

def schema
  @schema
end

Class Method Details

.parse(dir, filename = 'openapi.yaml') ⇒ Object



13
14
15
# File 'lib/openapi_contracts/doc.rb', line 13

def self.parse(dir, filename = 'openapi.yaml')
  new Parser.call(dir, filename)
end

Instance Method Details

#operation_for(path, method) ⇒ Object



32
33
34
# File 'lib/openapi_contracts/doc.rb', line 32

def operation_for(path, method)
  OperationRouter.new(self).route(path, method.downcase)
end

#pathsObject

Returns an Enumerator over all paths



28
29
30
# File 'lib/openapi_contracts/doc.rb', line 28

def paths
  @paths.each_value
end

#responses(&block) ⇒ Object

Returns an Enumerator over all Responses



37
38
39
40
41
42
43
44
45
# File 'lib/openapi_contracts/doc.rb', line 37

def responses(&block)
  return enum_for(:responses) unless block_given?

  paths.each do |path|
    path.operations.each do |operation|
      operation.responses.each(&block)
    end
  end
end

#with_path(path) ⇒ Object



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

def with_path(path)
  @paths[path]
end