Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/string_ext.rb

Instance Method Summary collapse

Instance Method Details

#msisdn(*args) ⇒ Object

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.



73
74
75
76
# 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) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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_underscoreObject



3
4
5
6
7
8
9
# 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_lengthObject

like strlen but with SMS alphabet calculations



17
18
19
20
21
22
# 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) ⇒ Object



12
13
14
# File 'lib/string_ext.rb', line 12

def split_at(index)
    [ self[0, index], self[index..-1] || "" ]
end

#strip_nongsm_chars(replacement = "") ⇒ Object



25
26
27
28
29
# 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 = "") ⇒ Object

Cleans a string to comply with the GSM alphabet



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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,"")
end