Class: CoreLibrary::JsonPointerHelper
- Inherits:
-
Object
- Object
- CoreLibrary::JsonPointerHelper
- Defined in:
- lib/apimatic-core/utilities/json_pointer_helper.rb
Overview
A utility for json specific operations.
Class Method Summary collapse
-
.get_value_by_json_pointer(hash, pointer, symbolize_keys: false) ⇒ Object?
Retrieves a value from a hash using a JSON pointer.
-
.split_into_parts(json_pointer) ⇒ Array(String, String), Array(nil, nil)
Splits a JSON pointer string into its prefix and field path components.
-
.update_entry_by_json_pointer(hash, pointer, value, symbolize_keys: false) ⇒ Object?
Retrieves a value from a hash using a JSON pointer.
Class Method Details
.get_value_by_json_pointer(hash, pointer, symbolize_keys: false) ⇒ Object?
Retrieves a value from a hash using a JSON pointer.
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.
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.
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 |