Class: RemoteCss::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/remote_css/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
12
# File 'lib/remote_css/client.rb', line 7

def initialize(options={})
  validate_options!(options)
  @options = options
  @options[:minify] = true unless @options.has_key?(:minify)
  @options[:verbose] = false unless @options.has_key?(:verbose)
end

Instance Attribute Details

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/remote_css/client.rb', line 5

def url
  @url
end

Instance Method Details

#cssObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/remote_css/client.rb', line 14

def css
  verbose("Reading HTML")
  doc = Nokogiri::HTML(@options[:body] || open(@options[:url]))
  verbose("Reading inline styles")
  styles = []
  inline_styles = doc.css("style").map{|s| styles << {:source => :inline, :style => s.text.strip} }
  threads = []
  remote_styles = doc.css("link[rel='stylesheet'][href]").each do |c|
    verbose("Loading #{c.attr("href")}")
    threads << Thread.new do
      styles << {:source => URI.join(@options[:url], c.attr("href")), :style => open(URI.join(@options[:url], c.attr("href"))).read.strip}
    end
  end

  threads.each { |thr| thr.join }

  style = styles.map{|s| "/* #{s[:source]} */\n\n#{s[:style]}" }.join("\n\n")

  if @options[:minify]
    CSSminify.compress(style)
  else
    style
  end

end