Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

able to find the unit, count and encoding type #178

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions examples/utils.rb
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions lib/plivo/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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