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



619
620
621
622
623
624
625
626
627
# File 'lib/functions.rb', line 619

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



649
650
651
# File 'lib/functions.rb', line 649

def self.encode_guid(s)
  return s.gsub('{',"").gsub('}',"").downcase
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



611
612
613
# File 'lib/functions.rb', line 611

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

.encode_javascript(s) ⇒ Object



654
655
656
# File 'lib/functions.rb', line 654

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



632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
# File 'lib/functions.rb', line 632

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) ⇒ Object

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



604
605
606
# File 'lib/functions.rb', line 604

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