Sha256: 091aa9384d40d2ecc3de85b05be52389297960ae7bd2a50c3f1a4d843ddcd7de

Contents?: true

Size: 1.59 KB

Versions: 7

Compression:

Stored size: 1.59 KB

Contents

require "lighthouse/preferences"
require "json"

module Lighthouse
  module Ruby
    class Builder

      attr_reader :response

      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 parsed_response
        get_test_scores(raw_response)
      end

      def raw_response
        JSON.parse(@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)
        @test_scores = { url: @url}
        @test_scores[:run_time] = Time.now
        @test_scores[:performance] = response.dig("categories", "performance" , "score").to_f * 100
        @test_scores[:accessibility] = response.dig("categories", "accessibility" , "score").to_f * 100
        @test_scores[:best_practices] = response.dig("categories", "best-practices" , "score").to_f * 100
        @test_scores[:seo] = response.dig("categories", "seo" , "score").to_f * 100
        @test_scores
      end

    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
lighthouse-ruby-0.2.5 lib/lighthouse/ruby/builder.rb
lighthouse-ruby-0.2.4 lib/lighthouse/ruby/builder.rb
lighthouse-ruby-0.2.3 lib/lighthouse/ruby/builder.rb
lighthouse-ruby-0.2.2 lib/lighthouse/ruby/builder.rb
lighthouse-ruby-0.2.1 lib/lighthouse/ruby/builder.rb
lighthouse-ruby-0.2.0 lib/lighthouse/ruby/builder.rb
lighthouse-ruby-0.1.6 lib/lighthouse/ruby/builder.rb