tiny_png 0.3.0.pre → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ Edge
2
+
3
+ - Capistrano recipe to shrink images on deploy
4
+
1
5
  0.3.0.pre - October 11, 2012
2
6
 
3
7
  - Use of config file
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/README.md CHANGED
@@ -5,6 +5,7 @@
5
5
  - [Installation] (#installation)
6
6
  - [Usage] (#usage)
7
7
  - [Using From Rake](#using-from-rake)
8
+ - [Using With Capistrano](#using-with-capistrano)
8
9
 
9
10
  ## What Is TinyPNG
10
11
 
@@ -28,6 +29,10 @@ When you pass a full path into the `shrink` method, it will replace the source i
28
29
  returned by TinyPNG. If this process fails for whatever reason, the original image will be restored.
29
30
  On success, the original image is completely overwritten and is no longer available.
30
31
 
32
+ These features can also be used via [Rake](#using-from-rake) or [Capistrano](#using-with-capistrano).
33
+ The latter gives you the option of setting up your deploy to automatically convert your images on your
34
+ external servers!
35
+
31
36
  ## Installation
32
37
 
33
38
  If you're using bundler, just add the following to your Gemfile:
@@ -44,7 +49,7 @@ Create an instance of the `TinyPng::Client` class:
44
49
  @client = TinyPng::Client.new
45
50
  ```
46
51
 
47
- Note that this will look for the configuration file in `config/tiny_png.yml`. See the [sample config](https://github.com/sturgill/tiny_png/sample_config.yml).
52
+ Note that this will look for the configuration file in `config/tiny_png.yml`. See the [sample config](https://github.com/sturgill/tiny_png/blob/master/sample_config.yml).
48
53
  If you choose to not use the config file, you will need to pass in your API key in the optional hash:
49
54
 
50
55
  ```ruby
@@ -157,4 +162,40 @@ Naturally, these can all be combined in any fashion:
157
162
 
158
163
  ```ruby
159
164
  SUPPRESS_EXCEPTIONS=true API_KEY=my_api_key SHRINK=/image/directory,/some/image.png rake tiny_png:shrink
165
+ ```
166
+
167
+ ### Using In Capistrano
168
+
169
+ Since Photoshop doesn't support indexed PNGs with alpha transparency, you might not want to convert the
170
+ images on your local box. With the included Capistrano recipe, you can automatically shrink all files
171
+ in your deploy path.
172
+
173
+ First, include the recipe in your deploy script:
174
+
175
+ ```ruby
176
+ require 'tiny_png/recipes'
177
+ ```
178
+
179
+ Now you can define the callback wherever makes sense for you. I would recommend calling this before
180
+ the assets are precompiled so that it isn't run multiple times per image:
181
+
182
+ ```ruby
183
+ before 'deploy:assets:precompile', 'tiny_png:shrink'
184
+ ```
185
+
186
+ By default, this script uses the settings found in config/tiny_png.yml. It also runs through the entire
187
+ release directory. Each option can be overwritten by setting Capistrano variables. These variables will
188
+ be sent directly to a Rake task, so the expected format of these variables is the same as listed above:
189
+
190
+ ```ruby
191
+ set :tiny_png_shrink, '/image/directory,/some/image.png'
192
+ set :tiny_png_api_key, 'my_api_key'
193
+ set :tiny_png_suppress_exceptions, true
194
+ ```
195
+
196
+ Also by default, the task is only run on web servers. This can be modified by setting the `tiny_png_server_role`
197
+ variable:
198
+
199
+ ```ruby
200
+ set :tiny_png_server_role, [:web, :app]
160
201
  ```
data/lib/tiny_png.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module TinyPng
2
2
  end
3
3
 
4
- require File.join File.dirname(__FILE__), 'tiny_png', 'client'
5
- require File.join File.dirname(__FILE__), 'tiny_png', 'version'
4
+ require 'tiny_png/client'
5
+ require 'tiny_png/version'
@@ -1,8 +1,8 @@
1
1
  require 'base64'
2
2
  require 'httparty'
3
- require File.join File.dirname(__FILE__), 'exceptions'
4
- require File.join File.dirname(__FILE__), 'shrink'
5
- require File.join File.dirname(__FILE__), 'exception_handling'
3
+ require 'tiny_png/exceptions'
4
+ require 'tiny_png/shrink'
5
+ require 'tiny_png/exception_handling'
6
6
 
7
7
  module TinyPng
8
8
  class Client
@@ -13,14 +13,22 @@ module TinyPng
13
13
  base_uri 'api.tinypng.org'
14
14
 
15
15
  #
16
- # Create a new instance of the TinyPng::Shrink class
16
+ # Create a new instance of the TinyPng::Shrink class. Any key not passed into the
17
+ # options hash will use the corresponding value from config/tiny_png.yml. If the config
18
+ # file does not exist, or if the config file does not define a given value, sensible defaults
19
+ # (as outlined below) will be used.
17
20
  #
18
21
  # Arguments:
19
- # - api_key (String)
20
22
  # - options (Hash)
21
23
  # - :suppress_exceptions (Boolean)
22
24
  # Default: false
23
- # If false (the default), exceptions will be raised when the process fails. If suppression is turned on, exceptions will not be raised, but a false value will be returned if shrinking fails for any reason.
25
+ # If false (the default), exceptions will be raised when the process fails. If suppression is
26
+ # turned on, exceptions will not be raised, but a false value will be returned if shrinking fails
27
+ # for any reason.
28
+ # - :api_key (String)
29
+ # Default: ''
30
+ # - :api_user (String)
31
+ # Default: 'api'
24
32
  #
25
33
  # Returns: TinyPng::Client instance
26
34
  #
@@ -34,7 +42,7 @@ module TinyPng
34
42
  @options = {
35
43
  :suppress_exceptions => config['suppress_exceptions'] || false,
36
44
  :api_key => config['api_key'] || '',
37
- :username => config['api_user'] || 'api'
45
+ :api_user => config['api_user'] || 'api'
38
46
  }.merge(options)
39
47
  end
40
48
  end
@@ -0,0 +1,35 @@
1
+ Capistrano::Configuration.instance(:must_exist).load do
2
+ namespace :tiny_png do
3
+ def rails_env
4
+ fetch(:rails_env, 'production')
5
+ end
6
+
7
+ def roles
8
+ fetch(:tiny_png_server_role, :web)
9
+ end
10
+
11
+ def shrinkwrap
12
+ fetch(:tiny_png_shrink, current_path)
13
+ end
14
+
15
+ def rake_env
16
+ env = ["RAILS_ENV=#{rails_env}", "SHRINK=#{shrinkwrap}"]
17
+ env.push "API_KEY=#{self[:tiny_png_api_key]}" unless self[:tiny_png_api_key].nil?
18
+ env.push "API_USER=#{self[:tiny_png_api_user]}" unless self[:tiny_png_api_user].nil?
19
+ env.push "SUPPRESS_EXCEPTIONS=#{self[:tiny_png_suppress_exceptions]}" unless self[:tiny_png_suppress_exceptions].nil?
20
+ env
21
+ end
22
+
23
+ def rake(*tasks)
24
+ rake = fetch(:rake, "rake")
25
+ tasks.each do |t|
26
+ run "if [ -d #{release_path} ]; then cd #{release_path}; else cd #{current_path}; fi; if [ -f Rakefile ]; then #{rake} #{rake_env.join(' ')} #{t}; fi;"
27
+ end
28
+ end
29
+
30
+ desc "Send the requested images to TinyPNG.org for shrinking"
31
+ task :shrink, :roles => lambda { roles } do
32
+ rake 'tiny_png:shrink'
33
+ end
34
+ end
35
+ end
@@ -2,7 +2,8 @@ module TinyPng::Shrink
2
2
  #
3
3
  # Replace an image at a given path with its shrunken version
4
4
  #
5
- # The image at the given path will be submitted to TinyPNG and the new version will overwrite the original. If this process fails for any reason, the original file will be rolled back.
5
+ # The image at the given path will be submitted to TinyPNG and the new version will overwrite the original.
6
+ # If this process fails for any reason, the original file will be rolled back.
6
7
  #
7
8
  # Arguments:
8
9
  # - shrinkwrap (any number of objects to be shrunk)
@@ -10,7 +11,7 @@ module TinyPng::Shrink
10
11
  #
11
12
  # If you send in a path to a specific file, it must end in `.png` or it will not be sent to TinyPNG for processing.
12
13
  #
13
- # Likewise, when traversing through a directory, only files that end in `.png` will be processed.
14
+ # Likewise, when traversing through a directory, only files that end in `.png` will be examined.
14
15
  #
15
16
  # All paths to specific files (whether sent in directly or picked out from the directory) need to be readable and writeable.
16
17
  #
@@ -48,7 +49,7 @@ module TinyPng::Shrink
48
49
  # passes our quick checks, so let's fire off the request
49
50
  response = self.class.post(
50
51
  '/api/shrink',
51
- :basic_auth => { :username => @options[:username], :password => @options[:api_key] },
52
+ :basic_auth => { :username => @options[:api_user], :password => @options[:api_key] },
52
53
  :body => current_content.force_encoding('BINARY')
53
54
  )
54
55
 
@@ -4,7 +4,7 @@ namespace :tiny_png do
4
4
  abort "set SHRINK env var, e.g., $ SHRINK=/path/to/image.png,/path/to/another.png rake tiny_png:shrink" if ENV['SHRINK'].nil?
5
5
 
6
6
  options = {}
7
- [:api_key, :user_name].each do |key|
7
+ [:api_key, :api_user].each do |key|
8
8
  env_key = key.to_s.upcase
9
9
  options[key] = ENV[env_key] unless ENV[env_key].nil?
10
10
  end
@@ -1,3 +1,3 @@
1
1
  module TinyPng
2
- Version = VERSION = '0.3.0.pre'
2
+ Version = VERSION = '1.0.0'
3
3
  end
@@ -0,0 +1 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'tiny_png', 'recipes'))
data/tiny_png.gemspec CHANGED
@@ -1,5 +1,4 @@
1
- $LOAD_PATH.unshift 'lib'
2
- require 'tiny_png/version'
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'lib', 'tiny_png', 'version'))
3
2
 
4
3
  Gem::Specification.new do |s|
5
4
  s.name = 'tiny_png'
@@ -11,12 +10,12 @@ Gem::Specification.new do |s|
11
10
  s.author = 'Chris Sturgill'
12
11
  s.email = 'chris@thesturgills.com'
13
12
  s.files = `git ls-files`.split($/)
14
- s.homepage = 'https://github.com/sturgill/tiny_png'
13
+ s.homepage = 'http://chris.thesturgills.com'
15
14
  s.require_paths = ['lib']
16
15
  s.add_dependency 'httparty'
17
16
  s.license = 'MIT'
18
17
  s.post_install_message = %q{For easiest use, create a config file at config/tiny_png.yml file. Example:
19
- https://github.com/sturgill/tiny_png/sample_config.yml
18
+ https://github.com/sturgill/tiny_png/blob/master/sample_config.yml
20
19
 
21
20
  }
22
21
  end
metadata CHANGED
@@ -1,8 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tiny_png
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0.pre
5
- prerelease: 6
4
+ version: 1.0.0
5
+ prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Chris Sturgill
@@ -13,7 +13,7 @@ date: 2012-10-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httparty
16
- requirement: &2152185580 !ruby/object:Gem::Requirement
16
+ requirement: &2156392360 !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: *2152185580
24
+ version_requirements: *2156392360
25
25
  description: Use the TinyPNG service to minimize the size of your image files.
26
26
  email: chris@thesturgills.com
27
27
  executables: []
@@ -30,6 +30,7 @@ extra_rdoc_files: []
30
30
  files:
31
31
  - .gitignore
32
32
  - CHANGELOG.md
33
+ - Gemfile
33
34
  - LICENSE
34
35
  - README.md
35
36
  - Rakefile
@@ -37,17 +38,19 @@ files:
37
38
  - lib/tiny_png/client.rb
38
39
  - lib/tiny_png/exception_handling.rb
39
40
  - lib/tiny_png/exceptions.rb
41
+ - lib/tiny_png/recipes.rb
40
42
  - lib/tiny_png/shrink.rb
41
43
  - lib/tiny_png/tasks.rb
42
44
  - lib/tiny_png/version.rb
45
+ - recipes/tiny_png.rb
43
46
  - sample_config.yml
44
47
  - tiny_png.gemspec
45
- homepage: https://github.com/sturgill/tiny_png
48
+ homepage: http://chris.thesturgills.com
46
49
  licenses:
47
50
  - MIT
48
51
  post_install_message: ! "For easiest use, create a config file at config/tiny_png.yml
49
- file. Example:\n https://github.com/sturgill/tiny_png/sample_config.yml\n \n
50
- \ "
52
+ file. Example:\n https://github.com/sturgill/tiny_png/blob/master/sample_config.yml\n
53
+ \ \n "
51
54
  rdoc_options: []
52
55
  require_paths:
53
56
  - lib
@@ -60,9 +63,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
60
63
  required_rubygems_version: !ruby/object:Gem::Requirement
61
64
  none: false
62
65
  requirements:
63
- - - ! '>'
66
+ - - ! '>='
64
67
  - !ruby/object:Gem::Version
65
- version: 1.3.1
68
+ version: '0'
66
69
  requirements: []
67
70
  rubyforge_project: tiny_png
68
71
  rubygems_version: 1.8.16