Sha256: f8a40d4df1b933fe209b9496c93e0f156e30054cdd1f19c150171fec130dde7d

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true
require 'cgi'
require 'json'
require 'validated_object'
require 'web_test/util'

module WebTest
  module BeFast
    class TestResult < ValidatedObject::Base
      attr_accessor :success, :score
      alias success? success

      validates :success, inclusion: [true, false]
      validates :score,   inclusion: 0..100
    end


    def self.parse(json:)
      response = JSON.parse(json)
      unless response.key?('ruleGroups')
        raise "Couldn't parse the PageSpeed response: #{response.inspect}"
      end
      score = response.fetch('ruleGroups').fetch('SPEED').fetch('score')
      { score: score }
    end

    def self.page_speed_score(url:)
      url_param = CGI.escape(WebTest::Util.make_url(url))
      key       = ENV['WEBSERVICE_MATCHER_INSIGHTS_KEY']
      if key.nil?
        raise 'be_fast requires the WEBSERVICE_MATCHER_INSIGHTS_KEY '\
              'environment variable to be set to a Google PageSpeed '\
              'Insights API key.'
      end
      endpoint  = 'https://www.googleapis.com/pagespeedonline/v2/runPagespeed'
      api_url   = "#{endpoint}?url=#{url_param}&screenshot=false&key=#{key}"
      response = Faraday.get(api_url)
      BeFast.parse(json: response.body).fetch(:score)
    end

    def self.test(url:)
      TestResult.new do |r|
        r.score   = BeFast.page_speed_score(url: url)
        r.success = r.score >= 85
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rspec-webservice_matchers-4.10.0 lib/web_test/be_fast.rb