Module: Codebot::Sanitizers

Included in:
Channel, Integration, Network, WebServer
Defined in:
lib/codebot/sanitizers.rb

Overview

This module provides data sanitization methods shared among multiple classes.

Instance Method Summary collapse

Instance Method Details

#valid!(original, sanitized, fallback = nil, options = {}) ⇒ Object

This method requires a validation to succeed, raising an exception if it does not. If no original value was provided, it returns, in this order, the given fallback, the return value of any block passed to this method, or, finally, nil, unless the :required option is set, in which case a ValidationError is raised.

Parameters:

  • original (Object)

    the original value

  • sanitized (Object)

    the sanitized value

  • fallback (Object) (defaults to: nil)

    an optional symbol representing an instance variable to be returned when the original value is nil

  • options (Hash) (defaults to: {})

    a hash optionally containing additional settings.

Raises:

  • (ValidationError)

    if the sanitization failed. The error message may be set using the :invalid_error option.

  • (ValidationError)

    if the :required option is set, but neither an original value nor a fallback value was specified and no block was given. The error message may be set using the :required_error option.



114
115
116
117
118
119
120
121
122
# File 'lib/codebot/sanitizers.rb', line 114

def valid!(original, sanitized, fallback = nil, options = {})
  return sanitized unless sanitized.nil?
  unless original.nil?
    raise ValidationError, options[:invalid_error] % original.inspect
  end
  return instance_variable_get(fallback) if fallback_exist?(fallback)
  return yield if block_given?
  raise ValidationError, options[:required_error] if options[:required]
end

#valid_boolean(bool) ⇒ Boolean?

Sanitizes a boolean value.

Parameters:

  • bool (Boolean, nil)

    the boolean to sanitize

Returns:

  • (Boolean, nil)

    the sanitized value or nil on error



55
56
57
# File 'lib/codebot/sanitizers.rb', line 55

def valid_boolean(bool)
  bool if [true, false].include? bool
end

#valid_channel_key(key) ⇒ String?

Sanitizes a channel key.

Parameters:

  • key (String, nil)

    the channel key to sanitize

Returns:

  • (String, nil)

    the sanitized value or nil on error



81
82
83
# File 'lib/codebot/sanitizers.rb', line 81

def valid_channel_key(key)
  key if /\A[[:graph:]&&[^,]]*\z/ =~ key
end

#valid_channel_name(channel) ⇒ String?

Sanitizes a channel name.

Parameters:

  • channel (String, nil)

    the channel name to sanitize

Returns:

  • (String, nil)

    the sanitized value or nil on error



71
72
73
74
75
# File 'lib/codebot/sanitizers.rb', line 71

def valid_channel_name(channel)
  # Colons are currently not considered valid characters because some IRCds
  # use them to delimit channel masks. This might change in the future.
  channel if /\A[&#\+!][[:graph:]&&[^:,]]{,49}\z/ =~ channel
end

#valid_endpoint(endpoint) ⇒ String?

Sanitizes an endpoint name.

Parameters:

  • endpoint (String, nil)

    the endpoint name to sanitize

Returns:

  • (String, nil)

    the sanitized value or nil on error



22
23
24
# File 'lib/codebot/sanitizers.rb', line 22

def valid_endpoint(endpoint)
  endpoint if /\A[[:alnum:]_-]*\z/ =~ endpoint
end

#valid_host(host) ⇒ String?

Sanitizes a hostname.

Parameters:

  • host (String, nil)

    the hostname to sanitize

Returns:

  • (String, nil)

    the sanitized value or nil on error



38
39
40
# File 'lib/codebot/sanitizers.rb', line 38

def valid_host(host)
  host if /\A[[:graph:]]+\z/ =~ host
end

#valid_identifier(identifier) ⇒ String?

Sanitizes an identifier.

Parameters:

  • identifier (String, nil)

    the identifier to sanitize

Returns:

  • (String, nil)

    the sanitized value or nil on error



14
15
16
# File 'lib/codebot/sanitizers.rb', line 14

def valid_identifier(identifier)
  identifier.downcase if /\A[[:alnum:]_-]+\z/ =~ identifier
end

#valid_network(name, conf) ⇒ Network?

Sanitizes a network name.

Parameters:

  • name (String)

    the name of the network

  • conf (Hash)

    the configuration containing all networks

Returns:

  • (Network, nil)

    the corresponding network or nil on error



90
91
92
93
94
# File 'lib/codebot/sanitizers.rb', line 90

def valid_network(name, conf)
  return if name.nil?

  conf[:networks].find { |net| net.name_eql? name }
end

#valid_port(port) ⇒ Integer?

Sanitizes a TCP/IP port number.

Parameters:

  • port (#to_i, #to_s)

    the port number to sanitize

Returns:

  • (Integer, nil)

    the sanitized value or nil on error



46
47
48
49
# File 'lib/codebot/sanitizers.rb', line 46

def valid_port(port)
  port_number = port.to_s.to_i(10) if /\A[0-9]+\z/ =~ port.to_s
  port_number if (1...2**16).cover? port_number
end

#valid_secret(secret) ⇒ String?

Sanitizes a webhook secret.

Parameters:

  • secret (String, nil)

    the webhook secret to sanitize

Returns:

  • (String, nil)

    the sanitized value or nil on error



30
31
32
# File 'lib/codebot/sanitizers.rb', line 30

def valid_secret(secret)
  secret if /\A[[:print:]]*\z/ =~ secret
end

#valid_string(str) ⇒ String?

Sanitizes a string.

Parameters:

  • str (String, nil)

    the string to sanitize

Returns:

  • (String, nil)

    the sanitized value or nil on error



63
64
65
# File 'lib/codebot/sanitizers.rb', line 63

def valid_string(str)
  str if str.is_a? String
end