tiny_png 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +5 -1
- data/README.md +33 -2
- data/lib/tiny_png.rb +7 -1
- data/lib/tiny_png/client.rb +0 -6
- data/lib/tiny_png/exceptions.rb +0 -3
- data/lib/tiny_png/path.rb +66 -0
- data/lib/tiny_png/recipes.rb +1 -0
- data/lib/tiny_png/shrink.rb +4 -12
- data/lib/tiny_png/tasks.rb +4 -1
- data/lib/tiny_png/version.rb +1 -1
- metadata +5 -4
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -4,6 +4,7 @@
|
|
4
4
|
- [What Does This Gem Do?] (#what-does-this-gem-do)
|
5
5
|
- [Installation] (#installation)
|
6
6
|
- [Usage] (#usage)
|
7
|
+
- [Using Blacklists](#using-blacklists)
|
7
8
|
- [Using From Rake](#using-from-rake)
|
8
9
|
- [Using With Capistrano](#using-with-capistrano)
|
9
10
|
|
@@ -99,6 +100,25 @@ For each image path analyzed (whether sent in directly or picked out of a folder
|
|
99
100
|
- That file must be readable and writeable
|
100
101
|
- The file name *must* end in `.png`
|
101
102
|
|
103
|
+
### Using Blacklists
|
104
|
+
|
105
|
+
There might be times where you want to shrink an entire directory, but exclude certain files. To do this, you'll need
|
106
|
+
pass a `TinyPng::Path` object to `@client.shrink`.
|
107
|
+
|
108
|
+
```ruby
|
109
|
+
@paths = TinyPng::Path.new '/images/path'
|
110
|
+
@paths.blacklist '/images/path/blacklist.png'
|
111
|
+
@client.shrink @paths
|
112
|
+
```
|
113
|
+
|
114
|
+
The blacklist array takes precedence over anything else inputted into this TinyPng::Path object.
|
115
|
+
|
116
|
+
**NOTE:** TinyPng::Path objects are independent of each other. If you send in multiple path objects to the `shrink` method,
|
117
|
+
the blacklist from one path object will not impact the other path object.
|
118
|
+
|
119
|
+
You can send in TinyPng::Path objects, directories, and full image paths to the shrink method, but each will
|
120
|
+
be analyzed by itself and the blacklist from any path object will be self-contained.
|
121
|
+
|
102
122
|
### Using From Rake
|
103
123
|
|
104
124
|
Create a Rakefile in your app's root (or add this to an existing Rakefile):
|
@@ -144,6 +164,13 @@ Combining directories and specific image files:
|
|
144
164
|
SHRINK=/image/directory,/path/to/image.png rake tiny_png:shrink
|
145
165
|
```
|
146
166
|
|
167
|
+
The Rake task uses the [blacklist method](#using-blacklists) in building out the full paths. This method allows you
|
168
|
+
to also specify blacklisted paths:
|
169
|
+
|
170
|
+
```ruby
|
171
|
+
SHRINK=/image/directory BLACKLIST=/image/directory/blacklist.png rake tiny_png:shrink
|
172
|
+
```
|
173
|
+
|
147
174
|
By default, this rake task will use the settings in config/tiny_png.yml. You can overwrite specific settings by passing
|
148
175
|
environment variables. For example, if I do not suppress exceptions in my base config, but want
|
149
176
|
to when running a rake task, all I would need to do is set the `SUPPRESS_EXCEPTIONS` environment variable to true:
|
@@ -161,10 +188,10 @@ API_KEY=my_api_key SHRINK=/some/image.png rake tiny_png:shrink
|
|
161
188
|
Naturally, these can all be combined in any fashion:
|
162
189
|
|
163
190
|
```ruby
|
164
|
-
SUPPRESS_EXCEPTIONS=true API_KEY=my_api_key SHRINK=/image/directory,/some/image.png rake tiny_png:shrink
|
191
|
+
SUPPRESS_EXCEPTIONS=true API_KEY=my_api_key SHRINK=/image/directory,/some/image.png BLACKLIST=/image/directory/blacklist.png rake tiny_png:shrink
|
165
192
|
```
|
166
193
|
|
167
|
-
### Using
|
194
|
+
### Using With Capistrano
|
168
195
|
|
169
196
|
Since Photoshop doesn't support indexed PNGs with alpha transparency, you might not want to convert the
|
170
197
|
images on your local box. With the included Capistrano recipe, you can automatically shrink all files
|
@@ -183,6 +210,9 @@ the assets are precompiled so that it isn't run multiple times per image:
|
|
183
210
|
before 'deploy:assets:precompile', 'tiny_png:shrink'
|
184
211
|
```
|
185
212
|
|
213
|
+
Ultimately, the Capistrano recipe will build and run a Rake task on the server, so you need to have the
|
214
|
+
[Rakefile setup correctly](#using-from-rake).
|
215
|
+
|
186
216
|
By default, this script uses the settings found in config/tiny_png.yml. It also runs through the entire
|
187
217
|
release directory. Each option can be overwritten by setting Capistrano variables. These variables will
|
188
218
|
be sent directly to a Rake task, so the expected format of these variables is the same as listed above:
|
@@ -191,6 +221,7 @@ be sent directly to a Rake task, so the expected format of these variables is th
|
|
191
221
|
set :tiny_png_shrink, '/image/directory,/some/image.png'
|
192
222
|
set :tiny_png_api_key, 'my_api_key'
|
193
223
|
set :tiny_png_suppress_exceptions, true
|
224
|
+
set :tiny_png_blacklist, '/image/directory/blacklist.png'
|
194
225
|
```
|
195
226
|
|
196
227
|
Also by default, the task is only run on web servers. This can be modified by setting the `tiny_png_server_role`
|
data/lib/tiny_png.rb
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
module TinyPng
|
2
2
|
end
|
3
3
|
|
4
|
+
require 'base64'
|
5
|
+
require 'httparty'
|
4
6
|
require 'tiny_png/client'
|
5
|
-
require 'tiny_png/version'
|
7
|
+
require 'tiny_png/version'
|
8
|
+
require 'tiny_png/exceptions'
|
9
|
+
require 'tiny_png/shrink'
|
10
|
+
require 'tiny_png/exception_handling'
|
11
|
+
require 'tiny_png/path'
|
data/lib/tiny_png/client.rb
CHANGED
data/lib/tiny_png/exceptions.rb
CHANGED
@@ -0,0 +1,66 @@
|
|
1
|
+
module TinyPng
|
2
|
+
class Path
|
3
|
+
#
|
4
|
+
# Create a TinyPng::Path object and optionally add .png paths to the
|
5
|
+
# show list (also transverses directories).
|
6
|
+
#
|
7
|
+
def initialize *show
|
8
|
+
@show = TinyPng::Path.get_all show
|
9
|
+
@blacklist = []
|
10
|
+
end
|
11
|
+
|
12
|
+
#
|
13
|
+
# Add paths to the list (also transverses directories).
|
14
|
+
#
|
15
|
+
def add *list
|
16
|
+
@show = (@show + TinyPng::Path.get_all(list)).uniq
|
17
|
+
end
|
18
|
+
|
19
|
+
#
|
20
|
+
# Add paths to the list (also transverses directories). The blacklist
|
21
|
+
# supercedes the add list, such that any path added to the blacklist will
|
22
|
+
# not show up in the final compilation.
|
23
|
+
#
|
24
|
+
def blacklist *list
|
25
|
+
@blacklist = (@blacklist + TinyPng::Path.get_all(list)).uniq
|
26
|
+
end
|
27
|
+
|
28
|
+
#
|
29
|
+
# Remove paths from the list (also transverses directories).
|
30
|
+
#
|
31
|
+
def remove_from_list *list
|
32
|
+
@show = @show - TinyPng::Path.get_all(list)
|
33
|
+
end
|
34
|
+
|
35
|
+
#
|
36
|
+
# Remove paths from the blacklist (also transverses directories).
|
37
|
+
#
|
38
|
+
def remove_from_blacklist *list
|
39
|
+
@blacklist = @blacklist - TinyPng::Path.get_all(list)
|
40
|
+
end
|
41
|
+
|
42
|
+
#
|
43
|
+
# Return the full list of paths, minus anything in the blacklist.
|
44
|
+
#
|
45
|
+
def list
|
46
|
+
@show.uniq - @blacklist
|
47
|
+
end
|
48
|
+
|
49
|
+
#
|
50
|
+
# Get all .png path names from the passed-in files and directories.
|
51
|
+
#
|
52
|
+
def self.get_all *list
|
53
|
+
list.flatten.uniq.collect do |path|
|
54
|
+
if path.is_a?(TinyPng::Path)
|
55
|
+
path.list
|
56
|
+
else
|
57
|
+
if File.directory?(path)
|
58
|
+
Dir.glob("#{path.gsub(/\/$/,'')}/**/*.png")
|
59
|
+
else
|
60
|
+
path.downcase.match(/\.png$/) ? path : nil
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end.flatten.uniq.compact
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
data/lib/tiny_png/recipes.rb
CHANGED
@@ -17,6 +17,7 @@ Capistrano::Configuration.instance(:must_exist).load do
|
|
17
17
|
env.push "API_KEY=#{self[:tiny_png_api_key]}" unless self[:tiny_png_api_key].nil?
|
18
18
|
env.push "API_USER=#{self[:tiny_png_api_user]}" unless self[:tiny_png_api_user].nil?
|
19
19
|
env.push "SUPPRESS_EXCEPTIONS=#{self[:tiny_png_suppress_exceptions]}" unless self[:tiny_png_suppress_exceptions].nil?
|
20
|
+
env.push "BLACKLIST=#{self[:tiny_png_blacklist]}" unless self[:tiny_png_blacklist].nil?
|
20
21
|
env
|
21
22
|
end
|
22
23
|
|
data/lib/tiny_png/shrink.rb
CHANGED
@@ -18,18 +18,11 @@ module TinyPng::Shrink
|
|
18
18
|
# Returns: Hash { :success => [Array, Of, Paths, Successfully, Overwrittern], :failure => [Array, Of, Paths, Left, Unchanged] }
|
19
19
|
#
|
20
20
|
|
21
|
-
def shrink *
|
21
|
+
def shrink *paths
|
22
22
|
results = { :success => [], :failure => [] }
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
key = self.shrink_in_place(image_path) ? :success : :failure
|
27
|
-
results[key].push image_path
|
28
|
-
end
|
29
|
-
else
|
30
|
-
key = shrink_in_place(shrinkable) ? :success : :failure
|
31
|
-
results[key].push shrinkable
|
32
|
-
end
|
23
|
+
TinyPng::Path.get_all(paths).each do |path|
|
24
|
+
key = shrink_in_place(path) ? :success : :failure
|
25
|
+
results[key].push path
|
33
26
|
end
|
34
27
|
results
|
35
28
|
end
|
@@ -38,7 +31,6 @@ module TinyPng::Shrink
|
|
38
31
|
|
39
32
|
def shrink_in_place image_path
|
40
33
|
# check to make sure everything looks kosher before sending data to TinyPNG
|
41
|
-
return raise_exception InvalidFileType, 'The file must be a PNG' unless image_path.downcase.match(/\.png$/)
|
42
34
|
return raise_exception FileDoesntExist, "Can't find a file at the specified path" unless File.exists? image_path
|
43
35
|
return raise_exception FileNotReadable, "Can't read the requested file" unless File.readable? image_path
|
44
36
|
return raise_exception FileNotWriteable, "Can't write to the specifed file" unless File.writable? image_path
|
data/lib/tiny_png/tasks.rb
CHANGED
@@ -16,6 +16,9 @@ namespace :tiny_png do
|
|
16
16
|
options[:suppress_exceptions] = ENV['SUPPRESS_EXCEPTIONS'].downcase == 'true'
|
17
17
|
end
|
18
18
|
|
19
|
-
TinyPng::
|
19
|
+
paths = TinyPng::Path.new ENV['SHRINK'].split(',')
|
20
|
+
paths.blacklist ENV['BLACKLIST'].split(',') unless ENV['BLACKLIST'].nil?
|
21
|
+
|
22
|
+
TinyPng::Client.new(options).shrink paths
|
20
23
|
end
|
21
24
|
end
|
data/lib/tiny_png/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tiny_png
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-10-
|
12
|
+
date: 2012-10-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: httparty
|
16
|
-
requirement: &
|
16
|
+
requirement: &2168794500 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2168794500
|
25
25
|
description: Use the TinyPNG service to minimize the size of your image files.
|
26
26
|
email: chris@thesturgills.com
|
27
27
|
executables: []
|
@@ -38,6 +38,7 @@ files:
|
|
38
38
|
- lib/tiny_png/client.rb
|
39
39
|
- lib/tiny_png/exception_handling.rb
|
40
40
|
- lib/tiny_png/exceptions.rb
|
41
|
+
- lib/tiny_png/path.rb
|
41
42
|
- lib/tiny_png/recipes.rb
|
42
43
|
- lib/tiny_png/shrink.rb
|
43
44
|
- lib/tiny_png/tasks.rb
|