Class: OpenAPI::Loader::Ref

Inherits:
String
  • Object
show all
Defined in:
lib/open_api/loader/ref.rb

Overview

Describes json pointer used in “$ref”-erences

See Also:

  • https://tools.ietf.org/html/rfc6901

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](*path) ⇒ OpenAPI::Ref

Builds the pointer from a path

Parameters:

  • path (Array<#to_s>, #to_s)

Returns:

  • (OpenAPI::Ref)


15
16
17
18
19
# File 'lib/open_api/loader/ref.rb', line 15

def self.[](*path)
  path.flatten.compact.each_with_object("#") do |key, obj|
    obj << "/#{key.to_s.gsub('~', '~0').gsub('/', '~1')}"
  end
end

Instance Method Details

#fetch_from(data) ⇒ Object

Extracs referred value from given source by local path (after #)

Parameters:

  • (Hash)

Returns:

  • (Object)


46
47
48
# File 'lib/open_api/loader/ref.rb', line 46

def fetch_from(data)
  read(data, [], *path)
end

#pathArray<String>

The local pointer to the json

Returns:

  • (Array<String>)


33
34
35
36
37
38
# File 'lib/open_api/loader/ref.rb', line 33

def path
  return [] if end_with? "#"
  split(%r{#/?}).last.to_s.split("/").map do |item|
    PARSER.unescape(item).gsub("~1", "/").gsub("~0", "~")
  end
end

#uriURI?

The URI to the remote document

Returns:

  • (URI, nil)


25
26
27
# File 'lib/open_api/loader/ref.rb', line 25

def uri
  URI split("#").first unless start_with? "#"
end