rack-smusher 0.0.2 → 0.0.3

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.
Files changed (3) hide show
  1. data/README.md +11 -3
  2. data/lib/rack/smusher.rb +37 -16
  3. metadata +4 -4
data/README.md CHANGED
@@ -8,16 +8,24 @@ It is intended to be used with [serve](http://github.com/jlong/serve), but it ma
8
8
 
9
9
  ## Why?
10
10
 
11
- Because I like to save my Fireworks editable files in the public folder while designing / prototyping. That, and it seemed like a fun miniproject.
11
+ I like to save Fireworks editable files inside my serve project, and store them in the git repository. Thus, collaborators have access to the source files and can make changes, history is preserved, and life is good.
12
+
13
+ That, and it seemed like a fun miniproject :)
12
14
 
13
15
 
14
16
  ## Usage
15
17
 
18
+ Put this in your config.ru:
19
+
16
20
  gem 'rack-smusher'
17
21
  require 'rack/smusher'
18
22
 
19
23
  use Rack::Smusher, {
20
- :cache_path => "cache",
21
- :image_path => "public"
24
+ :source => "app/images",
25
+ :target => "public/images",
26
+ :base_url => "/images/"
22
27
  }
23
28
 
29
+ Now, you can save your editable files in `app/images`, and rack-smusher will compress and copy them to `public/images` when the browser requests them. Folder structure will be preserved, so a file saved in `app/images/foo/bar.png` will be saved to `public/images/foo/bar.png`.
30
+
31
+ If for some reason your app uses a different path for images (say, `/img/`) you can specify it in the `:base_url` option.
data/lib/rack/smusher.rb CHANGED
@@ -2,43 +2,64 @@ require "rack"
2
2
  require "smusher"
3
3
  require 'digest/md5'
4
4
 
5
+ class String
6
+ def - arg
7
+ copy = self.dup
8
+ copy[arg] = ""
9
+ return copy
10
+ end
11
+ end
12
+
5
13
  class Rack::Smusher
6
14
 
7
- VERSION = "0.0.2"
15
+ VERSION = "0.0.3"
8
16
 
9
17
  def initialize(app, options = {}, &block)
10
18
  @app = app
11
19
  @options = {
12
- :cache => true,
13
- :cache_path => ".cache",
14
- :image_path => "public"
20
+ :source => "public/images/source",
21
+ :target => "public/images",
22
+ :base_url => "/images/"
15
23
  }.merge(options)
16
- if !File.exist? @options[:cache_path]
17
- mkdir @options[:cache_path]
18
- end
19
24
  instance_eval(&block) if block_given?
20
25
  end
21
26
 
22
27
  def call(env)
23
28
  status, headers, body = @app.call(env)
24
29
 
25
- type = headers['Content-Type']
30
+ if env['PATH_INFO'][/png/]
31
+ should_compress = false
32
+ source_path = @options[:source] + '/' + (env['PATH_INFO'] - @options[:base_url])
33
+ target_path = @options[:target] + '/' + (env['PATH_INFO'] - @options[:base_url])
26
34
 
27
- if type == 'image/png'
28
- file = @options[:image_path] + env['PATH_INFO']
29
- file_hash = Digest::MD5.hexdigest(File.read(file))
35
+ there_is_a_source_file = File.exist?(source_path)
36
+ there_is_a_target_file = File.exist?(target_path)
30
37
 
31
- target = @options[:cache_path] + '/' + file_hash + '_' + File.basename(file)
32
- if !File.exist? target || @options[:cache] == false
33
- cp_r file, target, { :verbose => false }
34
- %x(smusher #{target})
38
+ if there_is_a_source_file && there_is_a_target_file
39
+ if File.mtime(source_path) > File.mtime(target_path)
40
+ should_compress = true
41
+ end
35
42
  end
36
43
 
37
- body = File.read(target)
44
+ if !there_is_a_target_file && there_is_a_source_file
45
+ should_compress = true
46
+ end
47
+
48
+ if should_compress
49
+ status, body = compress source_path, target_path
50
+ end
38
51
  end
39
52
 
40
53
  @response = Rack::Response.new(body, status, headers)
41
54
  @response.to_a
42
55
  end
43
56
 
57
+ def compress source, target
58
+ puts "compressing #{source}"
59
+ mkdir_p File.dirname(target), { :verbose => false }
60
+ cp_r source, target, { :verbose => false }
61
+ %x(smusher #{target})
62
+ [200, File.read(target)]
63
+ end
64
+
44
65
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-smusher
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Ale Mu\xC3\xB1oz"
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-12-29 00:00:00 +01:00
18
+ date: 2010-12-30 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency