Class: HtmlCompressor::Compressor
- Inherits:
-
Object
- Object
- HtmlCompressor::Compressor
- Defined in:
- lib/html_compressor/compressor.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Error, OptionError, RuntimeError
Constant Summary collapse
- VERSION =
"0.0.1"
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
-
.compressor_type ⇒ Object
:nodoc:.
-
.default_options ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#command ⇒ Object
:nodoc:.
-
#compress(stream_or_string) ⇒ Object
Compress a stream or string of code with HTML Compressor.
-
#initialize(options = {}) ⇒ Compressor
constructor
:nodoc:.
Constructor Details
#initialize(options = {}) ⇒ Compressor
:nodoc:
23 24 25 26 |
# File 'lib/html_compressor/compressor.rb', line 23 def initialize( = {}) #:nodoc: @options = self.class..merge() @command = [path_to_java, "-jar", path_to_jar_file, *(command_option_for_type + )] end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
13 14 15 |
# File 'lib/html_compressor/compressor.rb', line 13 def @options end |
Class Method Details
.compressor_type ⇒ Object
:nodoc:
19 20 21 |
# File 'lib/html_compressor/compressor.rb', line 19 def self.compressor_type #:nodoc: raise Error, "create a HtmlCompressor instead" end |
.default_options ⇒ Object
:nodoc:
15 16 17 |
# File 'lib/html_compressor/compressor.rb', line 15 def self. #:nodoc: { :charset => "utf-8", :line_break => nil } end |
Instance Method Details
#command ⇒ Object
:nodoc:
28 29 30 |
# File 'lib/html_compressor/compressor.rb', line 28 def command #:nodoc: @command.map { |word| Shellwords.escape(word) }.join(" ") end |
#compress(stream_or_string) ⇒ Object
Compress a stream or string of code with HTML Compressor. (A stream is any object that responds to read
and close
like an IO.) If a block is given, you can read the compressed code from the block’s argument. Otherwise, compress
returns a string of compressed code.
Example: Compress HTML
compressor = HtmlCompressor::HtmlCompressor.new
compressor.compress(<<-END_HTML)
<html>
<head>
</head>
<body>
fdgdfgf
</body>
</html>
END_HTML
# => "<html><head></head><body>fdgdfgf</body></html>"
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/html_compressor/compressor.rb', line 52 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 RuntimeError, "compression failed" end end if status.exitstatus.zero? output else raise RuntimeError, "compression failed" end end end |