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.

Class Method Summary collapse

Class Method Details

.get_value_by_json_pointer(hash, pointer, symbolize_keys: false) ⇒ Object?

Retrieves a value from a hash using a JSON pointer.

Parameters:

  • hash (Hash)

    The input hash to search.

  • pointer (String)

    The JSON pointer string (e.g. “#/a/b”).

  • symbolize_keys (Boolean) (defaults to: false)

    Whether to symbolize keys in the hash while resolving.

Returns:

  • (Object, nil)

    The value at the given pointer path, or nil if not found or invalid.



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

def self.get_value_by_json_pointer(hash, pointer, symbolize_keys: false)
  return nil if hash.nil? || pointer.nil? || pointer.strip.empty?

  begin
    json_pointer_resolver = JsonPointer.new(hash, pointer, symbolize_keys: symbolize_keys)
    _value = json_pointer_resolver.value
    _value.is_a?(JsonPointer::NotFound) ? nil : _value
  rescue StandardError
    # Optionally log error or re-raise specific known ones
    nil
  end
end

.split_into_parts(json_pointer) ⇒ Array(String, String), Array(nil, nil)

Splits a JSON pointer string into its prefix and field path components.

Parameters:

  • json_pointer (String, nil)

    The JSON pointer string to split.

Returns:

  • (Array(String, String), Array(nil, nil))

    A tuple with path prefix and field path, or [nil, nil] if input is nil or empty.



9
10
11
12
13
14
15
16
# File 'lib/apimatic-core/utilities/json_pointer_helper.rb', line 9

def self.split_into_parts(json_pointer)
  return [nil, nil] if json_pointer.nil? || json_pointer.strip.empty?

  path_prefix, field_path = json_pointer.split('#', 2)
  field_path ||= ''

  [path_prefix, field_path]
end

.update_entry_by_json_pointer(hash, pointer, value, symbolize_keys: false) ⇒ Object?

Retrieves a value from a hash using a JSON pointer.

Parameters:

  • hash (Hash)

    The input hash to search.

  • pointer (String)

    The JSON pointer string (e.g. “#/a/b”).

  • symbolize_keys (Boolean) (defaults to: false)

    Whether to symbolize keys in the hash while resolving.

Returns:

  • (Object, nil)

    The value at the given pointer path, or nil if not found or invalid.



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/apimatic-core/utilities/json_pointer_helper.rb', line 45

def self.update_entry_by_json_pointer(hash, pointer, value, symbolize_keys: false)
  return hash if hash.nil? || pointer.nil? || pointer.strip.empty?

  begin
    value_extractor = JsonPointer.new(hash, pointer, symbolize_keys: symbolize_keys)
    value_extractor.value = value
    hash
  rescue StandardError
    # Optionally log error or re-raise specific known ones
    hash
  end
end