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-fede-0.85.38 lib/cc/analyzer/engine_output.rb
codeclimate-fede-0.85.37 lib/cc/analyzer/engine_output.rb
codeclimate-fede-0.85.36 lib/cc/analyzer/engine_output.rb
codeclimate-fede-0.85.35 lib/cc/analyzer/engine_output.rb
codeclimate-fede-0.85.33 lib/cc/analyzer/engine_output.rb
codeclimate-fede-0.85.32 lib/cc/analyzer/engine_output.rb
codeclimate-fede-0.85.31 lib/cc/analyzer/engine_output.rb
codeclimate-fede-0.85.30 lib/cc/analyzer/engine_output.rb
codeclimate-fede-0.85.21 lib/cc/analyzer/engine_output.rb
codeclimate-fede-0.85.24 lib/cc/analyzer/engine_output.rb
codeclimate-fede-0.85.23 lib/cc/analyzer/engine_output.rb
codeclimate-0.85.23 lib/cc/analyzer/engine_output.rb
codeclimate-0.85.22 lib/cc/analyzer/engine_output.rb
codeclimate-0.85.21 lib/cc/analyzer/engine_output.rb
codeclimate-0.85.19 lib/cc/analyzer/engine_output.rb
codeclimate-0.85.18 lib/cc/analyzer/engine_output.rb
codeclimate-0.85.17 lib/cc/analyzer/engine_output.rb
codeclimate-0.85.15 lib/cc/analyzer/engine_output.rb
codeclimate-0.85.14 lib/cc/analyzer/engine_output.rb
codeclimate-0.85.13 lib/cc/analyzer/engine_output.rb