Class: StringTruncator
- Inherits:
-
Object
- Object
- StringTruncator
- Defined in:
- lib/end_point_blank/string_truncator.rb
Constant Summary collapse
- DEFAULT_LIMIT =
1000- DEFAULT_SUFFIX =
"<truncated>"
Class Method Summary collapse
Class Method Details
.truncate(str, limit: DEFAULT_LIMIT, suffix: DEFAULT_SUFFIX) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/end_point_blank/string_truncator.rb', line 5 def self.truncate(str, limit: DEFAULT_LIMIT, suffix: DEFAULT_SUFFIX) return "" if str.nil? return str if str.bytesize <= limit suffix_bytes = suffix.bytesize max_bytes = limit - suffix_bytes truncated = str.byteslice(0, max_bytes) # Ensure valid UTF-8 while !truncated.valid_encoding? truncated = truncated.byteslice(0, truncated.bytesize - 1) end truncated + suffix end |