Class: RSpec::Rails::Api::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/rails/api/utils.rb

Overview

Helper methods

Class Method Summary collapse

Class Method Details

.deep_set(hash, path, value) ⇒ Hash

Sets a value at given dotted path in a hash

Parameters:

  • hash (Hash)

    The target hash

  • path (Array)

    List of keys to access value

  • value (*)

    Value to set

Returns:

  • (Hash)

    The modified hash



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rspec/rails/api/utils.rb', line 19

def deep_set(hash, path, value)
  raise 'path should be an array' unless path.is_a? Array

  return value if path.count.zero?

  current_key       = path.shift.to_s.to_sym
  hash[current_key] = {} unless hash[current_key].is_a?(Hash)
  hash[current_key] = deep_set(hash[current_key], path, value)

  hash
end

.hash_from_response(value) ⇒ Hash

Returns a hash from an object

Parameters:

  • value (Hash, Class)

    A hash or something with a “body” (as responses object in tests)

Returns:

  • (Hash)


37
38
39
40
41
# File 'lib/rspec/rails/api/utils.rb', line 37

def hash_from_response(value)
  return JSON.parse(value.body) if value.respond_to? :body

  value
end