diff --git a/examples/utils.rb b/examples/utils.rb new file mode 100644 index 00000000..7363cb52 --- /dev/null +++ b/examples/utils.rb @@ -0,0 +1,11 @@ +require 'rubygems' +require 'plivo' + +include Plivo +#able to identify the number of units, total character and encoding type +#function get_unit_count_encoding takes input string param, example:message content +begin + text = "❤aabbbb" + units, count, encoding_type = Utils.get_unit_count_encoding(text) + puts units +end \ No newline at end of file diff --git a/lib/plivo/utils.rb b/lib/plivo/utils.rb index 5b94f45e..50a9fb66 100644 --- a/lib/plivo/utils.rb +++ b/lib/plivo/utils.rb @@ -290,5 +290,34 @@ def valid_signatureV3?(uri, nonce, signature, auth_token, method, params={}) generated_signature = compute_signatureV3?(new_url, auth_token, nonce) return signature.split(",").include? generated_signature end + + def get_unit_count_encoding(text) + unit = 1 + mx_unsplit_len = 160 + mx_part_len = 153 + count = text.size + if text.force_encoding("UTF-8").ascii_only? + encoding_type = "GSM" + else + encoding_type = "UNICODE" + mx_unsplit_len = 70 + mx_part_len = 67 + end + if count <= mx_unsplit_len + return unit, count, encoding_type + end + charcount = text.size + while text.size > 0 + if text.size > mx_part_len + text = text[mx_part_len..charcount] + unit += 1 + else + text = '' + end + charcount = text.size + end + return unit, count, encoding_type + end + end end