Sha256: 4e7f0cfaa941ff3c12995e0136e4b10364dcf25876a702f6d5cdd096de6d5089

Contents?: true

Size: 1.21 KB

Versions: 5

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

require "addressable/uri"
require "public_suffix"

#
# Abstraction over WebMock to reduce duplication
#
# @author Mikael Henriksson <mikael@zoolutions.se>
# @since 0.1.0
#
module StubRequests
  #
  # Module ArgumentValidation provides validation of method arguments
  #
  # @author Mikael Henriksson <mikael@zoolutions.se>
  #
  module ArgumentValidation
    extend self

    #
    # Require the value to be any of the types past in
    #
    #
    # @param [Symbol] name the name of the argument
    # @param [Object] value the actual value of the argument
    # @param [Array, Class, Module] type nil the expected argument value class
    #
    # @raise [InvalidArgumentType] when the value is disallowed
    #
    # @return [void]
    #
    def validate!(name:, value:, type:)
      validate_type!(:name, name, [Symbol, String]) unless name
      validate_type!(name, value, type) if type
    end

    def validate_type!(name, value, type)
      expected_types = Array(type).flatten
      return if expected_types.any? { |is_a| value.is_a?(is_a) }

      raise StubRequests::InvalidArgumentType,
            name: name,
            actual: value.class,
            expected: expected_types
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
stub_requests-0.1.9 lib/stub_requests/argument_validation.rb
stub_requests-0.1.8 lib/stub_requests/argument_validation.rb
stub_requests-0.1.7 lib/stub_requests/argument_validation.rb
stub_requests-0.1.6 lib/stub_requests/argument_validation.rb
stub_requests-0.1.5 lib/stub_requests/argument_validation.rb