Parent

Class/Module Index [+]

Quicksearch

Brakeman::Warning

The Warning class stores information about warnings

Constants

TEXT_CONFIDENCE

Attributes

called_from[R]
check[R]
class[R]
code[RW]
confidence[R]
context[RW]
controller[R]
file[RW]
line[R]
message[RW]
method[R]
model[R]
template[R]
user_input[R]
warning_set[R]
warning_type[R]

Public Class Methods

new(options = {}) click to toggle source

+options+ can be a result from Tracker#find_call. Otherwise, it can be nil.

# File lib/brakeman/warning.rb, line 13
def initialize options = {}
  @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}", options[option])
  end

  result = options[: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

Public Instance Methods

eql?(other_warning) click to toggle source
# File lib/brakeman/warning.rb, line 63
def eql? other_warning
  self.hash == other_warning.hash
end
format_code(strip = true) click to toggle source

Return String of the code output from the OutputProcessor and stripped of newlines and tabs.

# File lib/brakeman/warning.rb, line 79
def format_code strip = true
  format_ruby self.code, strip
end
format_message() click to toggle source

Return formatted warning message

# File lib/brakeman/warning.rb, line 90
def format_message
  return @format_message if @format_message

  @format_message = self.message.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(strip = true) click to toggle source

Return String of the user input formatted and stripped of newlines and tabs.

# File lib/brakeman/warning.rb, line 85
def format_user_input strip = true
  format_ruby self.user_input, strip
end
hash() click to toggle source
# File lib/brakeman/warning.rb, line 59
def hash
  self.to_s.hash
end
to_hash() click to toggle source
# 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.message,
    :file => self.file,
    :line => self.line,
    :link => self.link,
    :code => (@code && self.format_code(false)),
    :location => location,
    :user_input => (@user_input && self.format_user_input(false)),
    :confidence => TEXT_CONFIDENCE[self.confidence]
  }
end
to_json() click to toggle source
# File lib/brakeman/warning.rb, line 181
def to_json
  MultiJson.dump self.to_hash
end
to_row(type = :warning) click to toggle source

Generates a hash suitable for inserting into a table

# 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.format_message }

  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() click to toggle source
# File lib/brakeman/warning.rb, line 144
def to_s
 output =  "(#{TEXT_CONFIDENCE[self.confidence]}) #{self.warning_type} - #{self.message}"
 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() click to toggle source

Returns name of a view, including where it was rendered from

# 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

[Validate]

Generated with the Darkfish Rdoc Generator 2.