magnetised-shine 0.4 → 0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README +2 -0
- data/lib/shine.rb +72 -5
- metadata +1 -1
data/LICENSE
CHANGED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Garry Hill <garry@magnetised.info>
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
20
|
+
|
data/README
CHANGED
data/lib/shine.rb
CHANGED
@@ -1,3 +1,23 @@
|
|
1
|
+
# Copyright (c) 2009 Garry Hill <garry@magnetised.info>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
1
21
|
require 'rubygems'
|
2
22
|
require 'net/http/post/multipart'
|
3
23
|
|
@@ -37,13 +57,14 @@ module Shine
|
|
37
57
|
def self.compress_files(filepaths, format=nil, options={})
|
38
58
|
filepaths = [filepaths] unless filepaths.is_a?(Array)
|
39
59
|
url = compress_url(format)
|
60
|
+
options = options.reject { |k, v| !v }
|
40
61
|
params = {}
|
41
62
|
begin
|
42
63
|
files = filepaths.map {|f| File.open(f) }
|
43
64
|
filepaths.each_with_index do |f, i|
|
44
65
|
params["file#{i.to_s.rjust(4, "0")}"] = UploadIO.new(f, MIME[format], filepaths[i])
|
45
66
|
end
|
46
|
-
req = Net::HTTP::Post::Multipart.new(url.path, params)
|
67
|
+
req = Net::HTTP::Post::Multipart.new(url.path, params.merge(options))
|
47
68
|
result = Net::HTTP.start(url.host, url.port) do |http|
|
48
69
|
http.request(req)
|
49
70
|
end
|
@@ -65,6 +86,7 @@ module Shine
|
|
65
86
|
def self.compress_string(source, format=nil, options={})
|
66
87
|
url = compress_url(format)
|
67
88
|
req = Net::HTTP::Post.new(url.path)
|
89
|
+
options = options.reject { |k, v| !v }
|
68
90
|
req.set_form_data({'source' => source}.merge(options))
|
69
91
|
result = Net::HTTP.start(url.host, url.port) do |http|
|
70
92
|
http.request(req)
|
@@ -90,13 +112,16 @@ module Shine
|
|
90
112
|
js
|
91
113
|
end
|
92
114
|
end
|
93
|
-
|
94
|
-
|
115
|
+
|
116
|
+
def self.file(f, options={})
|
117
|
+
self.files(f, options)
|
95
118
|
end
|
96
|
-
|
119
|
+
|
120
|
+
def self.files(input_files, options={})
|
121
|
+
input_files = [input_files] unless input_files.is_a?(Array)
|
97
122
|
input_files.flatten!
|
98
123
|
compressor = Shine::JS::Compressor.new(input_files)
|
99
|
-
compressor.compress
|
124
|
+
compressor.compress(options)
|
100
125
|
end
|
101
126
|
|
102
127
|
class Compressor
|
@@ -116,4 +141,46 @@ module Shine
|
|
116
141
|
end
|
117
142
|
end
|
118
143
|
end
|
144
|
+
module CSS
|
145
|
+
def self.in_place(input_file_path)
|
146
|
+
compressor = Shine::CSS::Compressor.new(input_file_path)
|
147
|
+
compressor.compress_in_place
|
148
|
+
end
|
149
|
+
|
150
|
+
def self.string(css, options={})
|
151
|
+
begin
|
152
|
+
Shine.compress_string(css, :css, options)
|
153
|
+
rescue CompressionError
|
154
|
+
js
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
def self.file(f, options={})
|
159
|
+
self.files(f, options)
|
160
|
+
end
|
161
|
+
|
162
|
+
def self.files(input_files, options={})
|
163
|
+
input_files = [input_files] unless input_files.is_a?(Array)
|
164
|
+
input_files.flatten!
|
165
|
+
compressor = Shine::CSS::Compressor.new(input_files)
|
166
|
+
compressor.compress(options)
|
167
|
+
end
|
168
|
+
|
169
|
+
class Compressor
|
170
|
+
def initialize(filepaths)
|
171
|
+
@filepaths = filepaths
|
172
|
+
end
|
173
|
+
|
174
|
+
def compress(options={})
|
175
|
+
Shine.compress_css(@filepaths, options)
|
176
|
+
end
|
177
|
+
|
178
|
+
def compress_in_place(options={})
|
179
|
+
@filepaths.each do |p|
|
180
|
+
c = Shine.compress_css(p, options)
|
181
|
+
File.open(p, 'w') { |f| f.write(c) }
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
119
186
|
end
|