Sha256: ee0bb0a0c2962719c7bc5de0b03a1ae0f4574a960626413ccb62a6f3923ebc20

Contents?: true

Size: 1.95 KB

Versions: 25

Compression:

Stored size: 1.95 KB

Contents

# coding: UTF-8

module Smess
  class << self

    def booleanize(value)
      value.to_s.downcase == "true"
    end

    # returns an array of strings of gsm-compatible lengths
    # this should be used when sending via concatenating providers
    def split_sms(text)
      return [text] unless text.sms_length > 160
      result = []

      while text.sms_length > 0
        part, text = text.split_at( split_point(text) )
        result << part
      end
      result
    end

    # it is not as simple as
    #def split_sms(text)
    #  text = text.scan(/.{1,154}/m) if text.sms_length > SMS_MAX_LENGTH
    #  Array(text)
    #end
    # (which i forgot trying to please Code Climate)



    # returns an array of strings of <160 char lengths, split on whitespace
    # this should be used when sending via non-concatenating providers
    def separate_sms(text)
      return [text] unless text.sms_length > SMS_MAX_LENGTH
      result = []
      while text.sms_length > SMS_MAX_LENGTH
        part, text = text.split_at( separation_point(text) )
        result << part.strip
      end
      result << text.strip
    end

    private

    SMS_MAX_LENGTH = 160

    # for finding the GSM alphabet split point for concatenated message strings
    # The reason this is a bit qirky is that a subset of characters are "extended".
    # These and take 2 bytes and the number of these in the message body alter the
    # "byte" splitpoint.
    def split_point(text)
      end_char = 155
      while text.sms_length > 154
        end_char -= 1
        text = text.split_at(end_char).first
        # puts "split_point #{end_char}"
      end
      end_char
    end

    # This is used when there is no concatenation and you want the string split on whitespace.
    def separation_point(text)
      end_char = SMS_MAX_LENGTH + 1
      while text.sms_length > SMS_MAX_LENGTH || !(text[-1] =~ /\s/)
        end_char -= 1
        text = text.split_at(end_char).first
      end
      end_char
    end

  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
smess-3.3.0 lib/smess/utils.rb
smess-3.2.0 lib/smess/utils.rb
smess-3.1.2 lib/smess/utils.rb
smess-3.1.1 lib/smess/utils.rb
smess-3.1.0 lib/smess/utils.rb
smess-3.0.0 lib/smess/utils.rb
smess-2.3.0 lib/smess/utils.rb
smess-2.2.1 lib/smess/utils.rb
smess-2.2.0 lib/smess/utils.rb
smess-2.1.0 lib/smess/utils.rb
smess-2.0.8 lib/smess/utils.rb
smess-2.0.7 lib/smess/utils.rb
smess-2.0.6 lib/smess/utils.rb
smess-2.0.5 lib/smess/utils.rb
smess-2.0.4 lib/smess/utils.rb
smess-2.0.3 lib/smess/utils.rb
smess-2.0.2 lib/smess/utils.rb
smess-2.0.1 lib/smess/utils.rb
smess-2.0 lib/smess/utils.rb
smess-1.0.10 lib/smess/utils.rb