Sha256: 4813494d3804cd4e693c405d4e58b69a13d2f4dba51baa90d2c4c6d803480d48

Contents?: true

Size: 1.48 KB

Versions: 97

Compression:

Stored size: 1.48 KB

Contents

module CC
  module Analyzer
    class EngineOutput
      delegate :blank?, to: :raw_output

      def initialize(name, raw_output)
        @name = name
        @raw_output = raw_output
      end

      def issue?
        valid_with_type?("issue")
      end

      def measurement?
        valid_with_type?("measurement")
      end

      def as_issue
        Issue.new(name, raw_output)
      end

      def to_json
        if issue?
          as_issue.to_json
        elsif measurement?
          Measurement.new(name, raw_output).to_json
        end
      end

      def valid?
        valid_json? && validator && validator.valid?
      end

      def error
        if !valid_json?
          { message: "Invalid JSON", output: raw_output }
        elsif !validator.present?
          { message: "Unsupported document type", output: raw_output }
        else
          validator.error
        end
      end

      private

      attr_accessor :name, :raw_output

      def valid_json?
        parsed_output.present?
      end

      def valid_with_type?(type)
        parsed_output &&
          parsed_output["type"].present? &&
          parsed_output["type"].downcase == type
      end

      def parsed_output
        @parsed_output ||= JSON.parse(raw_output)
      rescue JSON::ParserError
        nil
      end

      def validator
        if issue?
          IssueValidator.new(parsed_output)
        elsif measurement?
          MeasurementValidator.new(parsed_output)
        end
      end
    end
  end
end

Version data entries

97 entries across 97 versions & 2 rubygems

Version Path
codeclimate-0.85.27 lib/cc/analyzer/engine_output.rb
codeclimate-0.85.26 lib/cc/analyzer/engine_output.rb
codeclimate-0.85.25 lib/cc/analyzer/engine_output.rb
codeclimate-0.85.24 lib/cc/analyzer/engine_output.rb
codeclimate-fede-0.85.54 lib/cc/analyzer/engine_output.rb
codeclimate-fede-0.85.53 lib/cc/analyzer/engine_output.rb
codeclimate-fede-0.85.52 lib/cc/analyzer/engine_output.rb
codeclimate-fede-0.85.51 lib/cc/analyzer/engine_output.rb
codeclimate-fede-0.85.50 lib/cc/analyzer/engine_output.rb
codeclimate-fede-0.85.49 lib/cc/analyzer/engine_output.rb
codeclimate-fede-0.85.48 lib/cc/analyzer/engine_output.rb
codeclimate-fede-0.85.47 lib/cc/analyzer/engine_output.rb
codeclimate-fede-0.85.46 lib/cc/analyzer/engine_output.rb
codeclimate-fede-0.85.45 lib/cc/analyzer/engine_output.rb
codeclimate-fede-0.85.44 lib/cc/analyzer/engine_output.rb
codeclimate-fede-0.85.43 lib/cc/analyzer/engine_output.rb
codeclimate-fede-0.85.42 lib/cc/analyzer/engine_output.rb
codeclimate-fede-0.85.41 lib/cc/analyzer/engine_output.rb
codeclimate-fede-0.85.40 lib/cc/analyzer/engine_output.rb
codeclimate-fede-0.85.39 lib/cc/analyzer/engine_output.rb