Class: JsonReference::Reference
- Inherits:
-
Object
- Object
- JsonReference::Reference
- Includes:
- Comparable
- Defined in:
- lib/json_reference.rb
Instance Attribute Summary collapse
-
#pointer ⇒ Object
Returns the value of attribute pointer.
-
#uri ⇒ Object
Returns the value of attribute uri.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#initialize(ref) ⇒ Reference
constructor
A new instance of Reference.
- #inspect ⇒ Object
-
#resolve_pointer(data) ⇒ Object
Given the document addressed by #uri, resolves the JSON Pointer part of the reference.
- #to_s ⇒ Object
Constructor Details
#initialize(ref) ⇒ Reference
Returns a new instance of Reference.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/json_reference.rb', line 15 def initialize(ref) # given a simple fragment without '#', resolve as a JSON Pointer only as # per spec if ref.include?("#") uri, @pointer = ref.split('#') if uri && !uri.empty? @uri = URI.parse(uri) end @pointer ||= "" else @pointer = ref end # normalize pointers by prepending "#" and stripping trailing "/" @pointer = "#" + @pointer @pointer = @pointer.chomp("/") end |
Instance Attribute Details
#pointer ⇒ Object
Returns the value of attribute pointer.
12 13 14 |
# File 'lib/json_reference.rb', line 12 def pointer @pointer end |
#uri ⇒ Object
Returns the value of attribute uri.
13 14 15 |
# File 'lib/json_reference.rb', line 13 def uri @uri end |
Instance Method Details
#<=>(other) ⇒ Object
33 34 35 |
# File 'lib/json_reference.rb', line 33 def <=>(other) to_s <=> other.to_s end |
#inspect ⇒ Object
37 38 39 |
# File 'lib/json_reference.rb', line 37 def inspect "\#<JsonReference::Reference #{to_s}>" end |
#resolve_pointer(data) ⇒ Object
Given the document addressed by #uri, resolves the JSON Pointer part of the reference.
43 44 45 |
# File 'lib/json_reference.rb', line 43 def resolve_pointer(data) JsonPointer.evaluate(data, @pointer) end |
#to_s ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/json_reference.rb', line 47 def to_s if @uri "#{@uri.to_s}#{@pointer}" else @pointer end end |