magnetised-shine 0.3 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/bin/compress +2 -3
  2. data/lib/shine.rb +93 -18
  3. metadata +1 -1
data/bin/compress CHANGED
@@ -2,8 +2,7 @@
2
2
 
3
3
  $:.unshift(File.join(File.dirname(__FILE__), "../lib"))
4
4
 
5
- require 'shrimp'
5
+ require 'shine'
6
6
 
7
- js = Shrimp::JS.compress_all(ARGV)
7
+ puts Shine::JS.files(ARGV)
8
8
 
9
- puts js.join("\n")
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
- def self.compress_url
6
- URI.parse('http://shine.magnetised.info/compress')
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
- def self.compress_file(filepath, params={})
9
- url = compress_url
10
- File.open(filepath) do |file|
11
- req = Net::HTTP::Post::Multipart.new(url.path, 'file' => UploadIO.new(file, 'text/javascript', filepath))
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.compress(input_file)
21
- compressor = Shine::JS::Compressor.new(input_file)
22
- compressor.compress
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.compress_all(input_files)
26
- compressors = input_files.map {|f| Shine::JS::Compressor.new(f)}
27
- compressed = []
28
- compressors.each do |c|
29
- compressed << c.compress
86
+ def self.string(js, options={})
87
+ begin
88
+ Shine.compress_string(js, :js, options)
89
+ rescue CompressionError
90
+ js
30
91
  end
31
- compressed
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(filepath)
36
- @filepath = filepath
103
+ def initialize(filepaths)
104
+ @filepaths = filepaths
37
105
  end
38
106
 
39
107
  def compress(options={})
40
- Shine.compress_file(@filepath)
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: magnetised-shine
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.3"
4
+ version: "0.4"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Garry Hill