Class: SmsTools::EncodingDetection
- Inherits:
-
Object
- Object
- SmsTools::EncodingDetection
- 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
-
#text ⇒ Object
readonly
Returns the value of attribute text.
Instance Method Summary collapse
- #concatenated? ⇒ Boolean
- #concatenated_parts ⇒ Object
- #encoding ⇒ Object
- #gsm? ⇒ Boolean
-
#initialize(text) ⇒ EncodingDetection
constructor
A new instance of EncodingDetection.
-
#length ⇒ Object
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.
- #maximum_length_for(concatenated_parts) ⇒ Object
- #unicode? ⇒ Boolean
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
#text ⇒ Object (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
34 35 36 |
# File 'lib/sms_tools/encoding_detection.rb', line 34 def concatenated? concatenated_parts > 1 end |
#concatenated_parts ⇒ Object
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 |
#encoding ⇒ Object
22 23 24 |
# File 'lib/sms_tools/encoding_detection.rb', line 22 def encoding @encoding ||= GsmEncoding.valid?(text) ? :gsm : :unicode end |
#gsm? ⇒ Boolean
26 27 28 |
# File 'lib/sms_tools/encoding_detection.rb', line 26 def gsm? encoding == :gsm end |
#length ⇒ Object
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) = concatenated_parts > 1 ? :concatenated : :normal concatenated_parts * MAX_LENGTH_FOR_ENCODING[encoding][] end |
#unicode? ⇒ Boolean
30 31 32 |
# File 'lib/sms_tools/encoding_detection.rb', line 30 def unicode? encoding == :unicode end |