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
-
#valid!(original, sanitized, fallback = nil, options = {}) ⇒ Object
This method requires a validation to succeed, raising an exception if it does not.
-
#valid_boolean(bool) ⇒ Boolean?
Sanitizes a boolean value.
-
#valid_channel_key(key) ⇒ String?
Sanitizes a channel key.
-
#valid_channel_name(channel) ⇒ String?
Sanitizes a channel name.
-
#valid_endpoint(endpoint) ⇒ String?
Sanitizes an endpoint name.
-
#valid_host(host) ⇒ String?
Sanitizes a hostname.
-
#valid_identifier(identifier) ⇒ String?
Sanitizes an identifier.
-
#valid_network(name, conf) ⇒ Network?
Sanitizes a network name.
-
#valid_port(port) ⇒ Integer?
Sanitizes a TCP/IP port number.
-
#valid_secret(secret) ⇒ String?
Sanitizes a webhook secret.
-
#valid_string(str) ⇒ String?
Sanitizes a string.
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.
114 115 116 117 118 119 120 121 122 |
# File 'lib/codebot/sanitizers.rb', line 114 def valid!(original, sanitized, fallback = nil, = {}) return sanitized unless sanitized.nil? unless original.nil? raise ValidationError, [:invalid_error] % original.inspect end return instance_variable_get(fallback) if fallback_exist?(fallback) return yield if block_given? raise ValidationError, [:required_error] if [:required] end |
#valid_boolean(bool) ⇒ Boolean?
Sanitizes a boolean value.
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.
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.
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.
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.
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.
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.
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.
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.
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.
63 64 65 |
# File 'lib/codebot/sanitizers.rb', line 63 def valid_string(str) str if str.is_a? String end |