magnetised-shine 0.3 → 0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/bin/compress +2 -3
- data/lib/shine.rb +93 -18
- metadata +1 -1
data/bin/compress
CHANGED
data/lib/shine.rb
CHANGED
|
@@ -2,42 +2,117 @@ require 'rubygems'
|
|
|
2
2
|
require 'net/http/post/multipart'
|
|
3
3
|
|
|
4
4
|
module Shine
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
class CompressionError < Exception; end
|
|
6
|
+
|
|
7
|
+
MIME = {
|
|
8
|
+
:js => "text/javascript",
|
|
9
|
+
:css => "text/css"
|
|
10
|
+
}
|
|
11
|
+
@_server = nil
|
|
12
|
+
|
|
13
|
+
def self.default_server
|
|
14
|
+
"http://shine.magnetised.info"
|
|
15
|
+
end
|
|
16
|
+
def self.server
|
|
17
|
+
@_server || default_server
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def self.server=(url)
|
|
21
|
+
@_server = url
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def self.compress_url(format=:js)
|
|
25
|
+
# URI.parse("http://shine.magnetised.info/#{format}")
|
|
26
|
+
URI.parse("#{server}/#{format}")
|
|
7
27
|
end
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
28
|
+
|
|
29
|
+
def self.compress_js(filepaths, options={})
|
|
30
|
+
compress_files(filepaths, :js, options)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def self.compress_css(filepaths, options={})
|
|
34
|
+
compress_files(filepaths, :css, options)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def self.compress_files(filepaths, format=nil, options={})
|
|
38
|
+
filepaths = [filepaths] unless filepaths.is_a?(Array)
|
|
39
|
+
url = compress_url(format)
|
|
40
|
+
params = {}
|
|
41
|
+
begin
|
|
42
|
+
files = filepaths.map {|f| File.open(f) }
|
|
43
|
+
filepaths.each_with_index do |f, i|
|
|
44
|
+
params["file#{i.to_s.rjust(4, "0")}"] = UploadIO.new(f, MIME[format], filepaths[i])
|
|
45
|
+
end
|
|
46
|
+
req = Net::HTTP::Post::Multipart.new(url.path, params)
|
|
12
47
|
result = Net::HTTP.start(url.host, url.port) do |http|
|
|
13
48
|
http.request(req)
|
|
14
49
|
end
|
|
50
|
+
case result
|
|
51
|
+
when Net::HTTPSuccess
|
|
52
|
+
result.body
|
|
53
|
+
else
|
|
54
|
+
concatenate_files(files)
|
|
55
|
+
end
|
|
56
|
+
ensure
|
|
57
|
+
files.each { |f| f.close rescue nil }
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def self.concatenate_files(files)
|
|
62
|
+
files.map {|f| f.read }.join("\n")
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def self.compress_string(source, format=nil, options={})
|
|
66
|
+
url = compress_url(format)
|
|
67
|
+
req = Net::HTTP::Post.new(url.path)
|
|
68
|
+
req.set_form_data({'source' => source}.merge(options))
|
|
69
|
+
result = Net::HTTP.start(url.host, url.port) do |http|
|
|
70
|
+
http.request(req)
|
|
71
|
+
end
|
|
72
|
+
case result
|
|
73
|
+
when Net::HTTPSuccess
|
|
15
74
|
result.body
|
|
75
|
+
else
|
|
76
|
+
source
|
|
16
77
|
end
|
|
17
78
|
end
|
|
18
79
|
|
|
19
80
|
module JS
|
|
20
|
-
def self.
|
|
21
|
-
compressor = Shine::JS::Compressor.new(
|
|
22
|
-
compressor.
|
|
81
|
+
def self.in_place(input_file_path)
|
|
82
|
+
compressor = Shine::JS::Compressor.new(input_file_path)
|
|
83
|
+
compressor.compress_in_place
|
|
23
84
|
end
|
|
24
85
|
|
|
25
|
-
def self.
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
86
|
+
def self.string(js, options={})
|
|
87
|
+
begin
|
|
88
|
+
Shine.compress_string(js, :js, options)
|
|
89
|
+
rescue CompressionError
|
|
90
|
+
js
|
|
30
91
|
end
|
|
31
|
-
|
|
92
|
+
end
|
|
93
|
+
def self.file(f)
|
|
94
|
+
self.files([f])
|
|
95
|
+
end
|
|
96
|
+
def self.files(*input_files)
|
|
97
|
+
input_files.flatten!
|
|
98
|
+
compressor = Shine::JS::Compressor.new(input_files)
|
|
99
|
+
compressor.compress
|
|
32
100
|
end
|
|
33
101
|
|
|
34
102
|
class Compressor
|
|
35
|
-
def initialize(
|
|
36
|
-
@
|
|
103
|
+
def initialize(filepaths)
|
|
104
|
+
@filepaths = filepaths
|
|
37
105
|
end
|
|
38
106
|
|
|
39
107
|
def compress(options={})
|
|
40
|
-
Shine.
|
|
108
|
+
Shine.compress_js(@filepaths, options)
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def compress_in_place(options={})
|
|
112
|
+
@filepaths.each do |p|
|
|
113
|
+
c = Shine.compress_js(p, options)
|
|
114
|
+
File.open(p, 'w') { |f| f.write(c) }
|
|
115
|
+
end
|
|
41
116
|
end
|
|
42
117
|
end
|
|
43
118
|
end
|