Class: Guard::Brakeman

Inherits:
Plugin
  • Object
show all
Defined in:
lib/guard/brakeman.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(options = { }) ⇒ Brakeman

Returns a new instance of Brakeman.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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

Instance Method Details

#run_allObject

Gets called when all checks should be run.

Raises:

  • (:task_has_failed)

    when stop has failed



65
66
67
68
69
70
71
# 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) ⇒ Object

Gets called when watched paths and files have changes.

Parameters:

  • paths (Array<String>)

    the changed paths and files

Raises:

  • (:task_has_failed)

    when stop has failed



78
79
80
81
82
83
84
# 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

#startObject

Gets called once when Guard starts.

Raises:

  • (:task_has_failed)

    when stop has failed



50
51
52
53
54
55
56
57
58
59
# 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