class Coverband::Reporters::Web

Attributes

request[R]

Public Class Methods

new() click to toggle source

NOTE: if the user doesn't setup the webreporter we don't need any of the below files loaded or using memory

# File lib/coverband.rb, line 128
def initialize
  require "coverband/reporters/web"
  require "coverband/utils/html_formatter"
  require "coverband/utils/result"
  require "coverband/utils/file_list"
  require "coverband/utils/source_file"
  require "coverband/utils/lines_classifier"
  require "coverband/utils/results"
  require "coverband/reporters/html_report"
  init_web
end

Public Instance Methods

call(env) click to toggle source
# File lib/coverband/reporters/web.rb, line 33
def call(env)
  @request = Rack::Request.new(env)

  return [401, {"www-authenticate" => 'Basic realm=""'}, [""]] unless check_auth

  request_path_info = request.path_info == "" ? "/" : request.path_info
  if request.post?
    case request_path_info
    when %r{\/clear_view_tracking_file}
      clear_view_tracking_file
    when %r{\/clear_view_tracking}
      clear_view_tracking
    when %r{\/clear_file}
      clear_file
    when %r{\/clear}
      clear
    else
      [404, {"Content-Type" => "text/html"}, ["404 error!"]]
    end
  else
    case request_path_info
    when /.*\.(css|js|gif|png)/
      @static.call(env)
    when %r{\/settings}
      [200, {"Content-Type" => "text/html"}, [settings]]
    when %r{\/view_tracker_data}
      [200, {"Content-Type" => "text/json"}, [view_tracker_data]]
    when %r{\/view_tracker}
      [200, {"Content-Type" => "text/html"}, [view_tracker]]
    when %r{\/enriched_debug_data}
      [200, {"Content-Type" => "text/json"}, [enriched_debug_data]]
    when %r{\/debug_data}
      [200, {"Content-Type" => "text/json"}, [debug_data]]
    when %r{\/load_file_details}
      [200, {"Content-Type" => "text/json"}, [load_file_details]]
    when %r{\/$}
      [200, {"Content-Type" => "text/html"}, [index]]
    else
      [404, {"Content-Type" => "text/html"}, ["404 error!"]]
    end
  end
end
check_auth() click to toggle source
# File lib/coverband/reporters/web.rb, line 23
def check_auth
  return true unless Coverband.configuration.password

  # support rack 1.6.x and rack 2.0 (get_header)
  auth_header = request.respond_to?(:get_header) ? request.get_header("HTTP_AUTHORIZATION") : request.env["HTTP_AUTHORIZATION"]
  return unless auth_header

  Coverband.configuration.password == Base64.decode64(auth_header.split[1]).split(":")[1]
end
clear() click to toggle source
# File lib/coverband/reporters/web.rb, line 122
def clear
  if Coverband.configuration.web_enable_clear
    Coverband.configuration.store.clear!
    notice = "coverband coverage cleared"
  else
    notice = "web_enable_clear isnt enabled in your configuration"
  end
  [301, {"Location" => "#{base_path}?notice=#{notice}"}, []]
end
clear_file() click to toggle source
# File lib/coverband/reporters/web.rb, line 132
def clear_file
  if Coverband.configuration.web_enable_clear
    filename = request.params["filename"]
    Coverband.configuration.store.clear_file!(filename)
    notice = "coverage for file #{filename} cleared"
  else
    notice = "web_enable_clear isnt enabled in your configuration"
  end
  [301, {"Location" => "#{base_path}?notice=#{notice}"}, []]
end
clear_view_tracking() click to toggle source
# File lib/coverband/reporters/web.rb, line 143
def clear_view_tracking
  if Coverband.configuration.web_enable_clear
    tracker = Coverband::Collectors::ViewTracker.new(store: Coverband.configuration.store)
    tracker.reset_recordings
    notice = "view tracking reset"
  else
    notice = "web_enable_clear isnt enabled in your configuration"
  end
  [301, {"Location" => "#{base_path}/view_tracker?notice=#{notice}"}, []]
end
clear_view_tracking_file() click to toggle source
# File lib/coverband/reporters/web.rb, line 154
def clear_view_tracking_file
  if Coverband.configuration.web_enable_clear
    tracker = Coverband::Collectors::ViewTracker.new(store: Coverband.configuration.store)
    filename = request.params["filename"]
    tracker.clear_file!(filename)
    notice = "coverage for file #{filename} cleared"
  else
    notice = "web_enable_clear isnt enabled in your configuration"
  end
  [301, {"Location" => "#{base_path}/view_tracker?notice=#{notice}"}, []]
end
debug_data() click to toggle source
# File lib/coverband/reporters/web.rb, line 102
def debug_data
  Coverband.configuration.store.get_coverage_report.to_json
end
enriched_debug_data() click to toggle source
# File lib/coverband/reporters/web.rb, line 106
def enriched_debug_data
  Coverband::Reporters::HTMLReport.new(Coverband.configuration.store,
    static: false,
    base_path: base_path,
    notice: "",
    open_report: false).report_data
end
index() click to toggle source
# File lib/coverband/reporters/web.rb, line 76
def index
  notice = "<strong>Notice:</strong> #{Rack::Utils.escape_html(request.params["notice"])}<br/>"
  notice = request.params["notice"] ? notice : ""
  Coverband::Reporters::HTMLReport.new(Coverband.configuration.store,
    static: false,
    base_path: base_path,
    notice: notice,
    open_report: false).report
end
init_web() click to toggle source
# File lib/coverband/reporters/web.rb, line 16
def init_web
  full_path = Gem::Specification.find_by_name("coverband").full_gem_path
  @static = Rack::Static.new(self,
    root: File.expand_path("public", full_path),
    urls: [/.*\.css/, /.*\.js/, /.*\.gif/, /.*\.png/])
end
load_file_details() click to toggle source
# File lib/coverband/reporters/web.rb, line 114
def load_file_details
  filename = request.params["filename"]
  Coverband::Reporters::HTMLReport.new(Coverband.configuration.store,
    filename: filename,
    base_path: base_path,
    open_report: false).file_details
end
settings() click to toggle source
# File lib/coverband/reporters/web.rb, line 86
def settings
  Coverband::Utils::HTMLFormatter.new(nil, base_path: base_path).format_settings!
end
view_tracker() click to toggle source
# File lib/coverband/reporters/web.rb, line 90
def view_tracker
  notice = "<strong>Notice:</strong> #{Rack::Utils.escape_html(request.params["notice"])}<br/>"
  notice = request.params["notice"] ? notice : ""
  Coverband::Utils::HTMLFormatter.new(nil,
    notice: notice,
    base_path: base_path).format_view_tracker!
end
view_tracker_data() click to toggle source
# File lib/coverband/reporters/web.rb, line 98
def view_tracker_data
  Coverband::Collectors::ViewTracker.new(store: Coverband.configuration.store).as_json
end

Private Instance Methods

base_path() click to toggle source

This method should get the root mounted endpoint for example if the app is mounted like so: mount Coverband::Web, at: '/coverage' “/coverage/collect_coverage?” become: /coverage/ NOTE: DO NOT let standardrb `autofix` this to regex match %r{/.*/}.match?(request.path) ? request.path.match(“/.*/”) : “/” ^^ the above is NOT valid Ruby 2.3/2.4 even though rubocop / standard think it is

# File lib/coverband/reporters/web.rb, line 176
def base_path
  request.path =~ %r{\/.*\/} ? request.path.match("\/.*\/")[0] : "/"
end