Module: StringValidator::Sanitizers
- Included in:
- StringValidator
- Defined in:
- lib/string_validator/sanitizers.rb
Constant Summary collapse
- HTML_ESCAPE =
{ "&" => "&", "<" => "<", ">" => ">", '"' => """, "'" => "'", "/" => "/" }.freeze
Instance Method Summary collapse
- #blacklist(str, chars) ⇒ Object
- #escape(str) ⇒ Object
- #normalize_email(str, lowercase_domain: true) ⇒ Object
- #strip_low(str, keep_new_lines: false) ⇒ Object
- #to_boolean(str, strict: true) ⇒ Object
- #to_float(str) ⇒ Object
- #to_int(str, radix: 10) ⇒ Object
- #trim(str, chars = nil) ⇒ Object
- #unescape(str) ⇒ Object
- #whitelist(str, chars) ⇒ Object
Instance Method Details
#blacklist(str, chars) ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/string_validator/sanitizers.rb', line 32 def blacklist(str, chars) return str unless str.is_a?(String) return str if chars.nil? return str unless chars.is_a?(String) str.delete(chars) rescue ArgumentError, Encoding::InvalidByteSequenceError str end |
#escape(str) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/string_validator/sanitizers.rb', line 14 def escape(str) return str unless str.is_a?(String) str.gsub(/[&<>"'\/]/, HTML_ESCAPE) rescue ArgumentError, Encoding::InvalidByteSequenceError str end |
#normalize_email(str, lowercase_domain: true) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/string_validator/sanitizers.rb', line 100 def normalize_email(str, lowercase_domain: true) return str unless str.is_a?(String) s = str.strip return s if s.empty? local, at, domain = s.rpartition("@") return s if at != "@" || local.empty? || domain.empty? local = local.downcase domain = domain.downcase if lowercase_domain "#{local}@#{domain}" rescue ArgumentError, Encoding::InvalidByteSequenceError str end |
#strip_low(str, keep_new_lines: false) ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/string_validator/sanitizers.rb', line 50 def strip_low(str, keep_new_lines: false) return str unless str.is_a?(String) if keep_new_lines str.each_char.select { |c| c.ord >= 32 || c == "\n" }.join else str.each_char.select { |c| c.ord >= 32 }.join end rescue ArgumentError, Encoding::InvalidByteSequenceError str end |
#to_boolean(str, strict: true) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/string_validator/sanitizers.rb', line 61 def to_boolean(str, strict: true) return str unless str.is_a?(String) s = str.downcase.strip return true if s == "true" || s == "1" return false if s == "false" || s == "0" return true if !strict && %w[yes y].include?(s) return false if !strict && %w[no n].include?(s) str rescue ArgumentError, Encoding::InvalidByteSequenceError str end |
#to_float(str) ⇒ Object
80 81 82 83 84 85 |
# File 'lib/string_validator/sanitizers.rb', line 80 def to_float(str) return str unless str.is_a?(String) Float(str) rescue ArgumentError, TypeError str end |
#to_int(str, radix: 10) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/string_validator/sanitizers.rb', line 73 def to_int(str, radix: 10) return str unless str.is_a?(String) Integer(str, radix.to_i) rescue ArgumentError, TypeError str end |
#trim(str, chars = nil) ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/string_validator/sanitizers.rb', line 21 def trim(str, chars = nil) return str unless str.is_a?(String) if chars.nil? || chars == "" return str.strip end chars_str = chars.to_s str.gsub(/\A[#{Regexp.escape(chars_str)}]+|[#{Regexp.escape(chars_str)}]+\z/, "") rescue TypeError, ArgumentError, Encoding::InvalidByteSequenceError str end |
#unescape(str) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/string_validator/sanitizers.rb', line 87 def unescape(str) return str unless str.is_a?(String) str .gsub("&", "&") .gsub("<", "<") .gsub(">", ">") .gsub(""", '"') .gsub("'", "'") .gsub("/", "/") rescue ArgumentError, Encoding::InvalidByteSequenceError str end |
#whitelist(str, chars) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/string_validator/sanitizers.rb', line 41 def whitelist(str, chars) return str unless str.is_a?(String) return str if chars.nil? allowed = (chars) str.each_char.select { |c| allowed.include?(c) }.join rescue TypeError, ArgumentError, Encoding::InvalidByteSequenceError str end |