Class: SmsTools::EncodingDetection

Inherits:
Object
  • Object
show all
Defined in:
lib/sms_tools/encoding_detection.rb

Constant Summary collapse

MAX_LENGTH_FOR_ENCODING =
{
  gsm: {
    normal:       160,
    concatenated: 153,
  },
  unicode: {
    normal:       70,
    concatenated: 67,
  },
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ EncodingDetection

Returns a new instance of EncodingDetection.



18
19
20
# File 'lib/sms_tools/encoding_detection.rb', line 18

def initialize(text)
  @text = text
end

Instance Attribute Details

#textObject (readonly)

Returns the value of attribute text.



16
17
18
# File 'lib/sms_tools/encoding_detection.rb', line 16

def text
  @text
end

Instance Method Details

#concatenated?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/sms_tools/encoding_detection.rb', line 34

def concatenated?
  concatenated_parts > 1
end

#concatenated_partsObject



38
39
40
41
42
43
44
# File 'lib/sms_tools/encoding_detection.rb', line 38

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

#encodingObject



22
23
24
# File 'lib/sms_tools/encoding_detection.rb', line 22

def encoding
  @encoding ||= GsmEncoding.valid?(text) ? :gsm : :unicode
end

#gsm?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/sms_tools/encoding_detection.rb', line 26

def gsm?
  encoding == :gsm
end

#lengthObject

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



55
56
57
58
59
60
# File 'lib/sms_tools/encoding_detection.rb', line 55

def length
  length = text.length
  length += text.chars.count { |char| GsmEncoding.double_byte?(char) } if gsm?

  length
end

#maximum_length_for(concatenated_parts) ⇒ Object



46
47
48
49
50
# File 'lib/sms_tools/encoding_detection.rb', line 46

def maximum_length_for(concatenated_parts)
  message_type = concatenated_parts > 1 ? :concatenated : :normal

  concatenated_parts * MAX_LENGTH_FOR_ENCODING[encoding][message_type]
end

#unicode?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/sms_tools/encoding_detection.rb', line 30

def unicode?
  encoding == :unicode
end