class RubocopReporting::ToCSV

Public Class Methods

new(path_to_file = nil) click to toggle source
# File lib/rubocop_reporting.rb, line 9
def initialize(path_to_file = nil)
  @path_to_file = path_to_file
end

Public Instance Methods

accepted_formats() click to toggle source
# File lib/rubocop_reporting.rb, line 29
def accepted_formats
  ['.json']
end
generate() click to toggle source
# File lib/rubocop_reporting.rb, line 13
def generate
  raise FileNotFound if @path_to_file.nil?

  begin
    file = File.read(@path_to_file)
  rescue StandardError
    raise FileNotFound
  end

  unless accepted_formats.include? File.extname(@path_to_file)
    raise FileNotSupported
  end

  generate_csv_file(file)
end
generate_csv_file(file) click to toggle source
# File lib/rubocop_reporting.rb, line 33
def generate_csv_file(file)
  result = processing_json(file)
  CSV.open("#{Dir.pwd}/rubocop_reporting.csv", 'wb') do |csv|
    csv << result.keys
    csv << result.values
  end
end
processing_json(file) click to toggle source
# File lib/rubocop_reporting.rb, line 41
def processing_json(file)
  result = {}
  json = JSON.parse(file)

  json['files'].each do |json_file|
    json_file['offenses'].each do |offense|
      cop_name = offense['cop_name'].split('/')[0]
      result[cop_name] = result[cop_name].nil? ? 1 : (result[cop_name] + 1)
    end
  end

  result
end