Sha256: 4482d552be51379a3b695350bed3c2b638588abd67d1ae7deec77a867ccc841d

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

require 'due_sms_counter/version'
require 'json'

CONSTS = JSON.parse(File.read(File.join(File.dirname(__FILE__), 'due_sms_counter/consts.json')))

B7_2SIZED_CHARS, B7_BASE_CHARS, B7_SMS_COUNT, UNICODE_SMS_COUNT = CONSTS.values_at(*%w[B7_2SIZED_CHARS B7_BASE_CHARS B7_SMS_COUNT UNICODE_SMS_COUNT])
B7_CHARS = B7_BASE_CHARS + B7_2SIZED_CHARS

module DueSmsCounter

  def self.can_be_sent?(sms, max_sms)
    char_count = calc_char_count(sms)
    max_char   = calc_max_char_count(max_sms)
    is_unicode = char_count[:is_unicode]

    char_count[:char_count] <= max_char[is_unicode ? :unicode : :gsm7]
  end

  def self.calc_char_count(sms)
    sms = (sms || '').split('')
    is_7_bits = (sms + B7_CHARS).uniq.length == B7_CHARS.length
    if is_7_bits
      { char_count: sms.length + count_2chars_sized(sms),
        is_unicode: false }
    else
      { char_count: sms.length,
        is_unicode: true }
    end
  end

  def self.calc_max_char_count(sms_count)
    {
      gsm7: (
        B7_SMS_COUNT[sms_count.to_s] ||
        B7_SMS_COUNT['max']
      )[1],
      unicode: (
        UNICODE_SMS_COUNT[sms_count.to_s] ||
        UNICODE_SMS_COUNT['max']
      )[1]
    }
  end

  def self.count_2chars_sized(sms)
    sms.reduce(0) do |count, letter|
      count + (B7_2SIZED_CHARS.include?(letter) ? 1 : 0)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
due_sms_counter-0.2.0 lib/due_sms_counter.rb