tiny_png 0.2.0 → 0.3.0.pre

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ .bundle
2
+ tiny_png*.gem
3
+ Gemfile.lock
4
+ .rvmrc
data/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ 0.3.0.pre - October 11, 2012
2
+
3
+ - Use of config file
4
+ - Introduce rake task to perform shrinking action on images / directories
5
+
6
+ 0.2.0 - October 10, 2012
7
+
8
+ - Allow for shrinking multiple images / directories at one time
9
+ - Move from TinyPng::Shrink.new to TinyPng::Client.new
10
+
11
+ 0.1.1 - October 10, 2012
12
+
13
+ - First release
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 From Rake](#using-from-rake)
7
8
 
8
9
  ## What Is TinyPNG
9
10
 
@@ -37,16 +38,24 @@ gem 'tiny_png'
37
38
 
38
39
  ## Usage
39
40
 
40
- Create an instance of the `TinyPng::Client` class, passing in your API key:
41
+ Create an instance of the `TinyPng::Client` class:
41
42
 
42
43
  ```ruby
43
- @client = TinyPng::Client.new API_KEY
44
+ @client = TinyPng::Client.new
44
45
  ```
45
46
 
46
- If you want to work with a list of images, and want to silently suppress exceptions, just pass that in the options field:
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).
48
+ If you choose to not use the config file, you will need to pass in your API key in the optional hash:
47
49
 
48
50
  ```ruby
49
- @client = TinyPng::Client.new API_KEY, { :suppress_exceptions => true }
51
+ @client = TinyPng::Client.new :api_key => 'my_api_key'
52
+ ```
53
+
54
+ If you want to work with a list of images, and want to silently suppress exceptions, change that in the config file or
55
+ pass it into the optional hash:
56
+
57
+ ```ruby
58
+ @client = TinyPng::Client.new :suppress_exceptions => true
50
59
  ```
51
60
 
52
61
  Next, pass in the full paths to the images you want to shrink (you can also pass in whole directories)
