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
-
.encode_exception(e, include_backtrace = true) ⇒ Object
Generates a description string from an exception object.
- .encode_guid(s) ⇒ Object
-
.encode_html(s) ⇒ Object
Escape the string to be shown into an HTML screen.
- .encode_javascript(s) ⇒ Object
-
.encode_period(period, units) ⇒ Object
Returns a string with a description of a period of time, to be shown in the screen.
-
.encode_string(s) ⇒ Object
Then it makes it compatible with UTF-8.
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
271 272 273 274 275 276 277 278 279 |
# File 'lib/functions.rb', line 271 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
301 302 303 |
# File 'lib/functions.rb', line 301 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
263 264 265 |
# File 'lib/functions.rb', line 263 def self.encode_html(s) encode_string(CGI.escapeHTML(s.to_s)) end |
.encode_javascript(s) ⇒ Object
306 307 308 |
# File 'lib/functions.rb', line 306 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
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/functions.rb', line 284 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
256 257 258 |
# File 'lib/functions.rb', line 256 def self.encode_string(s) s.encode("UTF-8") end |