module BlackStack::Strings::Encoding


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


Public Class Methods

encode_exception(e, include_backtrace=true) click to toggle source

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

# File lib/functions.rb, line 234
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) click to toggle source
# File lib/functions.rb, line 264
def self.encode_guid(s)
  return s.gsub('{',"").gsub('}',"").upcase
end
encode_html(s) click to toggle source

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

# File lib/functions.rb, line 226
def self.encode_html(s)
  encode_string(CGI.escapeHTML(s.to_s))
end
encode_javascript(s) click to toggle source
# File lib/functions.rb, line 269
def self.encode_javascript(s)
  s.to_s.gsub("'", "\\\\'").gsub("\r", "' + String.fromCharCode(13) + '").gsub("\n", "' + String.fromCharCode(10) + '")
end
encode_period(period, units) click to toggle source

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

# File lib/functions.rb, line 247
def self.encode_period(period, units)
  s = "Last "
  s += units.to_i.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) click to toggle source

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

# File lib/functions.rb, line 219
def self.encode_string(s)
  s.encode("UTF-8")
end