Sha256: c4de90d7ec3eaa35e2953d3f903bfbc61d7ad9aeee908fa23911f3db6da5c0f2

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 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
  # Your code goes here...

  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.1.1 lib/due_sms_counter.rb