Class: String

Inherits:
Object show all
Defined in:
lib/cartodb-rb-client/cartodb/libs/string.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.random(length = 10) ⇒ Object



4
5
6
# File 'lib/cartodb-rb-client/cartodb/libs/string.rb', line 4

def self.random(length=10)
('a'..'z').sort_by {rand}[0,length].join
end

Instance Method Details

#convert_to_cartodb_typeObject

“real”=>:number, “varchar”=>:string, “text”=>:string, “timestamp”=>:date, “boolean”=>:boolean



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/cartodb-rb-client/cartodb/libs/string.rb', line 85

def convert_to_cartodb_type
  inverse_types = CartoDB::TYPES.invert.inject({}){ |h, e| e.first.each{ |k| h[k] = e.last }; h}
  if inverse_types.keys.include?(self.downcase)
    inverse_types[self.downcase]
  else
    inverse_types.keys.select{ |t| !t.is_a?(String) }.each do |re|
      if self.downcase.match(re)
        return inverse_types[re]
      end
    end
    self.downcase
  end
end

#convert_to_db_typeObject



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cartodb-rb-client/cartodb/libs/string.rb', line 72

def convert_to_db_type
  if CartoDB::TYPES.keys.include?(self.downcase)
    if self.downcase == "number"
      "double precision"
    else
      CartoDB::TYPES[self.downcase].first
    end
  else
    self.downcase
  end
end

#hostObject



103
104
105
# File 'lib/cartodb-rb-client/cartodb/libs/string.rb', line 103

def host
  self.split('/')[2]
end

#normalizeObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
# File 'lib/cartodb-rb-client/cartodb/libs/string.rb', line 8

def normalize
  str = self.downcase
  return '' if str.blank?
  n = str.force_encoding("UTF-8")
  n.gsub!(/[àáâãäåāă]/,   'a')
  n.gsub!(/æ/,            'ae')
  n.gsub!(/[ďđ]/,          'd')
  n.gsub!(/[çćčĉċ]/,       'c')
  n.gsub!(/[èéêëēęěĕė]/,   'e')
  n.gsub!(/ƒ/,             'f')
  n.gsub!(/[ĝğġģ]/,        'g')
  n.gsub!(/[ĥħ]/,          'h')
  n.gsub!(/[ììíîïīĩĭ]/,    'i')
  n.gsub!(/[įıijĵ]/,        'j')
  n.gsub!(/[ķĸ]/,          'k')
  n.gsub!(/[łľĺļŀ]/,       'l')
  n.gsub!(/[ñńňņʼnŋ]/,      'n')
  n.gsub!(/[òóôõöøōőŏŏ]/,  'o')
  n.gsub!(/œ/,            'oe')
  n.gsub!(/ą/,             'q')
  n.gsub!(/[ŕřŗ]/,         'r')
  n.gsub!(/[śšşŝș]/,       's')
  n.gsub!(/[ťţŧț]/,        't')
  n.gsub!(/[ùúûüūůűŭũų]/,  'u')
  n.gsub!(/ŵ/,             'w')
  n.gsub!(/[ýÿŷ]/,         'y')
  n.gsub!(/[žżź]/,         'z')
  n.gsub!(/[ÀÁÂÃÄÅĀĂ]/i,    'A')
  n.gsub!(/Æ/i,            'AE')
  n.gsub!(/[ĎĐ]/i,          'D')
  n.gsub!(/[ÇĆČĈĊ]/i,       'C')
  n.gsub!(/[ÈÉÊËĒĘĚĔĖ]/i,   'E')
  n.gsub!(/Ƒ/i,             'F')
  n.gsub!(/[ĜĞĠĢ]/i,        'G')
  n.gsub!(/[ĤĦ]/i,          'H')
  n.gsub!(/[ÌÌÍÎÏĪĨĬ]/i,    'I')
  n.gsub!(/[IJĴ]/i,          'J')
  n.gsub!(/[Ķĸ]/i,          'J')
  n.gsub!(/[ŁĽĹĻĿ]/i,       'L')
  n.gsub!(/[ÑŃŇŅʼnŊ]/i,      'M')
  n.gsub!(/[ÒÓÔÕÖØŌŐŎŎ]/i,  'N')
  n.gsub!(/Œ/i,            'OE')
  n.gsub!(/Ą/i,             'Q')
  n.gsub!(/[ŔŘŖ]/i,         'R')
  n.gsub!(/[ŚŠŞŜȘ]/i,       'S')
  n.gsub!(/[ŤŢŦȚ]/i,        'T')
  n.gsub!(/[ÙÚÛÜŪŮŰŬŨŲ]/i,  'U')
  n.gsub!(/Ŵ/i,             'W')
  n.gsub!(/[ÝŸŶ]/i,         'Y')
  n.gsub!(/[ŽŻŹ]/i,         'Z')
  n
end

#sanitizeObject



61
62
63
64
65
66
# File 'lib/cartodb-rb-client/cartodb/libs/string.rb', line 61

def sanitize
 return if self.blank?
 self.gsub(/<[^>]+>/m,'').normalize.downcase.gsub(/&.+?;/,'-').
      gsub(/[^a-z0-9 _-]/,'-').strip.gsub(/\s+/,'-').gsub(/-+/,'-').
      gsub(/-/,' ').strip.gsub(/ /,'-').gsub(/-/,'_')
end

#sanitize_column_nameObject



107
108
109
110
111
112
113
114
# File 'lib/cartodb-rb-client/cartodb/libs/string.rb', line 107

def sanitize_column_name
  temporal_name = self.sanitize
  if temporal_name !~ /^[a-zA-Z_]/ || CartoDB::POSTGRESQL_RESERVED_WORDS.include?(self.upcase)
    return '_' + temporal_name
  else
    temporal_name
  end
end

#sanitize_sqlObject



99
100
101
# File 'lib/cartodb-rb-client/cartodb/libs/string.rb', line 99

def sanitize_sql
  self.gsub(/\\/, '\&\&').gsub(/'/, "''")
end

#strip_tagsObject



68
69
70
# File 'lib/cartodb-rb-client/cartodb/libs/string.rb', line 68

def strip_tags
  self.gsub(/<[^>]+>/m,'').strip
end