clean_css 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in clean_css.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 SHIOYA, Hiromu
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # CleanCss
2
+
3
+ Ruby interface for clan-css (powered by node)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'clean_css'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install clean_css
18
+
19
+ ## Contributing
20
+
21
+ 1. Fork it
22
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
23
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
24
+ 4. Push to the branch (`git push origin my-new-feature`)
25
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+ desc "run spec"
5
+ RSpec::Core::RakeTask.new(:spec) do |t|
6
+ t.rspec_opts = ["-c", "-fs"]
7
+ end
data/clean_css.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'clean_css/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "clean_css"
8
+ gem.version = CleanCss::VERSION
9
+ gem.authors = ["SHIOYA, Hiromu"]
10
+ gem.email = ["kwappa.856@gmail.com"]
11
+ gem.description = %q{Ruby interface for clean-css (powered by node)}
12
+ gem.summary = %q{Ruby interface for clean-css (powered by node)}
13
+ gem.homepage = "https://github.com/kwappa/ruby-clean_css"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'popen4'
21
+ gem.add_development_dependency 'rspec'
22
+ end
@@ -0,0 +1,3 @@
1
+ class CleanCss
2
+ VERSION = "0.1.0"
3
+ end
data/lib/clean_css.rb ADDED
@@ -0,0 +1,57 @@
1
+ # -*- coding: utf-8 -*-
2
+ require "popen4"
3
+ require "clean_css/version"
4
+
5
+ class CleanCss
6
+ def initialize options = {}
7
+ @options = options
8
+ end
9
+
10
+ def command
11
+ @options[:command] || 'cleancss'
12
+ end
13
+
14
+ def compress(stream_or_string)
15
+ streamify(stream_or_string) do |stream|
16
+ output = true
17
+ status = POpen4.popen4(command, "b") do |stdout, stderr, stdin, pid|
18
+ begin
19
+ stdin.binmode
20
+ transfer(stream, stdin)
21
+
22
+ if block_given?
23
+ yield stdout
24
+ else
25
+ output = stdout.read
26
+ end
27
+
28
+ rescue Exception => e
29
+ raise StandardError, "compression failed"
30
+ end
31
+ end
32
+
33
+ if status.exitstatus.zero?
34
+ output
35
+ else
36
+ raise StandardError, "compression failed"
37
+ end
38
+ end
39
+ end
40
+
41
+ private
42
+ def streamify(stream_or_string)
43
+ if stream_or_string.respond_to?(:read)
44
+ yield stream_or_string
45
+ else
46
+ yield StringIO.new(stream_or_string.to_s)
47
+ end
48
+ end
49
+
50
+ def transfer(from_stream, to_stream)
51
+ while buffer = from_stream.read(4096)
52
+ to_stream.write(buffer)
53
+ end
54
+ from_stream.close
55
+ to_stream.close
56
+ end
57
+ end
@@ -0,0 +1,58 @@
1
+ # -*- coding: utf-8 -*-
2
+ $: << File.join(File.dirname(File.expand_path(__FILE__)), '..', 'lib')
3
+ require 'clean_css'
4
+
5
+ describe CleanCss do
6
+ let(:fixture_css) { <<-END_CSS
7
+ div.warning {
8
+ display: none;
9
+ }
10
+
11
+ div.error {
12
+ background: red;
13
+ color: white;
14
+ }
15
+
16
+ @media screen and (max-device-width: 640px) {
17
+ body { font-size: 90%; }
18
+ }
19
+ END_CSS
20
+ }
21
+
22
+ let(:compressed_css) {
23
+ 'div.warning{display:none}div.error{background:red;color:#fff}@media screen and (max-device-width: 640px){body{ font-size:90%}}'
24
+ }
25
+
26
+ describe '.initialize' do
27
+ context 'no options were given' do
28
+ subject { described_class.new }
29
+ its(:command) { should == 'cleancss' }
30
+ end
31
+ context 'option :command was given' do
32
+ let(:options) { { command: '/path/to/cleancss' } }
33
+ subject { described_class.new(options) }
34
+ its(:command) { should == options[:command] }
35
+ end
36
+ end
37
+
38
+ describe '#compress' do
39
+ context 'fixture css was given as string' do
40
+ subject { described_class.new.compress fixture_css }
41
+ it { should == compressed_css }
42
+ end
43
+
44
+ context 'fixture css was given as stream' do
45
+ subject { described_class.new.compress StringIO.new(fixture_css) }
46
+ it { should == compressed_css }
47
+ end
48
+
49
+ context 'fixture css was given as block' do
50
+ it 'yields an IO' do
51
+ described_class.new.compress(fixture_css) do |stream|
52
+ stream.should be_instance_of IO
53
+ stream.read.should == compressed_css
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: clean_css
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - SHIOYA, Hiromu
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: popen4
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Ruby interface for clean-css (powered by node)
47
+ email:
48
+ - kwappa.856@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - clean_css.gemspec
59
+ - lib/clean_css.rb
60
+ - lib/clean_css/version.rb
61
+ - spec/clean_css_spec.rb
62
+ homepage: https://github.com/kwappa/ruby-clean_css
63
+ licenses: []
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 1.8.24
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: Ruby interface for clean-css (powered by node)
86
+ test_files:
87
+ - spec/clean_css_spec.rb
88
+ has_rdoc: