Class: Brakeman::Warning
Overview
The Warning class stores information about warnings
Constant Summary collapse
- TEXT_CONFIDENCE =
[ "High", "Medium", "Weak" ]
Instance Attribute Summary collapse
-
#called_from ⇒ Object
readonly
Returns the value of attribute called_from.
-
#check ⇒ Object
readonly
Returns the value of attribute check.
-
#class ⇒ Object
readonly
Returns the value of attribute class.
-
#code ⇒ Object
Returns the value of attribute code.
-
#confidence ⇒ Object
readonly
Returns the value of attribute confidence.
-
#context ⇒ Object
Returns the value of attribute context.
-
#controller ⇒ Object
readonly
Returns the value of attribute controller.
-
#file ⇒ Object
Returns the value of attribute file.
-
#line ⇒ Object
readonly
Returns the value of attribute line.
-
#message ⇒ Object
Returns the value of attribute message.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#template ⇒ Object
readonly
Returns the value of attribute template.
-
#user_input ⇒ Object
readonly
Returns the value of attribute user_input.
-
#warning_set ⇒ Object
readonly
Returns the value of attribute warning_set.
-
#warning_type ⇒ Object
readonly
Returns the value of attribute warning_type.
Instance Method Summary collapse
- #eql?(other_warning) ⇒ Boolean
-
#format_code ⇒ Object
Return String of the code output from the OutputProcessor and stripped of newlines and tabs.
-
#format_message ⇒ Object
Return formatted warning message.
-
#format_user_input ⇒ Object
Return String of the user input formatted and stripped of newlines and tabs.
- #hash ⇒ Object
-
#initialize(options = {}) ⇒ Warning
constructor
options[:result]
can be a result from Tracker#find_call. - #link ⇒ Object
- #to_hash ⇒ Object
- #to_json ⇒ Object
-
#to_row(type = :warning) ⇒ Object
Generates a hash suitable for inserting into a table.
- #to_s ⇒ Object
-
#view_name ⇒ Object
Returns name of a view, including where it was rendered from.
Constructor Details
#initialize(options = {}) ⇒ Warning
options[:result]
can be a result from Tracker#find_call. Otherwise, it can be nil
.
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 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/brakeman/warning.rb', line 13 def initialize = {} @view_name = nil [:called_from, :check, :class, :code, :confidence, :controller, :file, :line, :link_path, :message, :method, :model, :template, :user_input, :warning_set, :warning_type].each do |option| self.instance_variable_set("@#{option}", [option]) end result = [:result] if result if result[:location][0] == :template #template result @template ||= result[:location][1] @code ||= result[:call] else @class ||= result[:location][1] @method ||= result[:location][2] @code ||= result[:call] end end if not @line if @user_input and @user_input.respond_to? :line @line = @user_input.line elsif @code and @code.respond_to? :line @line = @code.line end end unless @warning_set if self.model @warning_set = :model elsif self.template @warning_set = :template @called_from = self.template[:caller] elsif self.controller @warning_set = :controller else @warning_set = :warning end end @format_message = nil @row = nil end |
Instance Attribute Details
#called_from ⇒ Object (readonly)
Returns the value of attribute called_from.
5 6 7 |
# File 'lib/brakeman/warning.rb', line 5 def called_from @called_from end |
#check ⇒ Object (readonly)
Returns the value of attribute check.
5 6 7 |
# File 'lib/brakeman/warning.rb', line 5 def check @check end |
#class ⇒ Object (readonly)
Returns the value of attribute class.
5 6 7 |
# File 'lib/brakeman/warning.rb', line 5 def class @class end |
#code ⇒ Object
Returns the value of attribute code.
8 9 10 |
# File 'lib/brakeman/warning.rb', line 8 def code @code end |
#confidence ⇒ Object (readonly)
Returns the value of attribute confidence.
5 6 7 |
# File 'lib/brakeman/warning.rb', line 5 def confidence @confidence end |
#context ⇒ Object
Returns the value of attribute context.
8 9 10 |
# File 'lib/brakeman/warning.rb', line 8 def context @context end |
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
5 6 7 |
# File 'lib/brakeman/warning.rb', line 5 def controller @controller end |
#file ⇒ Object
Returns the value of attribute file.
8 9 10 |
# File 'lib/brakeman/warning.rb', line 8 def file @file end |
#line ⇒ Object (readonly)
Returns the value of attribute line.
5 6 7 |
# File 'lib/brakeman/warning.rb', line 5 def line @line end |
#message ⇒ Object
Returns the value of attribute message.
8 9 10 |
# File 'lib/brakeman/warning.rb', line 8 def @message end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
5 6 7 |
# File 'lib/brakeman/warning.rb', line 5 def method @method end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
5 6 7 |
# File 'lib/brakeman/warning.rb', line 5 def model @model end |
#template ⇒ Object (readonly)
Returns the value of attribute template.
5 6 7 |
# File 'lib/brakeman/warning.rb', line 5 def template @template end |
#user_input ⇒ Object (readonly)
Returns the value of attribute user_input.
5 6 7 |
# File 'lib/brakeman/warning.rb', line 5 def user_input @user_input end |
#warning_set ⇒ Object (readonly)
Returns the value of attribute warning_set.
5 6 7 |
# File 'lib/brakeman/warning.rb', line 5 def warning_set @warning_set end |
#warning_type ⇒ Object (readonly)
Returns the value of attribute warning_type.
5 6 7 |
# File 'lib/brakeman/warning.rb', line 5 def warning_type @warning_type end |
Instance Method Details
#eql?(other_warning) ⇒ Boolean
63 64 65 |
# File 'lib/brakeman/warning.rb', line 63 def eql? other_warning self.hash == other_warning.hash end |
#format_code ⇒ Object
Return String of the code output from the OutputProcessor and stripped of newlines and tabs.
79 80 81 |
# File 'lib/brakeman/warning.rb', line 79 def format_code Brakeman::OutputProcessor.new.format(self.code).gsub(/(\t|\r|\n)+/, " ") end |
#format_message ⇒ Object
Return formatted warning message
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/brakeman/warning.rb', line 90 def return @format_message if @format_message @format_message = self..dup if self.line @format_message << " near line #{self.line}" end if self.code @format_message << ": #{format_code}" end @format_message end |
#format_user_input ⇒ Object
Return String of the user input formatted and stripped of newlines and tabs.
85 86 87 |
# File 'lib/brakeman/warning.rb', line 85 def format_user_input Brakeman::OutputProcessor.new.format(self.user_input).gsub(/(\t|\r|\n)+/, " ") end |
#hash ⇒ Object
59 60 61 |
# File 'lib/brakeman/warning.rb', line 59 def hash self.to_s.hash end |
#link ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/brakeman/warning.rb', line 106 def link return @link if @link if @link_path if @link_path.start_with? "http" @link = @link_path else @link = "http://brakemanscanner.org/docs/warning_types/#{@link_path}" end else warning_path = self.warning_type.to_s.downcase.gsub(/\s+/, '_') + "/" @link = "http://brakemanscanner.org/docs/warning_types/#{warning_path}" end @link end |
#to_hash ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/brakeman/warning.rb', line 153 def to_hash case @warning_set when :template location = { :type => :template, :template => self.view_name } when :model location = { :type => :model, :model => self.model } when :controller location = { :type => :controller, :controller => self.controller } when :warning if self.class location = { :type => :method, :class => self.class, :method => self.method } else location = nil end end { :warning_type => self.warning_type, :message => self., :file => self.file, :line => self.line, :link => self.link, :code => (@code && self.format_code), :location => location, :user_input => (@user_input && self.format_user_input), :confidence => TEXT_CONFIDENCE[self.confidence] } end |
#to_json ⇒ Object
181 182 183 |
# File 'lib/brakeman/warning.rb', line 181 def to_json MultiJson.dump self.to_hash end |
#to_row(type = :warning) ⇒ Object
Generates a hash suitable for inserting into a table
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/brakeman/warning.rb', line 124 def to_row type = :warning @row = { "Confidence" => self.confidence, "Warning Type" => self.warning_type.to_s, "Message" => self. } case type when :template @row["Template"] = self.view_name.to_s when :model @row["Model"] = self.model.to_s when :controller @row["Controller"] = self.controller.to_s when :warning @row["Class"] = self.class.to_s @row["Method"] = self.method.to_s end @row end |
#to_s ⇒ Object
144 145 146 147 148 149 150 151 |
# File 'lib/brakeman/warning.rb', line 144 def to_s output = "(#{TEXT_CONFIDENCE[self.confidence]}) #{self.warning_type} - #{self.}" output << " near line #{self.line}" if self.line output << " in #{self.file}" if self.file output << ": #{self.format_code}" if self.code output end |
#view_name ⇒ Object
Returns name of a view, including where it was rendered from
68 69 70 71 72 73 74 75 |
# File 'lib/brakeman/warning.rb', line 68 def view_name return @view_name if @view_name if called_from @view_name = "#{template[:name]} (#{called_from.last})" else @view_name = template[:name] end end |