class SmsTools::EncodingDetection

Constants

MAX_LENGTH_FOR_ENCODING

Attributes

text[R]

Public Class Methods

new(text) click to toggle source
# File lib/sms_tools/encoding_detection.rb, line 22
def initialize(text)
  @text = text
end

Public Instance Methods

ascii?() click to toggle source
# File lib/sms_tools/encoding_detection.rb, line 37
def ascii?
  encoding == :ascii
end
concatenated?() click to toggle source
# File lib/sms_tools/encoding_detection.rb, line 49
def concatenated?
  concatenated_parts > 1
end
concatenated_parts() click to toggle source
# File lib/sms_tools/encoding_detection.rb, line 53
def concatenated_parts
  if length <= MAX_LENGTH_FOR_ENCODING[encoding][:normal]
    1
  else
    (length.to_f / MAX_LENGTH_FOR_ENCODING[encoding][:concatenated]).ceil
  end
end
encoding() click to toggle source
# File lib/sms_tools/encoding_detection.rb, line 26
def encoding
  @encoding ||=
    if text.ascii_only? and SmsTools.use_ascii_encoding?
      :ascii
    elsif SmsTools.use_gsm_encoding? and GsmEncoding.valid?(text)
      :gsm
    else
      :unicode
    end
end
gsm?() click to toggle source
# File lib/sms_tools/encoding_detection.rb, line 41
def gsm?
  encoding == :gsm
end
length() click to toggle source

Returns the number of symbols which the given text will eat up in an SMS message, taking into account any double-space symbols in the GSM 03.38 encoding.

# File lib/sms_tools/encoding_detection.rb, line 70
def length
  if unicode?
    length = text.chars.sum { |char| UnicodeEncoding.character_count(char) }
  else
    length = text.length
    length += text.chars.count { |char| GsmEncoding.double_byte?(char) } if gsm?
  end

  length
end
maximum_length_for(concatenated_parts) click to toggle source
# File lib/sms_tools/encoding_detection.rb, line 61
def maximum_length_for(concatenated_parts)
  message_type = concatenated_parts > 1 ? :concatenated : :normal

  concatenated_parts * MAX_LENGTH_FOR_ENCODING[encoding][message_type]
end
unicode?() click to toggle source
# File lib/sms_tools/encoding_detection.rb, line 45
def unicode?
  encoding == :unicode
end