due_sms_counter 0.2.0 → 0.3.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 +4 -4
- data/lib/due_sms_counter/version.rb +1 -1
- data/lib/due_sms_counter.rb +49 -31
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f9d439f08dab4143302f15bef065ef403d62b8c8
|
4
|
+
data.tar.gz: ab583904aba1e38bedf5bb3428e7b0c0c1c86ec3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e236f1620e84b6ff6763732c2aa07dc160dab45ffe190738c2821bfe9ada59d1670c216eca50e052eb722ef0ff0933ef546e43c24f0baa66285a26f6f10b2aab
|
7
|
+
data.tar.gz: 6401f7e897e902d9ede77296ab761c9eefbf0f85bd84c6c07f0a5d1b5f754a3d8e09d605ebdd6741b96e48c6c9d1a7cc3328496c1b3bd38c504782d4ff1e8988
|
data/lib/due_sms_counter.rb
CHANGED
@@ -7,43 +7,61 @@ B7_2SIZED_CHARS, B7_BASE_CHARS, B7_SMS_COUNT, UNICODE_SMS_COUNT = CONSTS.values_
|
|
7
7
|
B7_CHARS = B7_BASE_CHARS + B7_2SIZED_CHARS
|
8
8
|
|
9
9
|
module DueSmsCounter
|
10
|
+
class << self
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
def can_be_sent?(sms, max_sms)
|
13
|
+
char_count = calc_char_count(sms)
|
14
|
+
max_char = calc_max_char_count(max_sms)
|
15
|
+
is_unicode = char_count[:is_unicode]
|
15
16
|
|
16
|
-
|
17
|
-
|
17
|
+
char_count[:char_count] <= max_char[is_unicode ? :unicode : :gsm7]
|
18
|
+
end
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
20
|
+
def calc_char_count(sms)
|
21
|
+
sms = (sms || '').split('')
|
22
|
+
is_7_bits = (sms + B7_CHARS).uniq.length == B7_CHARS.length
|
23
|
+
if is_7_bits
|
24
|
+
{ char_count: sms.length + count_2chars_sized(sms),
|
25
|
+
is_unicode: false }
|
26
|
+
else
|
27
|
+
{ char_count: sms.length,
|
28
|
+
is_unicode: true }
|
29
|
+
end
|
28
30
|
end
|
29
|
-
end
|
30
31
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
32
|
+
def calc_max_char_count(sms_count)
|
33
|
+
{
|
34
|
+
gsm7: (
|
35
|
+
B7_SMS_COUNT[sms_count.to_s] ||
|
36
|
+
B7_SMS_COUNT['max']
|
37
|
+
)[1],
|
38
|
+
unicode: (
|
39
|
+
UNICODE_SMS_COUNT[sms_count.to_s] ||
|
40
|
+
UNICODE_SMS_COUNT['max']
|
41
|
+
)[1]
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
def calc_sms_count(char_count, is_unicode)
|
46
|
+
base = is_unicode ? UNICODE_SMS_COUNT : B7_SMS_COUNT
|
47
|
+
sms_count = 0
|
48
|
+
while 42 do
|
49
|
+
break if !base[sms_count.to_s] || (base[sms_count.to_s][1] >= char_count)
|
50
|
+
sms_count += 1
|
51
|
+
end
|
52
|
+
sms_count
|
53
|
+
end
|
54
|
+
|
55
|
+
def calc_char_and_sms_count(sms)
|
56
|
+
result = calc_char_count(sms)
|
57
|
+
result[:sms_count] = calc_sms_count(result[:char_count], result[:is_unicode])
|
58
|
+
result
|
59
|
+
end
|
43
60
|
|
44
|
-
|
45
|
-
|
46
|
-
|
61
|
+
def count_2chars_sized(sms)
|
62
|
+
sms.reduce(0) do |count, letter|
|
63
|
+
count + (B7_2SIZED_CHARS.include?(letter) ? 1 : 0)
|
64
|
+
end
|
47
65
|
end
|
48
66
|
end
|
49
67
|
end
|