module Redacted::ActionView::Helpers
Public Instance Methods
ascii_redact(str)
click to toggle source
# File lib/redacted/action_view/helpers.rb, line 4 def ascii_redact str length = str.length "\u2588" * length unless length.zero? end
redact(str)
click to toggle source
# File lib/redacted/action_view/helpers.rb, line 9 def redact str if str.include?("\n") redact_paragraphs(str).join('').html_safe else redact_text(str).html_safe end end
redact_html(html_str)
click to toggle source
# File lib/redacted/action_view/helpers.rb, line 28 def redact_html html_str doc = Nokogiri::HTML::DocumentFragment.parse(html_str) doc.traverse do |ele| ele.replace(redact_text(ele.content.chomp)) if ele.text? end doc.to_html end
redact_paragraphs(str)
click to toggle source
# File lib/redacted/action_view/helpers.rb, line 17 def redact_paragraphs str str.lines.map do |l| "<p>#{redact_text(l.chomp)}</p>" if l.chomp.present? end.reject{|l|l.nil?} end
redact_text(str)
click to toggle source
# File lib/redacted/action_view/helpers.rb, line 23 def redact_text str return '' unless str "<span class=\"redacted-text\">#{generate_fake_text(str)}</span>" end
Private Instance Methods
generate_fake_text(str)
click to toggle source
# File lib/redacted/action_view/helpers.rb, line 37 def generate_fake_text str word_count = str.split.size fake_text = FFaker::Lorem.words(word_count).join(' ') fake_text << FFaker::Lorem.characters(str.length - fake_text.length) if str.length > fake_text.length fake_text.slice(0, str.length) end