Module: SpotifyWeb::Assertions Private

Included in:
Client, Connection, Handler, Resource, Resource
Defined in:
lib/spotify_web/assertions.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Provides a set of helper methods for making assertions about the content of various objects

Instance Method Summary collapse

Instance Method Details

#assert_valid_keys(hash, *valid_keys) ⇒ nil

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Validates that the given hash only includes the specified valid keys.

Examples:

options = {:name => 'John Smith', :age => 30}

assert_valid_keys(options, :name)           # => ArgumentError: Invalid key(s): age
assert_valid_keys(options, 'name', 'age')   # => ArgumentError: Invalid key(s): age, name
assert_valid_keys(options, :name, :age)     # => nil

Raises:

  • (ArgumentError)

    if any invalid keys are found


16
17
18
19
# File 'lib/spotify_web/assertions.rb', line 16

def assert_valid_keys(hash, *valid_keys)
  invalid_keys = hash.keys - valid_keys
  raise ArgumentError, "Invalid key(s): #{invalid_keys.join(', ')}" unless invalid_keys.empty?
end

#assert_valid_values(value, *valid_values) ⇒ nil

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Validates that the given value only matches one of the specified valid values.

Examples:

value = :age

assert_valid_values(value, :name)           # => ArgumentError: :age is an invalid value; must be one of: :name
assert_valid_values(value, 'name', 'age')   # => ArgumentError: :age is an invalid value; must be one of: :name, "age"
assert_valid_values(value, :name, :age)     # => nil

Raises:

  • (ArgumentError)

    if the value is not found


32
33
34
# File 'lib/spotify_web/assertions.rb', line 32

def assert_valid_values(value, *valid_values)
  raise ArgumentError, "#{value} is an invalid value; must be one of: #{valid_values.join(', ')}" unless valid_values.include?(value)
end