Module: Contrast::Utils::StringUtils
- Defined in:
- lib/contrast/utils/string_utils.rb
Overview
Utilities for encoding and normalizing strings
Constant Summary collapse
- UTF8 =
'utf-8'
- HTTP_PREFIX =
'HTTP_'
Class Method Summary collapse
-
.force_utf8(str) ⇒ String
Cast the given object, which should be a String, into a UTF-8 String for reporting.
-
.normalized_key(str) ⇒ String
Given a string return a normalized version of that string.
- .present?(str) ⇒ Boolean
-
.protobuf_safe_string(string) ⇒ Object
Protobuf has a very strict typing.
-
.ret_length(string) ⇒ Object
Convenience method.
-
.transform_string(str) ⇒ String
transform string from snake_case to Capitalized Text.
-
.truncate(str, default = Contrast::Utils::ObjectShare::EMPTY_STRING) ⇒ String
Truncate a string to 255 characters max length.
Class Method Details
.force_utf8(str) ⇒ String
Cast the given object, which should be a String, into a UTF-8 String for reporting. All given objects will be cast to their to_s form, except nil which will become the ObjectShare::EMPTY_STRING, and then cast.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/contrast/utils/string_utils.rb', line 48 def force_utf8 str return Contrast::Utils::ObjectShare::EMPTY_STRING unless str str = str.to_s if str.encoding == Encoding::UTF_8 str = str.encode(UTF8, invalid: :replace, undef: :replace) unless str.valid_encoding? else str = str.encode(UTF8, str.encoding, invalid: :replace, undef: :replace) end str.to_s rescue StandardError => e # We were unable to switch the String to a UTF-8 format. # Return non-nil so as not to throw an exception later when trying # to do regexp or other compares on the String Contrast::CONFIG.proto_logger.trace('Unable to cast String to UTF-8 format', e, value: str) Contrast::Utils::ObjectShare::EMPTY_STRING end |
.normalized_key(str) ⇒ String
Given a string return a normalized version of that string. Keys are memoized so that the normalization process doesn’t need to happen every time.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/contrast/utils/string_utils.rb', line 74 def normalized_key str return unless str str = str.to_s @_normalized_keys ||= {} if @_normalized_keys.key?(str) @_normalized_keys[str] else upped = str.upcase stripped = upped.strip! || upped trimmed = stripped.tr!('-', '_') || stripped cut = trimmed.start_with?(HTTP_PREFIX) ? trimmed[5..] : trimmed @_normalized_keys[str] = cut end end |
.present?(str) ⇒ Boolean
21 22 23 |
# File 'lib/contrast/utils/string_utils.rb', line 21 def present? str !str.nil? && !str.to_s.empty? end |
.protobuf_safe_string(string) ⇒ Object
Protobuf has a very strict typing. Nil is not a String and will throw an exception if you try to set it. Use this to be safe. Uses the object share to avoid creating several new strings per request
28 29 30 |
# File 'lib/contrast/utils/string_utils.rb', line 28 def protobuf_safe_string string string.nil? ? Contrast::Utils::ObjectShare::EMPTY_STRING : string.to_s end |
.ret_length(string) ⇒ Object
Convenience method. We assume that we’re working on Strings or tags String representations of things. To that end, we’ll to_s anything that comes in before returning its length.
But don’t worry though, String.to_s just returns self. teehee
17 18 19 |
# File 'lib/contrast/utils/string_utils.rb', line 17 def ret_length string string.nil? ? 0 : string.to_s.length end |
.transform_string(str) ⇒ String
transform string from snake_case to Capitalized Text
94 95 96 97 98 |
# File 'lib/contrast/utils/string_utils.rb', line 94 def transform_string str return unless str str.split('-').map(&:capitalize).join(' ') end |
.truncate(str, default = Contrast::Utils::ObjectShare::EMPTY_STRING) ⇒ String
Truncate a string to 255 characters max length
37 38 39 40 41 |
# File 'lib/contrast/utils/string_utils.rb', line 37 def truncate str, default = Contrast::Utils::ObjectShare::EMPTY_STRING return default if str.nil? str.to_s[0..255] end |