plivo/plivo-ruby

Count and detect if characters are GSM or UNICODE

Closed this issue · 1 comments

Does, or can this ruby library include the same (or similar) method to detect if SMS characters are GSM or UNICODE encoding?

As seen at the bottom of this page:
https://www.plivo.com/docs/sms/concepts/encoding-and-concatenation#unicode

It is very useful.
We would like to use it in our app, so our clients can know how many SMS they will be sending.

@viktorsmari This feature has not yet been implemented. Our developer @narayana-plivo has created a function that allows you to return your requirements. Please use the code below for now. We'll try to include this in a future release.

require "plivo"
include Plivo

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 > 1
        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

text = "Hello, this is a sample text"
units, count, encoding_type = get_unit_count_encoding(text)
puts units, encoding_type, count

api = RestClient.new("<auth_id>", "<auth_token>")
response = api.messages.create(
  src: "<source_number>",
  dst: "<destination_number>",
  text: text,
)
puts response