Sha256: ba30188a0c99aa3888ae53f217d6b15b68914301be889068e4391956d67aefc5

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 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 execute
        @response = @runner.call("#{@cli} #{options}")
      end

      def test_scores
        get_test_scores(@response)
      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(response)
        json_result = JSON.parse(response)
        @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

2 entries across 2 versions & 1 rubygems

Version Path
lighthouse-ruby-0.1.3 lib/lighthouse/ruby/builder.rb
lighthouse-ruby-0.1.1 lib/lighthouse/ruby/builder.rb