Module: LeveretAuth::Utils
- Defined in:
- lib/leveret_auth/utils.rb
Class Method Summary collapse
- .build_error_message(missing_keys) ⇒ Object
- .deep_slice(hash, keys) ⇒ Object
- .load_json_file(file_path) ⇒ Object
- .validate(hash, required_keys) ⇒ Object
Class Method Details
.build_error_message(missing_keys) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/leveret_auth/utils.rb', line 19 def (missing_keys) = missing_keys.map do |key| key.is_a?(Array) ? key.join(' or ') : key end "Missing keys: #{.join(', ')}" end |
.deep_slice(hash, keys) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/leveret_auth/utils.rb', line 26 def deep_slice(hash, keys) return unless hash.is_a?(Hash) keys.each_with_object({}) do |k, new_hash| if k.is_a?(Hash) k.each do |sub_key, sub_value| new_hash[sub_key] = deep_slice(hash[sub_key], sub_value) if hash.key?(sub_key) end else new_hash[k] = hash[k] if hash.key?(k) end new_hash end end |
.load_json_file(file_path) ⇒ Object
4 5 6 7 |
# File 'lib/leveret_auth/utils.rb', line 4 def load_json_file(file_path) file = File.open(Rails.root.join(file_path), 'r').read JSON.parse(file).deep_symbolize_keys end |
.validate(hash, required_keys) ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'lib/leveret_auth/utils.rb', line 9 def validate(hash, required_keys) required_keys.select do |key| if key.is_a?(Array) hash.slice(*key).empty? else hash[key].nil? end end end |