Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/cps-client/core_ext.rb

Constant Summary collapse

CONVERTION_TABLE =
{
  'Ç'       => 'C',
  'ç'       => 'c',
  'û'       => 'u',
  'ú'       => 'u',
  'ù'       => 'u',
  'é'       => 'e',
  'ê'       => 'e',
  'ë'       => 'e',
  'è'       => 'e',
  'â'       => 'a',
  'à'       => 'a',
  'å'       => 'a',
  'á'       => 'a',
  'ï'       => 'i',
  'î'       => 'i',
  'ì'       => 'i',
  'í'       => 'i',
  'Å'       => 'A',
  'É'       => 'E',
  'æ'       => 'ae',
  'Æ'       => 'Ae',
  'ô'       => 'o',
  'ò'       => 'o',
  'ó'       => 'o',
  'Ä'       => 'Ae',
  'Ö'       => 'Oe',
  'Ü'       => 'Ue',
  'ä'       => 'ae',
  'ö'       => 'oe',
  'ü'       => 'ue',
  'ß'       => 'ss',
  'ÿ'       => 'y',
  'Ö'       => 'O',
  'ñ'       => 'n',
  'Ñ'       => 'N'
}

Instance Method Summary collapse

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/cps-client/core_ext.rb', line 79

def blank?
  self.nil? || self == ""
end

#domain_to_punycodeObject



83
84
85
86
87
88
89
90
# File 'lib/cps-client/core_ext.rb', line 83

def domain_to_punycode
  p = Punycode.encode(self)
  unless p.match(/-$/)
    "xn--#{p}"
  else
    p.sub(/-$/, "")
  end
end

#i18n_safeObject



45
46
47
48
49
50
51
52
# File 'lib/cps-client/core_ext.rb', line 45

def i18n_safe
  s = self
  return "" unless s
  CONVERTION_TABLE.each do |key, value|
    s = s.gsub(key, value)
  end
  return s
end

#to_iso_8859_1Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/cps-client/core_ext.rb', line 67

def to_iso_8859_1
  i = nil
  begin
    i = Iconv.new('ISO-8859-1', 'UTF-8') # Iconv.new(to, from)
    i.iconv(self.i18n_safe)
  rescue
    self
  ensure
    i.close unless i.nil?
  end
end

#to_punycodeObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cps-client/core_ext.rb', line 54

def to_punycode
  result = self
  if self.match(/@/)
    name   = self.split('@')[0]
    url    = Domainatrix.parse("http://"+self.split('@')[1]) # Domainatrix needs a protocol...
    result = "#{name}@#{url.domain.domain_to_punycode}.#{url.public_suffix}"
  elsif self.match(/./)
    url    = Domainatrix.parse("http://"+self)
    result = "#{url.domain.domain_to_punycode}.#{url.public_suffix}"
  end
  return result
end