Module: Shine
Defined Under Namespace
Modules: CSS, JS
Classes: CompressionError
Constant Summary
collapse
- MIME =
{
:js => "text/javascript",
:css => "text/css"
}
Class Method Summary
collapse
Class Method Details
.compress_css(filepaths, options = {}) ⇒ Object
53
54
55
|
# File 'lib/shine.rb', line 53
def self.compress_css(filepaths, options={})
compress_files(filepaths, :css, options)
end
|
.compress_files(filepaths, format = nil, options = {}) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/shine.rb', line 57
def self.compress_files(filepaths, format=nil, options={})
filepaths = [filepaths] unless filepaths.is_a?(Array)
url = compress_url(format)
options = options.reject { |k, v| !v }
params = {}
begin
files = filepaths.map {|f| File.open(f) }
filepaths.each_with_index do |f, i|
params["file#{i.to_s.rjust(4, "0")}"] = UploadIO.new(f, MIME[format], filepaths[i])
end
req = Net::HTTP::Post::Multipart.new(url.path, params.merge(options))
result = Net::HTTP.start(url.host, url.port) do |http|
http.request(req)
end
case result
when Net::HTTPSuccess
result.body
else
concatenate_files(files)
end
ensure
files.each { |f| f.close rescue nil }
end
end
|
.compress_js(filepaths, options = {}) ⇒ Object
49
50
51
|
# File 'lib/shine.rb', line 49
def self.compress_js(filepaths, options={})
compress_files(filepaths, :js, options)
end
|
.compress_string(source, format = nil, options = {}) ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/shine.rb', line 86
def self.compress_string(source, format=nil, options={})
url = compress_url(format)
req = Net::HTTP::Post.new(url.path)
options = options.reject { |k, v| !v }
req.set_form_data({'source' => source}.merge(options))
result = Net::HTTP.start(url.host, url.port) do |http|
http.request(req)
end
case result
when Net::HTTPSuccess
result.body
else
source
end
end
|
.compress_url(format = :js) ⇒ Object
44
45
46
47
|
# File 'lib/shine.rb', line 44
def self.compress_url(format=:js)
URI.parse("#{server}/#{format}")
end
|
.concatenate_files(files) ⇒ Object
82
83
84
|
# File 'lib/shine.rb', line 82
def self.concatenate_files(files)
files.map {|f| f.read }.join("\n")
end
|
.default_server ⇒ Object
33
34
35
|
# File 'lib/shine.rb', line 33
def self.default_server
"http://shine.magnetised.info"
end
|
.server ⇒ Object
36
37
38
|
# File 'lib/shine.rb', line 36
def self.server
@_server || default_server
end
|
.server=(url) ⇒ Object
40
41
42
|
# File 'lib/shine.rb', line 40
def self.server=(url)
@_server = url
end
|