class String

Public Instance Methods

msisdn(*args) click to toggle source

cleans and adds the country prefix on the string given preserveing any existing country code this is not entirely problem-free for US area codes without leading 0. if force_country_code is set it forces the selected country prefix on the string given yes old, yes crappy, yes convoluted… legacy stuff, ok.

# File lib/string_ext.rb, line 73
def msisdn(*args)
  ret = self.dup
  ret.msisdn!(*args)
end
msisdn!(country_code = 1, force_country_code = false) click to toggle source
# File lib/string_ext.rb, line 77
def msisdn!(country_code = 1, force_country_code = false)
  self.gsub!(/\D/,"")

  if begins_with_msisdn_international_prefix
    self.replace(self[2..-1])
  elsif begins_with_msisdn_national_prefix
    self.replace(self[1..-1])
    ensure_msisdn_countrycode_prefix(country_code)
  elsif force_country_code
    ensure_msisdn_countrycode_prefix(country_code)
  end

  validate_msisdn_length_range
  self
end
smess_to_underscore() click to toggle source
# File lib/string_ext.rb, line 3
def smess_to_underscore
  self.gsub(/::/, '/').
  gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
  gsub(/([a-z\d])([A-Z])/,'\1_\2').
  tr("-", "_").
  downcase
end
sms_length() click to toggle source

like strlen but with SMS alphabet calculations

# File lib/string_ext.rb, line 17
def sms_length
  escaped = Regexp.escape('€|^{}[]~\\')
  pattern = Regexp.new( "["+escaped+"]" )

  self.length + self.scan(pattern).length
end
split_at(index) click to toggle source
# File lib/string_ext.rb, line 12
def split_at(index)
    [ self[0, index], self[index..-1] || "" ]
end
strip_nongsm_chars(replacement = "") click to toggle source
# File lib/string_ext.rb, line 25
def strip_nongsm_chars(replacement = "")
  ret = self.dup
  ret.strip_nongsm_chars!(replacement)
  ret
end
strip_nongsm_chars!(replacement = "") click to toggle source

Cleans a string to comply with the GSM alphabet

# File lib/string_ext.rb, line 32
def strip_nongsm_chars!(replacement = "")
  # Should this be a patch to String?

  # keeping them here in canse I need them
  # basic alpha
  # '@','£','$','¥','è','é','ù','ì','ò','Ç',"\n",'Ø','ø',"\r",'Å','å',
  # 'Δ','_','Φ','Γ','Λ','Ω','Π','Ψ','Σ','Θ','Ξ','Æ','æ','ß','É',' ',
  # '!','"','#','¤','%','&','\'','(',')','*','+',',','-','.','/','0',
  # '1','2','3','4','5','6','7','8','9',':',';','<','=','>','?','¡',
  # 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
  # 'Q','R','S','T','U','V','W','X','Y','Z','Ä','Ö','Ñ','Ü','§','¿',
  # 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p',
  # 'q','r','s','t','u','v','w','x','y','z','ä','ö','ñ','ü','à'
  #
  # extended alpha
  # '|','^','€','{','}','[',']','~','\\'

  allowed = '@£$¥èéùìòÇ'+"\n"+'Øø'+"\r"+'ÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ'+' '+Regexp.escape('!"#¤%&\'()*+,-.')+'\/'+Regexp.escape('0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà|^€{}[]~\\')

  map = {
    /á|â|ã/ => "a",
    /ê|ẽ|ë/ => "e",
    /í|î|ï/ => "i",
    /ó|ô|õ/ => "o",
    /ú|ů|û/ => "u",
    /ç/ => "Ç"
  }

  map.each do |key, value|
    self.gsub!(key, value)
  end

  pattern = Regexp.new( "[^"+allowed+"]" )
  self.gsub!(pattern, replacement)
end

Private Instance Methods

begins_with_msisdn_international_prefix() click to toggle source
# File lib/string_ext.rb, line 95
def begins_with_msisdn_international_prefix
  length > 0 && self[0..1] == "00"
end
begins_with_msisdn_national_prefix() click to toggle source

correct most places in the world but notably not in north america (1… countries)

# File lib/string_ext.rb, line 100
def begins_with_msisdn_national_prefix
  length > 0 && self[0] == "0"
end
ensure_msisdn_countrycode_prefix(country_code) click to toggle source

Add country code unless the start of the number is the correct country code

# File lib/string_ext.rb, line 105
def ensure_msisdn_countrycode_prefix(country_code)
  unless self[0..country_code.to_s.length-1] == country_code.to_s
    self.prepend(country_code.to_s)
  end
end
validate_msisdn_length_range() click to toggle source
# File lib/string_ext.rb, line 111
def validate_msisdn_length_range
  unless (10..15) === self.length
    self.replace("")
  end
end