Module: BlackStack::Strings::Encoding

Defined in:
lib/functions.rb

Overview


Encoding: Make a string nice to be shown into an HTML string.


Class Method Summary collapse

Class Method Details

.encode_exception(e, include_backtrace = true) ⇒ Object

Generates a description string from an exception object. Eescapes the string to be shown into an HTML screen. Makes it compatible with UTF-8. More details here: bitbucket.org/leandro_sardi/blackstack/issues/961



206
207
208
209
210
211
212
213
214
# File 'lib/functions.rb', line 206

def self.encode_exception(e, include_backtrace=true)
  ret = encode_html(e.to_s) 
  if (include_backtrace == true)
    e.backtrace.each { |s|
      ret += "<br/>" + encode_html(s)
    } # e.backtrace.each
  end # if
  ret
end

.encode_guid(s) ⇒ Object



236
237
238
# File 'lib/functions.rb', line 236

def self.encode_guid(s)
  return s.gsub('{',"").gsub('}',"").upcase
end

.encode_html(s) ⇒ Object

Escape the string to be shown into an HTML screen. Then it makes it compatible with UTF-8. More details here: bitbucket.org/leandro_sardi/blackstack/issues/961



198
199
200
# File 'lib/functions.rb', line 198

def self.encode_html(s)
  encode_string(CGI.escapeHTML(s.to_s))
end

.encode_javascript(s) ⇒ Object



241
242
243
# File 'lib/functions.rb', line 241

def self.encode_javascript(s)
  s.to_s.gsub("'", "\\\\'").gsub("\r", "' + String.fromCharCode(13) + '").gsub("\n", "' + String.fromCharCode(10) + '")
end

.encode_period(period, units) ⇒ Object

Returns a string with a description of a period of time, to be shown in the screen. period: it may be ‘H’, ‘D’, ‘W’, ‘M’, ‘Y’

units: it is a positive integer



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/functions.rb', line 219

def self.encode_period(period, units)
  s = "Last "
  s += units.to_s + " " if units.to_i > 1
  s += "Hours" if period.upcase == "H" && units.to_i != 1
  s += "Days" if period.upcase == "D" && units.to_i != 1
  s += "Weeks" if period.upcase == "W" && units.to_i != 1
  s += "Months" if period.upcase == "M" && units.to_i != 1
  s += "Years" if period.upcase == "Y" && units.to_i != 1
  s += "Hour" if period.upcase == "H" && units.to_i == 1
  s += "Day" if period.upcase == "D" && units.to_i == 1
  s += "Week" if period.upcase == "W" && units.to_i == 1
  s += "Month" if period.upcase == "M" && units.to_i == 1
  s += "Year" if period.upcase == "Y" && units.to_i == 1
  s
end

.encode_string(s) ⇒ Object

Then it makes it compatible with UTF-8. More details here: bitbucket.org/leandro_sardi/blackstack/issues/961



191
192
193
# File 'lib/functions.rb', line 191

def self.encode_string(s)
  s.encode("UTF-8")
end