2022-08-05 18:15:17 +02:00
|
|
|
module OAuthsHelper
|
2024-01-22 14:45:48 +01:00
|
|
|
# Retrieves a value from a hash/array using a given path.
|
|
|
|
|
# obj [Hash, Array] The object or hash to query.
|
|
|
|
|
# path [String] The path to the desired value, using dot notation for nested keys and square brackets for array indexes.
|
|
|
|
|
# returns [Object, nil] The value at the specified path, or nil if the path is invalid or the value is not found.
|
|
|
|
|
def query_path_from_object(obj, path)
|
|
|
|
|
return nil unless obj.class == Hash or obj.class == Array
|
|
|
|
|
return nil unless path.class == String
|
|
|
|
|
begin
|
|
|
|
|
path_array = path
|
|
|
|
|
.split(Regexp.union([ '.', '[', ']' ])) # split by . and []
|
|
|
|
|
.filter { |v| not v.blank? } # remove possible blank values
|
|
|
|
|
.map do |v| # convert indexes to integer
|
|
|
|
|
if v == "0"
|
|
|
|
|
0
|
|
|
|
|
elsif v.to_i == 0
|
|
|
|
|
v
|
|
|
|
|
else
|
|
|
|
|
v.to_i
|
|
|
|
|
end
|
2022-08-05 18:15:17 +02:00
|
|
|
end
|
2024-01-22 14:45:48 +01:00
|
|
|
|
|
|
|
|
path_array.each do |selector|
|
|
|
|
|
break if obj == nil
|
|
|
|
|
|
|
|
|
|
obj = obj[selector]
|
2022-08-05 18:15:17 +02:00
|
|
|
end
|
|
|
|
|
|
2024-01-22 14:45:48 +01:00
|
|
|
obj
|
|
|
|
|
rescue
|
|
|
|
|
nil
|
2022-08-05 18:15:17 +02:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|