22
33module JSON
44 class Schema
5+ # a JSON Pointer, as described by RFC 6901 https://tools.ietf.org/html/rfc6901
56 class Pointer
67 class Error < JSON ::Schema ::SchemaError
78 end
@@ -10,6 +11,15 @@ class PointerSyntaxError < Error
1011 class ReferenceError < Error
1112 end
1213
14+ # parse a fragment to an array of reference tokens
15+ #
16+ # #/foo/bar
17+ #
18+ # => ['foo', 'bar']
19+ #
20+ # #/foo%20bar
21+ #
22+ # => ['foo bar']
1323 def self . parse_fragment ( fragment )
1424 fragment = Addressable ::URI . unescape ( fragment )
1525 match = fragment . match ( /\A #/ )
@@ -20,6 +30,15 @@ def self.parse_fragment(fragment)
2030 end
2131 end
2232
33+ # parse a pointer to an array of reference tokens
34+ #
35+ # /foo
36+ #
37+ # => ['foo']
38+ #
39+ # /foo~0bar/baz~1qux
40+ #
41+ # => ['foo~bar', 'baz/qux']
2342 def self . parse_pointer ( pointer_string )
2443 tokens = pointer_string . split ( '/' , -1 ) . map! do |piece |
2544 piece . gsub ( '~1' , '/' ) . gsub ( '~0' , '~' )
@@ -33,6 +52,13 @@ def self.parse_pointer(pointer_string)
3352 end
3453 end
3554
55+ # initializes a JSON::Schema::Pointer from the given representation.
56+ #
57+ # type may be one of:
58+ #
59+ # - :fragment - the representation is a fragment containing a pointer (starting with #)
60+ # - :pointer - the representation is a pointer (starting with /)
61+ # - :reference_tokens - the representation is an array of tokens referencing a path in a document
3662 def initialize ( type , representation )
3763 @type = type
3864 if type == :reference_tokens
@@ -49,6 +75,8 @@ def initialize(type, representation)
4975
5076 attr_reader :reference_tokens
5177
78+ # takes a root json document and evaluates this pointer through the document, returning the value
79+ # pointed to by this pointer.
5280 def evaluate ( document )
5381 reference_tokens . inject ( document ) do |value , token |
5482 if value . is_a? ( Array )
0 commit comments