Sha256: 55b36700b524184481a5cc79ebdc66eee5dcaf0a36dbfc30aa5c69ba4eae6893

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

require "lighthouse/preferences"
require "json"

module Lighthouse
  module Ruby
    class Builder

      def initialize(url)
        @url = url
        @runner = Lighthouse::Preferences.runner
        @cli = Lighthouse::Preferences.lighthouse_cli
        @port = Lighthouse::Preferences.remote_debugging_port
        @chrome_flags = Lighthouse::Preferences.chrome_flags
        @lighthouse_options = Lighthouse::Preferences.lighthouse_options
      end

      def run_test
        get_test_scores
      end

      private

      def options
        "'#{@url}'".tap do |builder|
          builder << ' --quiet'
          builder << ' --output=json'
          builder << " --port=#{@port}" if @port
          builder << " --#{@lighthouse_options}" if @lighthouse_options
          builder << " --chrome-flags='#{@chrome_flags}'" if @chrome_flags
        end.strip
      end

      def get_test_scores
        json_result = JSON.parse(@runner.call("#{@cli} #{options}"))

        @test_scores = { url: @url}
        @test_scores[:run_time] = Time.now
        @test_scores[:performance] = json_result.dig("categories", "performance" , "score") * 100
        @test_scores[:accessibility] = json_result.dig("categories", "accessibility" , "score") * 100
        @test_scores[:best_practices] = json_result.dig("categories", "best-practices" , "score") * 100
        @test_scores[:seo] = json_result.dig("categories", "seo" , "score") * 100
        @test_scores
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lighthouse-ruby-0.1.0 lib/lighthouse/ruby/builder.rb