Sha256: 2280164895d00fc060fff203f20730d1dea2cbca4df54e0d06ea02ee41a78d0b

Contents?: true

Size: 1.36 KB

Versions: 3

Compression:

Stored size: 1.36 KB

Contents

require 'crack'
require_relative '../domain/checkstyle'
require_relative '../domain/bad_file'

module PreCommit
  module Message
    ##
    # Responsible for extract error messages from terminal output
    class Extractor
      ##
      # Extract data from a XML formatted +terminal_output+
      #
      # @param terminal_output [String] XML formatted terminal ouput
      # @return [Domain::Checkstyle] The checkstyle
      def extract(terminal_output)
        if blank?(terminal_output) ||
           blank?(xml_content_of(terminal_output))
          return Domain::Checkstyle.good
        end

        xml_data = Crack::XML.parse(xml_content_of(terminal_output))
        files = xml_data['checkstyle']['file']

        Domain::Checkstyle.new(extract_bad_file(files))
      end

      private

      def xml_content_of(raw_output)
        raw_output[/<(.*)>/m]
      end

      def extract_bad_file(xml_files)
        return [bad_file(xml_files)] unless xml_files.is_a? Array
        xml_files.map { |e| bad_file(e) }
      end

      def bad_file(file)
        Domain::BadFile.new(file['name'], extract_errors(file))
      end

      def extract_errors(file)
        return [] if blank? file['error']
        return [file['error']] unless file['error'].is_a? Array
        file['error']
      end

      def blank?(value)
        value.nil? || value.empty?
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
java-checkstyle-1.1.0 lib/plugins/pre_commit/message/extractor.rb
java-checkstyle-1.0.5 lib/plugins/pre-commit/message/extractor.rb
java-checkstyle-1.0.4 lib/plugins/pre-commit/message/extractor.rb