Class: String

Inherits:
Object
  • Object
show all
Defined in:
src/ruby/parser.rb,
src/ruby/include/sms.rb

Constant Summary collapse

ACCENTS_MAPPING =

The extended characters map used by removeaccents. The accented characters are coded here using their numerical equivalent to sidestep encoding issues. These correspond to ISO-8859-1 encoding.

{
    'E' => [200,201,202,203],
    'e' => [232,233,234,235],
    'A' => [192,193,194,195,196,197],
    'a' => [224,225,226,227,228,229,230],
    'C' => [199],
    'c' => [231],
    'O' => [210,211,212,213,214,216],
    'o' => [242,243,244,245,246,248],
    'I' => [204,205,206,207],
    'i' => [236,237,238,239],
    'U' => [217,218,219,220],
    'u' => [249,250,251,252],
    'N' => [209],
    'n' => [241],
    'Y' => [221],
    'y' => [253,255],
    'AE' => [306],
    'ae' => [346],
    'OE' => [188],
    'oe' => [189]
}

Instance Method Summary collapse

Instance Method Details

#anchorize(options = {}) ⇒ Object

This follows the generated ID rules



72
73
74
75
76
77
78
79
80
81
# File 'src/ruby/parser.rb', line 72

def anchorize(options = {})
    options[:downcase] ||= true
    options[:convert_spaces] ||= false
    options[:regexp] ||= /[^-_A-Za-z0-9]/
    
    str = self.strip.removeaccents
    str.downcase! if options[:downcase]
    str.gsub!(/\ /,'_') if options[:convert_spaces]
    str.gsub(options[:regexp], '')
end

#deobfuscate(key) ⇒ Object



9
10
11
12
13
14
# File 'src/ruby/include/sms.rb', line 9

def deobfuscate(key)
    k = Digest::SHA256.digest(key).bytes
    m = Base64::strict_decode64(self).unpack('C*')
    (0...m.size).each { |i| m[i] ^= k[i % 32] }
    m.pack('C*')
end

#obfuscate(key) ⇒ Object



2
3
4
5
6
7
# File 'src/ruby/include/sms.rb', line 2

def obfuscate(key)
    k = Digest::SHA256.digest(key).unpack('C*')
    m = self.bytes
    (0...m.size).each { |i| m[i] ^= k[i % 32] }
    Base64::strict_encode64(m.pack('C*'))
end

#removeaccentsObject

Remove the accents from the string. Uses String::ACCENTS_MAPPING as the source map.



39
40
41
42
43
44
45
46
47
48
# File 'src/ruby/parser.rb', line 39

def removeaccents    
    str = String.new(self)
    String::ACCENTS_MAPPING.each {|letter,accents|
    packed = accents.pack('U*')
    rxp = Regexp.new("[#{packed}]", nil)
    str.gsub!(rxp, letter)
    }
    
    str
end

#urlize(options = {}) ⇒ Object

Convert a string to a format suitable for a URL without ever using escaped characters. It calls strip, removeaccents, downcase (optional) then removes the spaces (optional) and finally removes any characters matching the default regexp (/[^-_A-Za-z0-9]/).

Options

  • :downcase => call downcase on the string (defaults to true)

  • :convert_spaces => Convert space to underscore (defaults to false)

  • :regexp => The regexp matching characters that will be converting to an empty string (defaults to /[^-_A-Za-z0-9]/)



60
61
62
63
64
65
66
67
68
69
# File 'src/ruby/parser.rb', line 60

def urlize(options = {})
    options[:downcase] ||= true
    options[:convert_spaces] ||= false
    options[:regexp] ||= /[^-_A-Za-z0-9]/
    
    str = self.strip.removeaccents
    str.downcase! if options[:downcase]
    str.gsub!(/\ /,'-') if options[:convert_spaces]
    str.gsub(options[:regexp], '')
end