Class: Contrast::Utils::StringUtils

Inherits:
Object
  • Object
show all
Includes:
Components::Interface
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

Methods included from Components::Interface

included

Class Method Details

.force_utf8(str) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/contrast/utils/string_utils.rb', line 50

def self.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
  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.

Parameters:

  • str (String)

    the String to normalize

Returns:

  • (String)

    a copy of the given String, upper cased, trimmed, dashes replaced with underscore, and HTTP trimmed



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/contrast/utils/string_utils.rb', line 76

def self.normalized_key str
  return nil 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..-1] : trimmed
    @_normalized_keys[str] = cut
  end
end

.present?(str) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/contrast/utils/string_utils.rb', line 25

def self.present? str
  !str.nil? && !str.to_s.empty?
end

.protobuf_format(data, truncate: true) ⇒ Object



29
30
31
32
33
34
# File 'lib/contrast/utils/string_utils.rb', line 29

def self.protobuf_format data, truncate: true
  data = data&.to_s
  data = Contrast::Utils::StringUtils.force_utf8(data)
  data = Contrast::Utils::StringUtils.truncate(data) if truncate
  data
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



39
40
41
# File 'lib/contrast/utils/string_utils.rb', line 39

def self.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



21
22
23
# File 'lib/contrast/utils/string_utils.rb', line 21

def self.ret_length string
  string.nil? ? 0 : string.to_s.length
end

.truncate(str, default = Contrast::Utils::ObjectShare::EMPTY_STRING) ⇒ Object

Truncate a string to 255 characters max length



44
45
46
47
48
# File 'lib/contrast/utils/string_utils.rb', line 44

def self.truncate str, default = Contrast::Utils::ObjectShare::EMPTY_STRING
  return default if str.nil?

  str.to_s[0..255]
end