class Guard::Brakeman

The Brakeman guard that gets notifications about the following Guard events: `start`, `stop`, `reload`, `run_all` and `run_on_changes`.

Public Class Methods

new(options = { }) click to toggle source
Calls superclass method
# File lib/guard/brakeman.rb, line 13
def initialize(options = { })
  super

  ::Brakeman.instance_variable_set(:@quiet, options[:quiet])

  if options[:skip_checks]
    options[:skip_checks] = options[:skip_checks].map do |val|
      # mimic Brakeman::set_options behavior
      val[0,5] == "Check" ? val : "Check" << val
    end
  end

  if options[:url_safe_methods]
    options[:url_safe_methods]=
      options[:url_safe_methods].map do |val|
         val.to_sym
    end
  end

  # chatty implies notifications
  options[:notifications] = true if options[:chatty]

  # TODO mixing the use of this attr, good to match?  Bad to couple?
  @options = {
    :notifications => true,
    :run_on_start => false,
    :chatty => false,
    :min_confidence => 2,
    :quiet => false
  }.merge!(options)
  @scanner_opts = ::Brakeman::set_options({:app_path => '.'}.merge(@options))
end

Public Instance Methods

run_all() click to toggle source

Gets called when all checks should be run.

@raise [:task_has_failed] when stop has failed

# File lib/guard/brakeman.rb, line 65
def run_all
  fail "no scanner opts (start not called?)!" if @scanner_opts.nil?
  tracker.run_checks
  ::Brakeman.filter_warnings tracker, @scanner_opts
  print_failed
  throw :task_has_failed if tracker.filtered_warnings.any?
end
run_on_changes(paths) click to toggle source

Gets called when watched paths and files have changes.

@param [Array<String>] paths the changed paths and files @raise [:task_has_failed] when stop has failed

# File lib/guard/brakeman.rb, line 78
def run_on_changes paths
  return run_all unless tracker.checks
  info "\n\nrescanning #{paths}, running all checks" unless options[:quiet]
  report = ::Brakeman::rescan(tracker, paths)
  print_changed(report)
  throw :task_has_failed if report.any_warnings?
end
start() click to toggle source

Gets called once when Guard starts.

@raise [:task_has_failed] when stop has failed

# File lib/guard/brakeman.rb, line 50
def start
  @scanner_opts = ::Brakeman::set_options({:app_path => '.'}.merge(@options))
  @options.merge!(@scanner_opts)

  if @options[:run_on_start]
    run_all
  elsif @options[:chatty]
    Guard::Compat::Notifier.notify("Brakeman is ready to work!", :title => "Brakeman started", :image => :pending)
  end
end

Private Instance Methods

decorate_warning(warning) click to toggle source
# File lib/guard/brakeman.rb, line 196
def decorate_warning(warning)
  color = case warning.confidence
  when 0
    :red
  when 1
    :yellow
  when 2
    :white
  end

  msg = ::Brakeman::Warning::TEXT_CONFIDENCE[warning.confidence]
  output =  Guard::Compat::UI.color(msg, color)
  output << " - #{warning.warning_type} - #{warning.message}"
  output << " near line #{warning.line}" if warning.line

  if path = relative_warning_path(warning)
    output << " in #{path}"
  end

  output << ": #{warning.format_code}" if warning.code
  output
end
info(message, color = :white) click to toggle source
# File lib/guard/brakeman.rb, line 186
def info(message, color = :white)
  Guard::Compat::UI.info(Guard::Compat::UI.color(message, color))
end
pluralize(count, singular, plural = nil) click to toggle source
# File lib/guard/brakeman.rb, line 177
def pluralize(count, singular, plural = nil)
  "#{count || 0} " + ((count == 1 || count =~ /^1(\.0+)?$/) ? singular : (plural || pluralize_word(singular)))
end
pluralize_word(singular) click to toggle source

try ActiveSupport or naive pluralize

# File lib/guard/brakeman.rb, line 182
def pluralize_word(singular)
  singular.respond_to?(:pluralize) ? singular.pluralize : singular + 's'
end
print_changed(report) click to toggle source
print_failed() click to toggle source
relative_warning_path(warning) click to toggle source
# File lib/guard/brakeman.rb, line 219
def relative_warning_path warning
  case
  when warning.file.nil? # This should never really happen
    nil
  when warning.respond_to?(:relative_path) # For Brakeman < 4.5.1
    warning.relative_path
  else # Must be new Brakeman::FilePath, Brakeman >= 4.5.1
    warning.file.relative
  end
end
tracker() click to toggle source
# File lib/guard/brakeman.rb, line 88
def tracker
  @tracker ||= ::Brakeman::Scanner.new(@scanner_opts).process
end
warning_info(warnings, color = :white) click to toggle source
# File lib/guard/brakeman.rb, line 190
def warning_info(warnings, color = :white)
  warnings.each do |warning|
    info(decorate_warning(warning))
  end
end
write_report() click to toggle source
# File lib/guard/brakeman.rb, line 169
def write_report
  @options[:output_files].each_with_index do |output_file, i|
    File.open output_file, "w" do |f|
      f.puts tracker.report.send(@options[:output_formats][i])
    end
  end
end