Class: CleanCss

Inherits:
Object
  • Object
show all
Defined in:
lib/clean_css.rb,
lib/clean_css/version.rb

Constant Summary collapse

VERSION =
"0.1.1"

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CleanCss

Returns a new instance of CleanCss.



6
7
8
# File 'lib/clean_css.rb', line 6

def initialize options = {}
  @options = options
end

Instance Method Details

#commandObject



10
11
12
# File 'lib/clean_css.rb', line 10

def command
  @options[:command] || 'cleancss'
end

#compress(stream_or_string) ⇒ Object



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
39
# File 'lib/clean_css.rb', line 14

def compress(stream_or_string)
  streamify(stream_or_string) do |stream|
    output = true
    status = POpen4.popen4(command, "b") do |stdout, stderr, stdin, pid|
      begin
        stdin.binmode
        transfer(stream, stdin)

        if block_given?
          yield stdout
        else
          output = stdout.read
        end

      rescue Exception => e
        raise StandardError, "compression failed"
      end
    end

    if status.exitstatus.zero?
      output
    else
      raise StandardError, "compression failed"
    end
  end
end