Sha256: 232a4a96b1b4bd97f0f1cbaf0a80a3e899cf7c8c39d082b277cf7024da12fb00

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

require 'open-uri'

class Page
  attr_reader :url, :source, :name

  URL = /(?:https?:\/\/)(?:[\da-z\.-]+)\.(?:[a-z\.]{2,6})(?:[\/\w \.-]*)\/?/

  def initialize(url)
    @name   = Time.new.to_i.to_s + '_' + rand(9999).to_s
    @url    = URI.parse(url)
    @source = open(url).readlines
  end

  def to_s
    <<-EOS.gsub(/^\s+\|\s/, '')
          | http.page_start("#{name}")
          | http.request_batch({
          |   {"GET", "#{url}", auto_decompress=true}
          | })
          |
          | http.request_batch({
          | #{asset_include.join("\n")}
          | })
          |
          | http.page_end("#{name}")
          |
          |
    EOS
  end

  def assets
    matcher = /.*#{url.host}.*(js|css|png|jpg|jpeg|gif)/i
    @source.select { |line| URL.match(line)          }.
        map        { |line| URL.match(line)[0]       }.
        select     { |line| matcher.match(line)      }.
        uniq.
        sort
  end

  def asset_include
    assets.map do |url|
      '  {"GET", "' + url + '", auto_decompress=true},'
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
load_impact-0.0.2 lib/load_impact/page.rb
load_impact-0.0.1 lib/load_impact/page.rb