Sha256: 41f1bd2d7b4289989475fb43f9491337d13617d91f8f3f1ce1c154b7cd9147d4

Contents?: true

Size: 1.35 KB

Versions: 4

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true
require 'cgi'
require 'json'
require 'rspec/webservice_matchers/util'

module RSpec
  module WebserviceMatchers
    module BeFast
      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(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

      RSpec::Matchers.define :be_fast do
        score = nil

        match do |url|
          score = BeFast.page_speed_score(url: url)
          score >= 85
        end

        failure_message do
          "PageSpeed score is #{score}/100."
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rspec-webservice_matchers-4.8.1 lib/rspec/webservice_matchers/be_fast.rb
rspec-webservice_matchers-4.8.0 lib/rspec/webservice_matchers/be_fast.rb
rspec-webservice_matchers-4.7.0 lib/rspec/webservice_matchers/be_fast.rb
rspec-webservice_matchers-4.6.0 lib/rspec/webservice_matchers/be_fast.rb