Sha256: 52e196640a04c2900add6c3d6404e825a99f2e761bcfd9f672c5c88ca22e497d

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

require 'net/https'
require 'uri'

module PageSpeed
  class Request
    PAGESPEED_API_URL = 'https://www.googleapis.com/pagespeedonline/v1/runPagespeed'

    attr_accessor :url, :api_key

    def initialize(url, api_key, strategy)
      @url = url =~ /^https?:\/\// ? url : ('http://' + url )
      @api_key = api_key
      @strategy = strategy
      @uri = build_request_uri
    end

    def pagespeed
      http = Net::HTTP.new(@uri.host, @uri.port)
      http.use_ssl = true
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE

      request = Net::HTTP::Get.new(@uri.request_uri)
      response = http.request(request)

      if response.code.to_i == 200
        PageSpeed::Parser.parse(response.body)
      else
        status_error(response)
      end

    rescue Exception => e
      puts e.message
      puts e.backtrace.join("\n")
      puts "\033[31mUh oh, didn't work. Maybe the host is down or the url is wrong... or perhaps google is down :("
      exit
    end

    def status_error(response)
      puts "#{response.code}"
      exit
    end

    private

    def build_request_uri
      uri = URI.parse(PAGESPEED_API_URL)
      uri.query = "url=#{@url}&strategy=#{@strategy}&key=#{@api_key}"
      uri
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pagespeed-0.1.0 lib/pagespeed/request.rb