Class: CoreLibrary::JsonPointerHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/apimatic-core/utilities/json_pointer_helper.rb

Overview

A utility for json specific operations.

Constant Summary collapse

NotFound =
Class.new
WILDCARD =
'~'.freeze
ARRAY_PUSH_KEY =
'-'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash, path, options = {}) ⇒ JsonPointerHelper

Returns a new instance of JsonPointerHelper.



18
19
20
21
22
# File 'lib/apimatic-core/utilities/json_pointer_helper.rb', line 18

def initialize(hash, path, options = {})
  @hash = hash
  @path = path
  @options = options
end

Class Method Details

.escape_fragment(fragment) ⇒ Object



8
9
10
11
12
# File 'lib/apimatic-core/utilities/json_pointer_helper.rb', line 8

def self.escape_fragment(fragment)
  return fragment if fragment == WILDCARD

  fragment.gsub(/~/, '~0').gsub(%r{/}, '~1')
end

.unescape_fragment(fragment) ⇒ Object



14
15
16
# File 'lib/apimatic-core/utilities/json_pointer_helper.rb', line 14

def self.unescape_fragment(fragment)
  fragment.gsub(/~1/, '/').gsub(/~0/, '~')
end

Instance Method Details

#exists?Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
37
38
# File 'lib/apimatic-core/utilities/json_pointer_helper.rb', line 28

def exists?
  _exists = false
  get_target_member(@hash, path_fragments.dup) do |target, options = {}|
    if options[:wildcard]
      _exists = target.any? { |t| !t.nil? && !t.is_a?(NotFound) }
    else
      _exists = true unless target.is_a?(NotFound)
    end
  end
  _exists
end

#valueObject



24
25
26
# File 'lib/apimatic-core/utilities/json_pointer_helper.rb', line 24

def value
  get_member_value
end