Sha256: 0736f18e1fdd0fb0766b79fe06f64faa346f98c320ad40c3a719723b25b0ad01

Contents?: true

Size: 1.25 KB

Versions: 11

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

require 'active_support/hash_with_indifferent_access'

module RSpec
  module Rails
    module Api
      # Helper methods
      class Utils
        class << self
          ##
          # Sets a value at given dotted path in a hash
          #
          # @param hash      [Hash]   The target hash
          # @param path      [Array]  List of keys to access value
          # @param value     [*]      Value to set
          #
          # @return [Hash] The modified hash
          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

          ##
          # Returns a hash from an object
          #
          # @param value [Hash,Class] A hash or something with a "body" (as responses object in tests)
          #
          # @return [Hash]
          def hash_from_response(value)
            return JSON.parse(value.body) if value.respond_to? :body

            value
          end
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
rspec-rails-api-0.9.0 lib/rspec/rails/api/utils.rb
rspec-rails-api-0.8.3 lib/rspec/rails/api/utils.rb
rspec-rails-api-0.8.2 lib/rspec/rails/api/utils.rb
rspec-rails-api-0.8.1 lib/rspec/rails/api/utils.rb
rspec-rails-api-0.8.0 lib/rspec/rails/api/utils.rb
rspec-rails-api-0.7.0 lib/rspec/rails/api/utils.rb
rspec-rails-api-0.6.3 lib/rspec/rails/api/utils.rb
rspec-rails-api-0.6.2 lib/rspec/rails/api/utils.rb
rspec-rails-api-0.6.1 lib/rspec/rails/api/utils.rb
rspec-rails-api-0.6.0 lib/rspec/rails/api/utils.rb
rspec-rails-api-0.5.0 lib/rspec/rails/api/utils.rb