module SmsTools::GsmEncoding

UTF-8 to GSM-7 (GSM 03.38) mapping. Based on code from: github.com/threez/smspromote/blob/master/lib/smspromote/encoding.rb

Constants

GSM_EXTENSION_TABLE_ESCAPE_CODE
GSM_TO_UTF8
UTF8_TO_GSM
UTF8_TO_GSM_BASE_TABLE
UTF8_TO_GSM_EXTENSION_TABLE

Public Instance Methods

double_byte?(char) click to toggle source
# File lib/sms_tools/gsm_encoding.rb, line 160
def double_byte?(char)
  UTF8_TO_GSM_EXTENSION_TABLE[char.unpack('U').first]
end
from_utf8(utf8_encoded_string) click to toggle source
# File lib/sms_tools/gsm_encoding.rb, line 164
def from_utf8(utf8_encoded_string)
  gsm_encoded_string = ''

  utf8_encoded_string.unpack('U*').each do |char|
    if converted = UTF8_TO_GSM[char]
      gsm_encoded_string << converted
    else
      raise "Unsupported symbol in GSM-7 encoding: 0x#{char.to_s(16).upcase}"
    end
  end

  gsm_encoded_string
end
to_utf8(gsm_encoded_string) click to toggle source
# File lib/sms_tools/gsm_encoding.rb, line 178
def to_utf8(gsm_encoded_string)
  utf8_encoded_string = ''
  escape              = false

  gsm_encoded_string.each_char do |char|
    if char == GSM_EXTENSION_TABLE_ESCAPE_CODE
      escape = true
    elsif escape
      escape = false
      utf8_encoded_string << [fetch_utf8_char(GSM_EXTENSION_TABLE_ESCAPE_CODE + char)].pack('U')
    else
      utf8_encoded_string << [fetch_utf8_char(char)].pack('U')
    end
  end

  utf8_encoded_string
end
valid?(utf8_encoded_string) click to toggle source
# File lib/sms_tools/gsm_encoding.rb, line 156
def valid?(utf8_encoded_string)
  utf8_encoded_string.unpack('U*').all? { |char| UTF8_TO_GSM[char] }
end

Private Instance Methods

fetch_utf8_char(char) click to toggle source
# File lib/sms_tools/gsm_encoding.rb, line 198
def fetch_utf8_char(char)
  GSM_TO_UTF8.fetch(char) { raise "Unsupported symbol in GSM-7 encoding: #{char}" }
end