Module: Pushwoosh::Helpers

Defined in:
lib/pushwoosh/helpers.rb

Class Method Summary collapse

Class Method Details

.limit_string(str, limit_bytes) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/pushwoosh/helpers.rb', line 4

def limit_string(str, limit_bytes)
  return '' if str.empty? || !str

  str = str.mb_chars.compose.to_s if str.respond_to?(:mb_chars)
  new_str = str.byteslice(0, limit_bytes)
  until new_str[-1].force_encoding('utf-8').valid_encoding?
    # remove the invalid char
    new_str = new_str.slice(0..-2)
  end

  new_str
end