Sha256: fd7e1f5aeb2b73d883fd507302ec3edb3365037ef00299f5ba5998927eae5bd7

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

# typed: strict

require "json"
require "sorbet_progress/error"
require "sorbet_progress/metric"
require "sorbet_progress/metrics"

module SorbetProgress
  # Parse Sorbet's JSON metrics file.
  class Parser
    extend T::Sig

    sig { params(json: String).returns(Metrics) }
    def parse(json)
      transform(parse_json(json))
    end

    private

    sig { params(json: String).returns(Hash) }
    def parse_json(json)
      JSON.parse(json)
    rescue JSON::ParserError => e
      raise Error.new(3, "Metrics file is not valid JSON: " + e.message)
    end

    sig { params(parsed: Hash).returns(Metrics) }
    def transform(parsed)
      Metrics.new(
        parsed.
          fetch("metrics").
          map { |metric|
            next unless metric.key?("value")
            Metric.new(
              metric.fetch("name"),
              metric.fetch("value")
            )
          }.
          compact
      )
    rescue KeyError => e
      raise Error.new(4, "Expected file to have key: metrics: " + e.message)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sorbet-progress-0.2.4 lib/sorbet_progress/parser.rb
sorbet-progress-0.2.3 lib/sorbet_progress/parser.rb