module SimpleJSONAPIClient::Utils

Public Class Methods

hash_dig(hash, *keys) click to toggle source
# File lib/simple_jsonapi_client/utils.rb, line 15
def hash_dig(hash, *keys)
  if hash.respond_to?(:dig)
    hash.dig(*keys)
  else
    dig(hash, keys)
  end
end
symbolize_keys(hash) click to toggle source

Implementation adapted from ActiveSupport Copyright © 2005-2019 David Heinemeier Hansson github.com/rails/rails/blob/b9ca94caea2ca6a6cc09abaffaad67b447134079/activesupport/lib/active_support/core_ext/hash/keys.rb

# File lib/simple_jsonapi_client/utils.rb, line 7
def symbolize_keys(hash)
  result = {}
  hash.each_key do |key|
    result[(key.to_sym rescue key)] = hash[key]
  end
  result
end

Private Class Methods

dig(hash, key, *rest) click to toggle source

Implementation adapted from the hash_dig gem, Copyright © 2015 Colin Kelley, MIT License github.com/Invoca/ruby_dig/blob/19fa8c1d2cc7706d015a3004f028169a2ff83391/lib/ruby_dig.rb

# File lib/simple_jsonapi_client/utils.rb, line 27
def dig(hash, key, *rest)
  value = hash[key]
  if value.nil? || rest.empty?
    value
  elsif value.is_a?(Hash) || value.is_a?(Array)
    dig(value, *rest)
  else
    fail TypeError, "#{value.class} does not work with #dig" # should not happen with our use of #dig
  end
end
transform_values(hash) { |value| ... } click to toggle source
# File lib/simple_jsonapi_client/utils.rb, line 38
def transform_values(hash)
  result = {}
  hash.each do |key, value|
    result[key] = yield(value)
  end
  result
end