@@ -83,4 +92,69 @@ For each image path analyzed (whether sent in directly or picked out of a folder
83
92
 
84
93
  - There must be a file already at the specified path
85
94
  - That file must be readable and writeable
86
- - The file name *must* end in `.png`
95
+ - The file name *must* end in `.png`
96
+
97
+ ### Using From Rake
98
+
99
+ Create a Rakefile in your app's root (or add this to an existing Rakefile):
100
+
101
+ ```ruby
102
+ require 'your/app'
103
+ require 'tiny_png/tasks'
104
+ ```
105
+
106
+ The `rake tiny_png:shrink` task can be called to shrink images from the command line. To use this rake task,
107
+ you must pass a environment variable called `SHRINK`. `SHRINK` should be a comma-separated list of paths you want shrunk.
108
+ As with the `@client.shrink` method, the SHRINK variable accepts both directory and specific image paths.
109
+
110
+ **NOTE:** Because this works by splitting the environment string into an array based on a comma separator, it follows
111
+ that you can't have commas in your directory or image paths.
112
+
113
+ The same constraints exist from rake as do from the method call: namely, you must supply the full path, and specific
114
+ image paths must exist, be readable, be writable, and end in `.png`.
115
+
116
+ **Examples:**
117
+
118
+ Shrink a single file:
119
+
120
+ ```ruby
121
+ SHRINK=/path/to/image.png rake tiny_png:shrink
122
+ ```
123
+
124
+ Shrink multiple files:
125
+
126
+ ```ruby
127
+ SHRINK=/path/to/first.png,/path/to/second.png rake tiny_png:shrink
128
+ ```
129
+
130
+ Shrink a whole directory:
131
+
132
+ ```ruby
133
+ SHRINK=/image/directory rake tiny_png:shrink
134
+ ```
135
+
136
+ Combining directories and specific image files:
137
+
138
+ ```ruby
139
+ SHRINK=/image/directory,/path/to/image.png rake tiny_png:shrink
140
+ ```
141
+
142
+ By default, this rake task will use the settings in config/tiny_png.yml. You can overwrite specific settings by passing
143
+ environment variables. For example, if I do not suppress exceptions in my base config, but want
144
+ to when running a rake task, all I would need to do is set the `SUPPRESS_EXCEPTIONS` environment variable to true:
145
+
146
+ ```ruby
147
+ SUPPRESS_EXCEPTIONS=true SHRINK=/some/image.png rake tiny_png:shrink
148
+ ```
149
+
150
+ You can pass in an api key in a similar way:
151
+
152
+ ```ruby
153
+ API_KEY=my_api_key SHRINK=/some/image.png rake tiny_png:shrink
154
+ ```
155
+
156
+ Naturally, these can all be combined in any fashion:
157
+
158
+ ```ruby
159
+ SUPPRESS_EXCEPTIONS=true API_KEY=my_api_key SHRINK=/image/directory,/some/image.png rake tiny_png:shrink
160
+ ```
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'rubygems'
data/lib/tiny_png.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  module TinyPng
2
2
  end
3
3
 
4
- require File.join File.dirname(__FILE__), 'tiny_png', 'client'
4
+ require File.join File.dirname(__FILE__), 'tiny_png', 'client'
5
+ require File.join File.dirname(__FILE__), 'tiny_png', 'version'
@@ -24,9 +24,18 @@ module TinyPng
24
24
  #
25
25
  # Returns: TinyPng::Client instance
26
26
  #
27
- def initialize api_key, options={}
28
- @options = { :suppress_exceptions => false }.merge options
29
- @auth = { :username => 'api', :password => api_key }
27
+ def initialize options={}
28
+ if defined?(Rails) && Rails.try(:root) && Rails.try(:env)
29
+ config_path = File.join(Rails.root, 'config', 'tiny_png.yml')
30
+ config = YAML.load_file(config_path)[Rails.env] if File.exists? config_path
31
+ end
32
+ config ||= {}
33
+
34
+ @options = {
35
+ :suppress_exceptions => config['suppress_exceptions'] || false,
36
+ :api_key => config['api_key'] || '',
37
+ :username => config['api_user'] || 'api'
38
+ }.merge(options)
30
39
  end
31
40
  end
32
41
  end
@@ -46,7 +46,11 @@ module TinyPng::Shrink
46
46
  current_content = File.read image_path
47
47
 
48
48
  # passes our quick checks, so let's fire off the request
49
- response = self.class.post('/api/shrink', :basic_auth => @auth, :body => current_content.force_encoding('BINARY'))
49
+ response = self.class.post(
50
+ '/api/shrink',
51
+ :basic_auth => { :username => @options[:username], :password => @options[:api_key] },
52
+ :body => current_content.force_encoding('BINARY')
53
+ )
50
54
 
51
55
  # abort unless TinyPNG successfully did its thing
52
56
  return raise_exception ShrinkingFailed, response.message unless response.code == 200
@@ -0,0 +1,21 @@
1
+ namespace :tiny_png do
2
+ desc 'Shrink the images/directory: the SHRINK env variable must be set (comma-separated list of all files / directories you want shrunk)'
3
+ task :shrink => :environment do
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
+
6
+ options = {}
7
+ [:api_key, :user_name].each do |key|
8
+ env_key = key.to_s.upcase
9
+ options[key] = ENV[env_key] unless ENV[env_key].nil?
10
+ end
11
+
12
+ unless ENV['SUPPRESS_EXCEPTIONS'].nil?
13
+ unless ['true', 'false'].include?(ENV['SUPPRESS_EXCEPTIONS'].downcase)
14
+ abort "SUPPRESS_EXCEPTIONS can only be set to true or false: #{ENV['SUPPRESS_EXCEPTIONS']} is not a valid boolean value."
15
+ end
16
+ options[:suppress_exceptions] = ENV['SUPPRESS_EXCEPTIONS'].downcase == 'true'
17
+ end
18
+
19
+ TinyPng::Client.new(options).shrink ENV['SHRINK'].split(',')
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module TinyPng
2
+ Version = VERSION = '0.3.0.pre'
3
+ end
data/sample_config.yml ADDED
@@ -0,0 +1,3 @@
1
+ development:
2
+ suppress_exceptions: false
3
+ api_key: my_api_key_here
data/tiny_png.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ $LOAD_PATH.unshift 'lib'
2
+ require 'tiny_png/version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'tiny_png'
6
+ s.version = TinyPng::Version
7
+ s.platform = Gem::Platform::RUBY
8
+ s.rubyforge_project = 'tiny_png'
9
+ s.summary = 'Make your PNGs tiny'
10
+ s.description = "Use the TinyPNG service to minimize the size of your image files."
11
+ s.author = 'Chris Sturgill'
12
+ s.email = 'chris@thesturgills.com'
13
+ s.files = `git ls-files`.split($/)
14
+ s.homepage = 'https://github.com/sturgill/tiny_png'
15
+ s.require_paths = ['lib']
16
+ s.add_dependency 'httparty'
17
+ s.license = 'MIT'
18
+ 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
20
+
21
+ }
22
+ 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.2.0
5
- prerelease:
4
+ version: 0.3.0.pre
5
+ prerelease: 6
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: &2152072180 !ruby/object:Gem::Requirement
16
+ requirement: &2152185580 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,24 +21,33 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2152072180
24
+ version_requirements: *2152185580
25
25
  description: Use the TinyPNG service to minimize the size of your image files.
26
26
  email: chris@thesturgills.com
27
27
  executables: []
28
28
  extensions: []
29
29
  extra_rdoc_files: []
30
30
  files:
31
+ - .gitignore
32
+ - CHANGELOG.md
33
+ - LICENSE
34
+ - README.md
35
+ - Rakefile
36
+ - lib/tiny_png.rb
31
37
  - lib/tiny_png/client.rb
32
38
  - lib/tiny_png/exception_handling.rb
33
39
  - lib/tiny_png/exceptions.rb
34
40
  - lib/tiny_png/shrink.rb
35
- - lib/tiny_png.rb
36
- - LICENSE
37
- - README.md
41
+ - lib/tiny_png/tasks.rb
42
+ - lib/tiny_png/version.rb
43
+ - sample_config.yml
44
+ - tiny_png.gemspec
38
45
  homepage: https://github.com/sturgill/tiny_png
39
46
  licenses:
40
47
  - MIT
41
- post_install_message:
48
+ 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
+ \ "
42
51
  rdoc_options: []
43
52
  require_paths:
44
53
  - lib
@@ -51,9 +60,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
51
60
  required_rubygems_version: !ruby/object:Gem::Requirement
52
61
  none: false
53
62
  requirements:
54
- - - ! '>='
63
+ - - ! '>'
55
64
  - !ruby/object:Gem::Version
56
- version: '0'
65
+ version: 1.3.1
57
66
  requirements: []
58
67
  rubyforge_project: tiny_png
59
68
  rubygems_version: 1.8.16