class Rack::EnvRibbon::HtmlConverter

Public Class Methods

new(html, env) click to toggle source
# File lib/rack/env_ribbon/html_converter.rb, line 6
def initialize(html, env)
  @html = html
  @env = env.to_s
end

Public Instance Methods

insert_env_ribbon_into_body_tag!() click to toggle source
# File lib/rack/env_ribbon/html_converter.rb, line 27
def insert_env_ribbon_into_body_tag!
  content = "<a class=\"github-fork-ribbon left-top red fixed\" onClick=\"this.style.display='none'\" title=\"#{@env}\">#{@env}</a>"
  insert_into('body', content, new_line: true)
end
insert_env_ribbon_style_into_head_tag!() click to toggle source
# File lib/rack/env_ribbon/html_converter.rb, line 20
def insert_env_ribbon_style_into_head_tag!
  css = ::File.open(::File.join(assets_path, 'stylesheets/env_ribbon.css'))
  content = "<style>#{css.read}</style>"
  css.close
  insert_into('head', content, last_line: true, new_line: true)
end
insert_env_string_into_title_tag!() click to toggle source
# File lib/rack/env_ribbon/html_converter.rb, line 15
def insert_env_string_into_title_tag!
  tag = 'title'
  insert_into(tag, "(#{@env}) ") if has_tag?(tag)
end
result() click to toggle source
# File lib/rack/env_ribbon/html_converter.rb, line 32
def result
  @html
end
valid_html?() click to toggle source
# File lib/rack/env_ribbon/html_converter.rb, line 11
def valid_html?
  has_tag?('html') && has_tag?('body')
end

Private Instance Methods

assets_path() click to toggle source
# File lib/rack/env_ribbon/html_converter.rb, line 60
def assets_path
  ::File.expand_path('../../../vendor/assets', __dir__)
end
end_tag_regexp(tag) click to toggle source
# File lib/rack/env_ribbon/html_converter.rb, line 42
def end_tag_regexp(tag)
  /(<\/#{tag}>)/i
end
has_tag?(tag) click to toggle source
# File lib/rack/env_ribbon/html_converter.rb, line 46
def has_tag?(tag)
  !!(@html =~ start_tag_regexp(tag) && @html =~ end_tag_regexp(tag))
end
insert_into(tag, content, last_line: false, new_line: false) click to toggle source
# File lib/rack/env_ribbon/html_converter.rb, line 50
def insert_into(tag, content, last_line: false, new_line: false)
  if last_line
    content_placement = new_line ?  "\n#{content}\n\\1" : "#{content}\\1"
    @html = @html.sub(end_tag_regexp(tag), content_placement)
  else
    content_placement = new_line ?  "\n\\1#{content}\n" : "\\1#{content}"
    @html = @html.sub(start_tag_regexp(tag), content_placement)
  end
end
start_tag_regexp(tag) click to toggle source
# File lib/rack/env_ribbon/html_converter.rb, line 38
def start_tag_regexp(tag)
  /(<#{tag}[^>]*>)/i
end