Class: Openapi3Parser::Source
- Inherits:
-
Object
- Object
- Openapi3Parser::Source
- Defined in:
- lib/openapi3_parser/source.rb,
lib/openapi3_parser/source/reference.rb,
lib/openapi3_parser/source/reference_resolver.rb
Overview
Represents a source of data used to produce the OpenAPI document. Documents which do not have any references to external files will only have a single source
Defined Under Namespace
Classes: Reference, ReferenceResolver
Instance Attribute Summary collapse
-
#document ⇒ Document
readonly
The document that this source is associated with.
-
#parent ⇒ Source?
readonly
Set to a Source if this source was created due to a reference within a different Source.
-
#reference_register ⇒ Document::ReferenceRegister
readonly
An object to track references for a document.
-
#source_input ⇒ SourceInput
readonly
The source input which provides the data.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #available? ⇒ Boolean
-
#data ⇒ Object
The data from the source.
-
#data_at_pointer(json_pointer) ⇒ Object
Access the data in a source at a particular pointer.
-
#has_pointer?(json_pointer) ⇒ Boolean
Whether the source has data at the particular pointer.
-
#initialize(source_input, document, reference_register, parent = nil) ⇒ Source
constructor
A new instance of Source.
-
#inspect ⇒ Object
return [String].
-
#register_reference(given_reference, factory, context) ⇒ ReferenceResolver
Used to register a reference with the underlying document and return a reference resolver to access the object referenced.
- #relative_to_root ⇒ String
-
#resolve_source(reference) ⇒ Source
Access/create the source object for a reference.
-
#root? ⇒ Boolean
Whether this is the root source of a document.
Constructor Details
#initialize(source_input, document, reference_register, parent = nil) ⇒ Source
Returns a new instance of Source.
24 25 26 27 28 29 |
# File 'lib/openapi3_parser/source.rb', line 24 def initialize(source_input, document, reference_register, parent = nil) @source_input = source_input @document = document @reference_register = reference_register @parent = parent end |
Instance Attribute Details
#document ⇒ Document (readonly)
The document that this source is associated with
17 18 19 |
# File 'lib/openapi3_parser/source.rb', line 17 def document @document end |
#parent ⇒ Source? (readonly)
Set to a Source if this source was created due to a reference within a different Source
17 18 19 |
# File 'lib/openapi3_parser/source.rb', line 17 def parent @parent end |
#reference_register ⇒ Document::ReferenceRegister (readonly)
An object to track references for a document
17 18 19 |
# File 'lib/openapi3_parser/source.rb', line 17 def reference_register @reference_register end |
#source_input ⇒ SourceInput (readonly)
The source input which provides the data
17 18 19 |
# File 'lib/openapi3_parser/source.rb', line 17 def source_input @source_input end |
Instance Method Details
#==(other) ⇒ Object
107 108 109 |
# File 'lib/openapi3_parser/source.rb', line 107 def ==(other) source_input == other.source_input && document == other.document end |
#available? ⇒ Boolean
37 38 39 |
# File 'lib/openapi3_parser/source.rb', line 37 def available? source_input.available? end |
#data ⇒ Object
The data from the source
32 33 34 |
# File 'lib/openapi3_parser/source.rb', line 32 def data @data ||= normalize_data(source_input.contents) end |
#data_at_pointer(json_pointer) ⇒ Object
Access the data in a source at a particular pointer
91 92 93 94 |
# File 'lib/openapi3_parser/source.rb', line 91 def data_at_pointer(json_pointer) return data if json_pointer.empty? data.dig(*json_pointer) if data.respond_to?(:dig) end |
#has_pointer?(json_pointer) ⇒ Boolean
Whether the source has data at the particular pointer
97 98 99 |
# File 'lib/openapi3_parser/source.rb', line 97 def has_pointer?(json_pointer) # rubocop:disable Naming/PredicateName !data_at_pointer(json_pointer).nil? end |
#inspect ⇒ Object
return [String]
112 113 114 |
# File 'lib/openapi3_parser/source.rb', line 112 def inspect %{#{self.class.name}(input: #{source_input})} end |
#register_reference(given_reference, factory, context) ⇒ ReferenceResolver
Used to register a reference with the underlying document and return a reference resolver to access the object referenced
55 56 57 58 59 60 61 62 63 |
# File 'lib/openapi3_parser/source.rb', line 55 def register_reference(given_reference, factory, context) reference = Reference.new(given_reference) ReferenceResolver.new( reference, factory, context ).tap do |resolver| next if resolver.in_root_source? reference_register.register(resolver.reference_factory) end end |
#relative_to_root ⇒ String
102 103 104 105 |
# File 'lib/openapi3_parser/source.rb', line 102 def relative_to_root return "" if root? source_input.relative_to(document.root_source.source_input) end |
#resolve_source(reference) ⇒ Source
Access/create the source object for a reference
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/openapi3_parser/source.rb', line 69 def resolve_source(reference) if reference.only_fragment? # I found the spec wasn't fully clear on expected behaviour if a source # references a fragment that doesn't exist in it's current document # and just the root source. I'm assuming to be consistent with URI a # fragment only reference only references current JSON document. This # could be incorrect though. self else next_source_input = source_input.resolve_next(reference) source = document.source_for_source_input(next_source_input) source || self.class.new(next_source_input, document, reference_register, self) end end |
#root? ⇒ Boolean
Whether this is the root source of a document
42 43 44 |
# File 'lib/openapi3_parser/source.rb', line 42 def root? document.root_source == self end |