-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add comments * Extended models to return identical Response objects * Rename path to pointer * Rename schema to raw
- Loading branch information
Showing
6 changed files
with
94 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,47 @@ | ||
module OpenapiContracts | ||
class Doc | ||
autoload :Header, 'openapi_contracts/doc/header' | ||
autoload :Method, 'openapi_contracts/doc/method' | ||
autoload :Parser, 'openapi_contracts/doc/parser' | ||
autoload :Path, 'openapi_contracts/doc/path' | ||
autoload :Response, 'openapi_contracts/doc/response' | ||
autoload :Schema, 'openapi_contracts/doc/schema' | ||
|
||
def self.parse(dir, filename = 'openapi.yaml') | ||
new Parser.call(dir, filename) | ||
end | ||
|
||
attr_reader :schema | ||
|
||
def initialize(schema) | ||
@schema = Schema.new(schema) | ||
@paths = @schema['paths'].to_h do |path, _| | ||
[path, Path.new(@schema.at_pointer(['paths', path]))] | ||
end | ||
end | ||
|
||
delegate :dig, :fetch, :[], :at_path, to: :@schema | ||
# Returns an Enumerator over all paths | ||
def paths | ||
@paths.each_value | ||
end | ||
|
||
def response_for(path, method, status) | ||
path = ['paths', path, method, 'responses', status] | ||
return unless dig(*path).present? | ||
with_path(path)&.with_method(method)&.with_status(status) | ||
end | ||
|
||
# Returns an Enumerator over all Responses | ||
def responses(&block) | ||
return enum_for(:responses) unless block_given? | ||
|
||
paths.each do |path| | ||
path.methods.each do |method| | ||
method.responses.each(&block) | ||
end | ||
end | ||
end | ||
|
||
Response.new(@schema.at_path(path)) | ||
def with_path(path) | ||
@paths[path] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module OpenapiContracts | ||
class Doc::Method | ||
def initialize(schema) | ||
@schema = schema | ||
@responses = schema['responses'].to_h do |status, _| | ||
[status, Doc::Response.new(schema.navigate('responses', status))] | ||
end | ||
end | ||
|
||
def responses | ||
@responses.each_value | ||
end | ||
|
||
def with_status(status) | ||
@responses[status] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module OpenapiContracts | ||
class Doc::Path | ||
def initialize(schema) | ||
@schema = schema | ||
@methods = @schema.to_h do |method, _| | ||
[method, Doc::Method.new(@schema.navigate(method))] | ||
end | ||
end | ||
|
||
def methods | ||
@methods.each_value | ||
end | ||
|
||
def with_method(method) | ||
@methods[method] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,45 @@ | ||
module OpenapiContracts | ||
# Represents a part or the whole schema | ||
# Generally even parts of the schema contain the whole schema, but store the pointer to | ||
# their position in the overall schema. This allows even small sub-schemas to resolve | ||
# links to any other part of the schema | ||
class Doc::Schema | ||
attr_reader :path, :schema | ||
attr_reader :pointer, :raw | ||
|
||
def initialize(schema, path = nil) | ||
@schema = schema | ||
@path = path.freeze | ||
def initialize(raw, pointer = nil) | ||
@raw = raw | ||
@pointer = pointer.freeze | ||
end | ||
|
||
# Resolves Schema ref pointers links like "$ref: #/some/path" and returns new sub-schema | ||
# at the target if the current schema is only a ref link. | ||
def follow_refs | ||
if (ref = dig('$ref')) | ||
at_path(ref.split('/')[1..]) | ||
if (ref = as_h['$ref']) | ||
at_pointer(ref.split('/')[1..]) | ||
else | ||
self | ||
end | ||
end | ||
|
||
# Generates a fragment pointer for the current schema path | ||
def fragment | ||
path.map { |p| p.gsub('/', '~1') }.join('/').then { |s| "#/#{s}" } | ||
pointer.map { |p| p.gsub('/', '~1') }.join('/').then { |s| "#/#{s}" } | ||
end | ||
|
||
delegate :dig, :fetch, :key?, :[], to: :to_h | ||
delegate :dig, :fetch, :key?, :[], :to_h, to: :as_h | ||
|
||
def at_path(path) | ||
self.class.new(schema, path) | ||
def at_pointer(pointer) | ||
self.class.new(raw, pointer) | ||
end | ||
|
||
def to_h | ||
return @schema if path.nil? || path.empty? | ||
def as_h | ||
return @raw if pointer.nil? || pointer.empty? | ||
|
||
@schema.dig(*path) | ||
@raw.dig(*pointer) | ||
end | ||
|
||
def navigate(*sub_path) | ||
self.class.new(schema, (path + Array.wrap(sub_path))) | ||
def navigate(*spointer) | ||
self.class.new(@raw, (pointer + Array.wrap(spointer))) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters