Sha256: 5e97f0b68be4aae299bc0b3e83bb98aa5d5efdbfd2cf1c6c3f9a1a3450965366

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

class StyleStats::Css
  class Fetch
    attr_accessor :stylesheets, :elements

    def initialize(path)
      self.stylesheets = []
      self.elements = []
      get(path)
    end

    private
    def get(path)
      if path =~ URI::regexp
        request(path)
      else
        self.stylesheets.push(open(path).read)
      end
    end

    def request(path)
      file = open(path)
      case file.content_type
      when 'text/css'
        self.stylesheets.push(file.read)
      when 'text/html'
        doc = Oga.parse_html(file.read)
        find_stylesheets(doc, path).each { |file| request(file) }
        self.elements = find_elements(doc)
      else
        raise StyleStats::ContentError.new
      end
    rescue SocketError
      raise StyleStats::RequestError.new
    end

    def find_elements(doc)
      doc.xpath('//style').map(&:text)
    end

    def find_stylesheets(doc, url)
      base = URI.parse(url)
      doc.xpath('//link[@rel="stylesheet"]').map do |node|
        uri = URI.parse(node.get("href"))
        uri.scheme = 'http' unless uri.scheme
        uri.host = base.host unless uri.host
        uri.port = base.port unless uri.port
        uri.to_s
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
style_stats-0.0.1 lib/style_stats/css/fetch.rb