gsm_texting 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/character_constants.rb +10 -0
- data/lib/conversion_table.rb +276 -0
- data/lib/gsm_texting.rb +60 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 03234a631d38f4635ce82a83f5ca18333707720b26e4dbe0b85fa13c48f4aaf6
|
4
|
+
data.tar.gz: 3577bc5333944115ce46c1d3f5426676e94b7bfbc7f5cd0c10e4e01208601ee9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b08c53257ce053edcc96fcf1b7d232cbadea8814c4719224863b419b746c693a5113192674ee825cf9f7499db41c9cb198f807ae6936a32d75c9a6940fb97d42
|
7
|
+
data.tar.gz: 260563b1489dfba0e615e2be20e68080834d5af5f0bb88d8f8f5bf7f2061df0728e6f48fe51ad9b283ecef437e61aa4a0bfa9a6bf287d934958867da88b65440
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module CharacterConstants
|
2
|
+
SP = 32.chr.freeze # Space
|
3
|
+
SQ = 39.chr.freeze # Single Quote
|
4
|
+
DQ = 34.chr.freeze # Double Quote
|
5
|
+
BS = 92.chr.freeze # Backslash
|
6
|
+
LF = 10.chr.freeze # Line Feed
|
7
|
+
CR = 13.chr.freeze # Carriage Return
|
8
|
+
ESC = 92.chr.freeze # Escape
|
9
|
+
FF = 12.chr.freeze # Page Break
|
10
|
+
end
|
@@ -0,0 +1,276 @@
|
|
1
|
+
require 'character_constants'
|
2
|
+
|
3
|
+
module ConversionTable
|
4
|
+
include CharacterConstants
|
5
|
+
|
6
|
+
# https://www.twilio.com/docs/sms/services/copilot-smart-encoding-char-list
|
7
|
+
# Converts UNICODE characters to GSM compatible ones
|
8
|
+
CONVERSION_TABLE = {
|
9
|
+
0x00ab => DQ, # «
|
10
|
+
0x00bb => DQ, # »
|
11
|
+
0x02ba => DQ, # ʺ
|
12
|
+
0x02ee => DQ, # ˮ
|
13
|
+
0x201f => DQ, # ‟
|
14
|
+
0x275d => DQ, # ❝
|
15
|
+
0x275e => DQ, # ❞
|
16
|
+
0x301d => DQ, #
|
17
|
+
0x301e => DQ, # 〞
|
18
|
+
0xff02 => DQ, #
|
19
|
+
0x2018 => SQ, # ‘
|
20
|
+
0x2019 => SQ, # ’
|
21
|
+
0x02bb => SQ, # ʻ
|
22
|
+
0x02c8 => SQ, # ˈ
|
23
|
+
0x02bc => SQ, # ʼ
|
24
|
+
0x02bd => SQ, # ʽ
|
25
|
+
0x02b9 => SQ, # ʹ
|
26
|
+
0xff07 => SQ, #
|
27
|
+
0x00b4 => SQ, # ´
|
28
|
+
0x02ca => SQ, # ˊ
|
29
|
+
0x0060 => SQ, # `
|
30
|
+
0x02cb => SQ, # ˋ
|
31
|
+
0x275b => SQ, # ❛
|
32
|
+
0x275c => SQ, # ❜
|
33
|
+
0x0313 => SQ, #
|
34
|
+
0x0314 => SQ, #
|
35
|
+
0xfe10 => SQ, #
|
36
|
+
0xfe11 => SQ, #
|
37
|
+
0x00f7 => '/', # ÷
|
38
|
+
0x00bc => '1/4', # ¼
|
39
|
+
0x00bd => '1/2', # ½
|
40
|
+
0x00be => '3/4', # ¾
|
41
|
+
0x29f8 => '/', # ⧸
|
42
|
+
0x0337 => '/', #
|
43
|
+
0x0338 => '/', #
|
44
|
+
0x2044 => '/', # ⁄
|
45
|
+
0x2215 => '/', # ∕
|
46
|
+
0xff0f => '/', # /
|
47
|
+
0x29f9 => BS, # ⧹
|
48
|
+
0x29f5 => BS, # ⧵
|
49
|
+
0x20e5 => BS, #
|
50
|
+
0xfe68 => BS, #
|
51
|
+
0xff3c => BS, # \
|
52
|
+
0x0332 => '_', #
|
53
|
+
0xff3f => '_', # _
|
54
|
+
0x20d2 => '|', #
|
55
|
+
0x20d3 => '|', #
|
56
|
+
0x2223 => '|', # ∣
|
57
|
+
0xff5c => '|', #
|
58
|
+
0x23b8 => '|', # ⎸
|
59
|
+
0x23b9 => '|', #
|
60
|
+
0x23d0 => '|', # ⏐
|
61
|
+
0x239c => '|', # ⎜
|
62
|
+
0x239f => '|', # ⎟
|
63
|
+
0x23bc => '-', # ⎼
|
64
|
+
0x23bd => '-', # ⎽
|
65
|
+
0x2015 => '-', # ―
|
66
|
+
0xfe63 => '-', # ﹣
|
67
|
+
0xff0d => '-', # -
|
68
|
+
0x2010 => '-', # ‐
|
69
|
+
0x2043 => '-', # ⁃
|
70
|
+
0xfe6b => '@', # ﹫
|
71
|
+
0xff20 => '@', # @
|
72
|
+
0xfe69 => '$', # ﹩
|
73
|
+
0xff04 => '$', # $
|
74
|
+
0x01c3 => '!', # ǃ
|
75
|
+
0xfe15 => '!', #
|
76
|
+
0xfe57 => '!', #
|
77
|
+
0xff01 => '!', # !
|
78
|
+
0xfe5f => '#', # ﹟
|
79
|
+
0xff03 => '#', # #
|
80
|
+
0xfe6a => '%', # ﹪
|
81
|
+
0xff05 => '%', # %
|
82
|
+
0xfe60 => '&', # ﹠
|
83
|
+
0xff06 => '&', # &
|
84
|
+
0x0326 => ',', #
|
85
|
+
0xfe50 => ',', # ﹐
|
86
|
+
0xfe51 => ',', # ﹑
|
87
|
+
0xff0c => ',', # ,
|
88
|
+
0xff64 => ',', # 、
|
89
|
+
0x2768 => '(', # ❨
|
90
|
+
0x276a => '(', # ❪
|
91
|
+
0xfe59 => '(', # ﹙
|
92
|
+
0xff08 => '(', #
|
93
|
+
0x27ee => '(', # ⟮
|
94
|
+
0x2985 => '(', # ⦅
|
95
|
+
0x2769 => ')', # ❩
|
96
|
+
0x276b => ')', # ❫
|
97
|
+
0xfe5a => ')', # ﹚
|
98
|
+
0xff09 => ')', # )
|
99
|
+
0x27ef => ')', # ⟯
|
100
|
+
0x2986 => ')', # ⦆
|
101
|
+
0x204e => '*', # ⁎
|
102
|
+
0x2217 => '*', # ∗
|
103
|
+
0x229b => '*', # ⊛
|
104
|
+
0x2722 => '*', # ✢
|
105
|
+
0x2723 => '*', # ✣
|
106
|
+
0x2724 => '*', # ✤
|
107
|
+
0x2725 => '*', # ✥
|
108
|
+
0x2731 => '*', # ✱
|
109
|
+
0x2732 => '*', # ✲
|
110
|
+
0x2733 => '*', # ✳
|
111
|
+
0x273a => '*', # ✺
|
112
|
+
0x273b => '*', # ✻
|
113
|
+
0x273c => '*', # ✼
|
114
|
+
0x273d => '*', # ✽
|
115
|
+
0x2743 => '*', # ❃
|
116
|
+
0x2749 => '*', # ❉
|
117
|
+
0x274a => '*', # ❊
|
118
|
+
0x274b => '*', # ❋
|
119
|
+
0x29c6 => '*', # ⧆
|
120
|
+
0xfe61 => '*', # ﹡
|
121
|
+
0xff0a => '*', # *
|
122
|
+
0x02d6 => '+', # ˖
|
123
|
+
0xfe62 => '+', # ﹢
|
124
|
+
0xff0b => '+', # +
|
125
|
+
0x3002 => '.', # 。
|
126
|
+
0xfe52 => '.', # ﹒
|
127
|
+
0xff0e => '.', # .
|
128
|
+
0xff61 => '.', # 。
|
129
|
+
0xff10 => '0', # 0
|
130
|
+
0xff11 => '1', # 1
|
131
|
+
0xff12 => '2', # 2
|
132
|
+
0xff13 => '3', # 3
|
133
|
+
0xff14 => '4', # 4
|
134
|
+
0xff15 => '5', # 5
|
135
|
+
0xff16 => '6', # 6
|
136
|
+
0xff17 => '7', # 7
|
137
|
+
0xff18 => '8', # 8
|
138
|
+
0xff19 => '9', # 9
|
139
|
+
0x02d0 => ':', # ː
|
140
|
+
0x02f8 => ':', # ˸
|
141
|
+
0x2982 => ':', # ˸
|
142
|
+
0xa789 => ':', # ꞉
|
143
|
+
0xfe13 => ':', # ︓
|
144
|
+
0xff1a => ':', # :
|
145
|
+
0x204f => ';', # ⁏
|
146
|
+
0xfe14 => ';', # ︔
|
147
|
+
0xfe54 => ';', # ﹔
|
148
|
+
0xff1b => ';', # ;
|
149
|
+
0xfe64 => '<', # ﹤
|
150
|
+
0xff1c => '<', # <
|
151
|
+
0x0347 => '=', #
|
152
|
+
0xa78a => '=', # ꞊
|
153
|
+
0xfe66 => '=', # ﹦
|
154
|
+
0xff1d => '=', # =
|
155
|
+
0xfe65 => '>', # ﹥
|
156
|
+
0xff1e => '>', # >
|
157
|
+
0xfe16 => '?', # ︖
|
158
|
+
0xfe56 => '?', # ﹖
|
159
|
+
0xff1f => '?', # ?
|
160
|
+
0xff21 => 'A', # A
|
161
|
+
0x1d00 => 'A', # ᴀ
|
162
|
+
0xff22 => 'B', # B
|
163
|
+
0x0299 => 'B', # ʙ
|
164
|
+
0xff23 => 'C', # C
|
165
|
+
0x1d04 => 'C', # ᴄ
|
166
|
+
0xff24 => 'D', # D
|
167
|
+
0x1d05 => 'D', # ᴅ
|
168
|
+
0xff25 => 'E', # E
|
169
|
+
0x1d07 => 'E', # ᴇ
|
170
|
+
0xff26 => 'F', # F
|
171
|
+
0xA730 => 'F', # ꜰ
|
172
|
+
0xff27 => 'G', # G
|
173
|
+
0x0262 => 'G', # ɢ
|
174
|
+
0xff28 => 'H', # H
|
175
|
+
0x029c => 'H', # ʜ
|
176
|
+
0xff29 => 'I', # I
|
177
|
+
0x026a => 'I', # ɪ
|
178
|
+
0xff2a => 'J', # J
|
179
|
+
0x1d0a => 'J', # ᴊ
|
180
|
+
0xff2b => 'K', # K
|
181
|
+
0x1d0b => 'K', # ᴋ
|
182
|
+
0xff2c => 'L', # L
|
183
|
+
0x029f => 'L', # ʟ
|
184
|
+
0xff2d => 'M', # M
|
185
|
+
0x1d0d => 'M', # ᴍ
|
186
|
+
0xff2e => 'N', # N
|
187
|
+
0x0274 => 'N', # ɴ
|
188
|
+
0xff2f => 'O', # O
|
189
|
+
0x1d0f => 'O', # ᴏ
|
190
|
+
0xff30 => 'P', # P
|
191
|
+
0x1d18 => 'P', # ᴘ
|
192
|
+
0xff31 => 'Q', # Q
|
193
|
+
0xff32 => 'R', # R
|
194
|
+
0x0280 => 'R', # ʀ
|
195
|
+
0xff33 => 'S', # S
|
196
|
+
0xa731 => 'S', # ꜱ
|
197
|
+
0xff34 => 'T', # T
|
198
|
+
0x1d1b => 'T', # ᴛ
|
199
|
+
0xff35 => 'U', # U
|
200
|
+
0x1d1c => 'U', # ᴜ
|
201
|
+
0xff36 => 'V', # V
|
202
|
+
0x1d20 => 'V', # ᴠ
|
203
|
+
0xff37 => 'W', # W
|
204
|
+
0x1d21 => 'W', # ᴡ
|
205
|
+
0xff38 => 'X', # X
|
206
|
+
0xff39 => 'Y', # Y
|
207
|
+
0x028f => 'Y', # ʏ
|
208
|
+
0xff3a => 'Z', # Z
|
209
|
+
0x1d22 => 'Z', # ᴢ
|
210
|
+
0x02c6 => '^', # ˆ
|
211
|
+
0x0302 => '^', #
|
212
|
+
0xff3e => '^', # ^
|
213
|
+
0x1dcd => '^', #
|
214
|
+
0x2774 => '{', # ❴
|
215
|
+
0xfe5b => '{', # ﹛
|
216
|
+
0xff5b => '{', #
|
217
|
+
0x2775 => '}', # ❵
|
218
|
+
0xfe5c => '}', # ﹜
|
219
|
+
0xff5d => '}', # }
|
220
|
+
0xff3b => '[', #
|
221
|
+
0xff3d => ']', # ]
|
222
|
+
0x02dc => '~', # ˜
|
223
|
+
0x02f7 => '~', # ˷
|
224
|
+
0x0303 => '~', #
|
225
|
+
0x0330 => '~', #
|
226
|
+
0x0334 => '~', #
|
227
|
+
0x223c => '~', # ∼
|
228
|
+
0xff5e => '~', # ~
|
229
|
+
0x00a0 => SP,
|
230
|
+
0x2000 => SP,
|
231
|
+
0x2001 => SP,
|
232
|
+
0x2002 => SP,
|
233
|
+
0x2003 => SP,
|
234
|
+
0x2004 => SP,
|
235
|
+
0x2005 => SP,
|
236
|
+
0x2006 => SP,
|
237
|
+
0x2007 => SP,
|
238
|
+
0x2008 => SP,
|
239
|
+
0x2009 => SP,
|
240
|
+
0x200a => SP,
|
241
|
+
0x3000 => SP,
|
242
|
+
0x008d => SP,
|
243
|
+
0x009f => SP,
|
244
|
+
0x0080 => SP,
|
245
|
+
0x0090 => SP,
|
246
|
+
0x009b => SP,
|
247
|
+
0x0010 => SP,
|
248
|
+
0x0009 => SP,
|
249
|
+
0x0000 => SP,
|
250
|
+
0x0003 => SP,
|
251
|
+
0x0004 => SP,
|
252
|
+
0x0017 => SP,
|
253
|
+
0x0019 => SP,
|
254
|
+
0x0011 => SP,
|
255
|
+
0x0012 => SP,
|
256
|
+
0x0013 => SP,
|
257
|
+
0x0014 => SP,
|
258
|
+
0x2017 => '_', # ‗
|
259
|
+
0x2014 => '-', # —
|
260
|
+
0x2013 => '-', # –
|
261
|
+
0x201a => SQ, # ‚
|
262
|
+
0x202f => SP,
|
263
|
+
0x2039 => '>', # ‹
|
264
|
+
0x203A => '<', # ›
|
265
|
+
0x203c => '!!', # ‼
|
266
|
+
0x201e => DQ, # „
|
267
|
+
0x201d => DQ, # ”
|
268
|
+
0x201c => DQ, # “
|
269
|
+
0x201b => SQ, # ‛
|
270
|
+
0x2026 => '...', # …
|
271
|
+
0x2028 => SP,
|
272
|
+
0x2029 => SP,
|
273
|
+
0x205f => SP,
|
274
|
+
0x2060 => SP
|
275
|
+
}.freeze
|
276
|
+
end
|
data/lib/gsm_texting.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'character_constants'
|
2
|
+
require 'conversion_table'
|
3
|
+
|
4
|
+
module GSMTexting
|
5
|
+
include CharacterConstants
|
6
|
+
include ConversionTable
|
7
|
+
extend self
|
8
|
+
|
9
|
+
DEFAULT_REPLACE_CHAR = "?".freeze
|
10
|
+
|
11
|
+
BASIC_CHARACTER_SET = [
|
12
|
+
'@', 'Δ', SP, '0', '¡', 'P', '¿', 'p',
|
13
|
+
'£', '_', '!', '1', 'A', 'Q', 'a', 'q',
|
14
|
+
'$', 'Φ', DQ, '2', 'B', 'R', 'b', 'r',
|
15
|
+
'¥', 'Γ', '#', '3', 'C', 'S', 'c', 's',
|
16
|
+
'è', 'Λ', '¤', '4', 'D', 'T', 'd', 't',
|
17
|
+
'é', 'Ω', '%', '5', 'E', 'U', 'e', 'u',
|
18
|
+
'ù', 'Π', '&', '6', 'F', 'V', 'f', 'v',
|
19
|
+
'ì', 'Ψ', SQ, '7', 'G', 'W', 'g', 'w',
|
20
|
+
'ò', 'Σ', '(', '8', 'H', 'X', 'h', 'x',
|
21
|
+
'Ç', 'Θ', ')', '9', 'I', 'Y', 'i', 'y',
|
22
|
+
LF, 'Ξ', '*', ':', 'J', 'Z', 'j', 'z',
|
23
|
+
'Ø', ESC, '+', ';', 'K', 'Ä', 'k', 'ä',
|
24
|
+
'ø', 'Æ', ',', '<', 'L', 'Ö', 'l', 'ö',
|
25
|
+
CR, 'æ', '-', '=', 'M', 'Ñ', 'm', 'ñ',
|
26
|
+
'Å', 'ß', '.', '>', 'N', 'Ü', 'n', 'ü',
|
27
|
+
'å', 'É', '/', '?', 'O', '§', 'o', 'à'
|
28
|
+
].freeze
|
29
|
+
|
30
|
+
BASIC_CHARACTER_SET_EXTENSION = [
|
31
|
+
FF, BS, '|', '^', '€', '{', '}', '[', '~', ']'
|
32
|
+
].freeze
|
33
|
+
|
34
|
+
CHARACTER_SET_REGEX = Regexp.escape(BASIC_CHARACTER_SET.join)
|
35
|
+
CHARACTER_EXTENSION_REGEX = Regexp.escape(BASIC_CHARACTER_SET_EXTENSION.compact.join)
|
36
|
+
|
37
|
+
GSM_REGEX = /\A[#{CHARACTER_SET_REGEX}#{CHARACTER_EXTENSION_REGEX}]*\z/.freeze
|
38
|
+
|
39
|
+
# Verifies if string can be encoded in GSM-7 without loosing information
|
40
|
+
def can_encode?(str)
|
41
|
+
str.nil? || !(GSM_REGEX =~ str).nil?
|
42
|
+
end
|
43
|
+
|
44
|
+
# Convert unicode chars to GSM-7 compatible equivalents if possible
|
45
|
+
def encode(str, replace_char: nil)
|
46
|
+
return if str.nil?
|
47
|
+
|
48
|
+
unless replace_char && can_encode?(replace_char)
|
49
|
+
replace_char = DEFAULT_REPLACE_CHAR
|
50
|
+
end
|
51
|
+
|
52
|
+
str.unpack("U*").map { |char|
|
53
|
+
gsm_compatible_char = CONVERSION_TABLE[char]
|
54
|
+
utf8_char = char.chr(Encoding::UTF_8)
|
55
|
+
gsm_char = can_encode?(utf8_char) ? utf8_char : replace_char
|
56
|
+
|
57
|
+
gsm_compatible_char || gsm_char
|
58
|
+
}.join
|
59
|
+
end
|
60
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gsm_texting
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jorge Kalmbach
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-09-12 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: GSM Text encode
|
14
|
+
email:
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/character_constants.rb
|
20
|
+
- lib/conversion_table.rb
|
21
|
+
- lib/gsm_texting.rb
|
22
|
+
homepage: https://github.com/kalmbach/gsm_texting
|
23
|
+
licenses:
|
24
|
+
- MIT
|
25
|
+
metadata: {}
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubygems_version: 3.0.3
|
42
|
+
signing_key:
|
43
|
+
specification_version: 4
|
44
|
+
summary: GSM Text encode
|
45
|
+
test_files: []
|