Module: Ovh::Http2sms::GsmEncoding

Defined in:
lib/ovh/http2sms/gsm_encoding.rb

Overview

GSM 03.38 encoding utilities for SMS character counting

Uses the gsm_encoder gem for character validation and provides OVH-specific SMS limits accounting for the STOP clause.

Standard SMS messages use GSM 7-bit encoding which allows 160 characters. Extension characters (€, |, ^, {, }, [, ], ~, ) count as 2 characters. Non-GSM characters force Unicode encoding which limits messages to 70 characters.

Commercial SMS must include STOP clause which uses 11 characters, reducing the first SMS limit.

Constant Summary collapse

GSM_SINGLE_SMS_LIMIT =

SMS limits for GSM 7-bit encoding

160
GSM_CONCAT_SMS_LIMIT =

7 chars used for UDH header in concatenated SMS

153
GSM_FIRST_COMMERCIAL_LIMIT =

After STOP clause (11 chars)

149
GSM_CONCAT_COMMERCIAL_LIMIT =
153
UNICODE_SINGLE_SMS_LIMIT =

SMS limits for Unicode encoding

70
UNICODE_CONCAT_SMS_LIMIT =

3 chars used for UDH header

67
UNICODE_FIRST_COMMERCIAL_LIMIT =

After STOP clause (11 chars)

59
UNICODE_CONCAT_COMMERCIAL_LIMIT =
70
EXTENSION_CHARACTERS =

Extension characters that count as 2 in GSM encoding

Set["", "|", "^", "{", "}", "[", "]", "~", "\\"].freeze

Class Method Summary collapse

Class Method Details

.detect_encoding(message) ⇒ Symbol

Detect the required encoding for a message

Examples:

GsmEncoding.detect_encoding("Hello!") # => :gsm
GsmEncoding.detect_encoding("Привет") # => :unicode

Parameters:

  • message (String)

    The message to analyze

Returns:

  • (Symbol)

    :gsm or :unicode



55
56
57
# File 'lib/ovh/http2sms/gsm_encoding.rb', line 55

def detect_encoding(message)
  gsm_compatible?(message) ? :gsm : :unicode
end

.gsm_char_count(message) ⇒ Integer

Calculate the GSM character count (extension chars count as 2)

Examples:

GsmEncoding.gsm_char_count("Hello") # => 5
GsmEncoding.gsm_char_count("Price: €10") # => 11 (€ counts as 2)

Parameters:

  • message (String)

    The message to count

Returns:

  • (Integer)

    Character count in GSM encoding



67
68
69
70
71
# File 'lib/ovh/http2sms/gsm_encoding.rb', line 67

def gsm_char_count(message)
  message.each_char.sum do |char|
    EXTENSION_CHARACTERS.include?(char) ? 2 : 1
  end
end

.gsm_compatible?(message) ⇒ Boolean

Check if a message contains only GSM 03.38 characters

Examples:

GsmEncoding.gsm_compatible?("Hello!") # => true
GsmEncoding.gsm_compatible?("Hello 👋") # => false

Parameters:

  • message (String)

    The message to check

Returns:

  • (Boolean)

    true if all characters are GSM compatible



43
44
45
# File 'lib/ovh/http2sms/gsm_encoding.rb', line 43

def gsm_compatible?(message)
  GSMEncoder.can_encode?(message)
end

.message_info(message, commercial: true) ⇒ Hash

Calculate message info including SMS count

Examples:

GsmEncoding.message_info("Hello!")
# => { characters: 6, encoding: :gsm, sms_count: 1, remaining: 143, ... }

Parameters:

  • message (String)

    The message to analyze

  • commercial (Boolean) (defaults to: true)

    Whether this is a commercial SMS (includes STOP clause)

Returns:

  • (Hash)

    Message information with keys:

    • :characters - Character count (accounting for extension chars in GSM)

    • :encoding - :gsm or :unicode

    • :sms_count - Number of SMS segments required

    • :remaining - Characters remaining in current segment

    • :max_single_sms - Maximum chars for single SMS with this encoding

    • :non_gsm_chars - Array of non-GSM characters found (empty for GSM encoding)



99
100
101
102
103
104
105
106
107
# File 'lib/ovh/http2sms/gsm_encoding.rb', line 99

def message_info(message, commercial: true)
  encoding = detect_encoding(message)

  if encoding == :gsm
    gsm_message_info(message, commercial: commercial)
  else
    unicode_message_info(message, commercial: commercial)
  end
end

.non_gsm_characters(message) ⇒ Array<String>

Find all non-GSM characters in a message

Examples:

GsmEncoding.non_gsm_characters("Hello 👋 World") # => ["👋"]

Parameters:

  • message (String)

    The message to check

Returns:

  • (Array<String>)

    Array of non-GSM characters found



80
81
82
# File 'lib/ovh/http2sms/gsm_encoding.rb', line 80

def non_gsm_characters(message)
  message.each_char.reject { |char| can_encode_char?(char) }.uniq
end