Sha256: 7337a1d9265e19329f3eef1f351471d595a441d09da8517938c8b5b901061765

Contents?: true

Size: 1.7 KB

Versions: 1

Compression:

Stored size: 1.7 KB

Contents

module Saddler
  module Reporter
    class Text
      VERSION = '0.2.0'
      def initialize(output)
        @output = output
      end

      def report(messages, _options = {})
        parsed_messages = parse_messages(messages)
        parsed_messages.each do |msg|
          severity = msg[:severity] && msg[:severity].upcase
          line = msg[:line]
          path = msg[:file]
          column = msg[:column]
          message = msg[:message]
          @output.puts "#{path}:#{line}:#{column} #{severity}: #{message}"
        end
      end

      def parse_messages(messages)
        # TODO: split to class or gem
        require 'rexml/document'
        parsed = []
        document = REXML::Document.new messages
        document.elements.each('/checkstyle/file') do |file_element|
          file_name = file_element.attribute('name') && file_element.attribute('name').value
          file_element.elements.each('error') do |error_element|
            line = error_element.attribute('line') && error_element.attribute('line').value.to_i
            column = error_element.attribute('column') && error_element.attribute('column').value.to_i
            severity = error_element.attribute('severity') && error_element.attribute('severity').value
            message = error_element.attribute('message') && error_element.attribute('message').value
            source = error_element.attribute('source') && error_element.attribute('source').value
            parsed << {
              file: file_name,
              line: line,
              column: column,
              severity: severity,
              message: message,
              source: source
            }
          end
        end
        parsed
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
saddler-reporter-text-0.2.0 lib/saddler/reporter/text.rb