Class: Asimov::Utils::RequestOptionsValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/asimov/utils/request_options_validator.rb

Overview

Validates a set of request options that are passed to the application configuration or on Asimov::Client initialization. Ensures that the value is a hash, that all keys are symbols, and that all keys correspond to legitimate options (typically relating to network behavior). Currently does not validate the option values.

Constant Summary collapse

ALLOWED_OPTIONS =

This is taken from HTTParty

%i[timeout open_timeout read_timeout write_timeout local_host local_port
verify verify_peer ssl_ca_file ssl_ca_path ssl_version ciphers
http_proxyaddr http_proxyport http_proxyuser http_proxypass].freeze

Class Method Summary collapse

Class Method Details

.check_symbol(key) ⇒ Object



48
49
50
51
52
53
# File 'lib/asimov/utils/request_options_validator.rb', line 48

def self.check_symbol(key)
  return if key.is_a?(Symbol)

  raise Asimov::ConfigurationError,
        "Request options keys must be symbols.  The key '#{key}' is not a symbol."
end

.check_unsupported_options(unsupported_options) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/asimov/utils/request_options_validator.rb', line 36

def self.check_unsupported_options(unsupported_options)
  return if unsupported_options.empty?

  quoted_keys = unsupported_options.map { |k| "'#{k}'" }
  raise Asimov::ConfigurationError,
        "The options #{quoted_keys.join(',')} are not supported."
end

.generate_unsupported_options(options) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/asimov/utils/request_options_validator.rb', line 27

def self.generate_unsupported_options(options)
  unsupported_options = []
  options.each_key do |k|
    check_symbol(k)
    unsupported_options << k unless supported_option?(k)
  end
  unsupported_options
end

.supported_option?(key) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/asimov/utils/request_options_validator.rb', line 44

def self.supported_option?(key)
  ALLOWED_OPTIONS.include?(key)
end

.validate(options) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/asimov/utils/request_options_validator.rb', line 17

def self.validate(options)
  unless options.is_a?(Hash)
    raise Asimov::ConfigurationError,
          "Request options must be a hash"
  end

  check_unsupported_options(generate_unsupported_options(options))
  options
end