tiny_png 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2012 Chris Sturgill, chris.thesturgills.com
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ 'Software'), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+ ## Table Of Contents
2
+
3
+ - [What Is TinyPNG] (#what-is-tinypng)
4
+ - [What Does This Gem Do?] (#what-does-this-gem-do)
5
+ - [Installation] (#installation)
6
+ - [Usage] (#usage)
7
+
8
+ ## What Is TinyPNG
9
+
10
+ [TinyPNG](http://www.tinypng.org) is a web-based service for shrinking the size of your PNG files.
11
+ The process employed preserves full alpha transparency. In short, it's a free service that makes
12
+ PNGs smaller (which reduces bandwidth and increases site performance).
13
+
14
+ ## What Does This Gem Do
15
+
16
+ TinyPNG has opened up a private beta for programmatically calling using their services. Before
17
+ this API was introduced, TinyPNG could only be used from their [web interface](http://www.tinypng.org).
18
+ And while their homepage is cool and all, manually uploading and saving files is a less-than-enjoyable
19
+ process. The kind of process a computer was made to do for you. Ergo this gem.
20
+
21
+ This gem interacts with TinyPNG's API to replace a given PNG with its shrunken counterpart.
22
+
23
+ ## Installation
24
+
25
+ If you're using bundler, just add the following to your Gemfile:
26
+
27
+ ```ruby
28
+ gem 'tiny_png'
29
+ ```
30
+
31
+ ## Usage
32
+
33
+ Create an instance of the `TinyPng::Shrink` class, passing in your API key:
34
+
35
+ ```ruby
36
+ @tiny = TinyPng::Shrink.new API_KEY
37
+ ```
38
+
39
+ If you want to work with a list of images, and want to silently suppress exceptions, just pass that in the options field:
40
+
41
+ ```ruby
42
+ @tiny = TinyPng::Shrink.new API_KEY, { :suppress_exceptions => true }
43
+ ```
44
+
45
+ Next, pass in the full path to the image you want to shrink
46
+
47
+ ```ruby
48
+ @tiny.shrink '/FULL/PATH/TO/IMAGE.png'
49
+ ```
50
+
51
+ The shrunken image will be saved in the same path, effectively overwriting the old file.
52
+
53
+ **NOTE:**
54
+
55
+ - There must be a file already at the specified path
56
+ - That file must be readable and writeable
57
+ - The file name *must* end in `.png`
@@ -0,0 +1,9 @@
1
+ module TinyPng::ExceptionHandling
2
+ #
3
+ # If exceptions are suppressed, just return false. Otherwise, raise the given exception
4
+ #
5
+ def raise_exception exception, message
6
+ return false if @options[:suppress_exceptions]
7
+ raise exception, message
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ class InvalidFileType < Exception
2
+ end
3
+
4
+ class FileDoesntExist < Exception
5
+ end
6
+
7
+ class FileNotReadable < Exception
8
+ end
9
+
10
+ class FileNotWriteable < Exception
11
+ end
12
+
13
+ class ShrinkingFailed < Exception
14
+ end
@@ -0,0 +1,74 @@
1
+ require 'base64'
2
+ require 'httparty'
3
+ require File.join File.dirname(__FILE__), 'exceptions'
4
+ require File.join File.dirname(__FILE__), 'exception_handling'
5
+
6
+ module TinyPng
7
+ class Shrink
8
+ include TinyPng::ExceptionHandling
9
+
10
+ include HTTParty
11
+ base_uri 'api.tinypng.org'
12
+
13
+ #
14
+ # Create a new instance of the TinyPng::Shrink class
15
+ #
16
+ # Arguments:
17
+ # - api_key (String)
18
+ # - options (Hash)
19
+ # - :suppress_exceptions (Boolean)
20
+ # Default: false
21
+ # 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.
22
+ #
23
+ # Returns: TinyPng::Shrink instance
24
+ #
25
+ def initialize api_key, options={}
26
+ @options = { :suppress_exceptions => false }.merge options
27
+ @auth = { :username => 'api', :password => api_key }
28
+ end
29
+
30
+ #
31
+ # Replace an image at a given path with its shrunken version
32
+ #
33
+ # 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.
34
+ #
35
+ # Arguments:
36
+ # - image_path (String)
37
+ # The full path to the image that should be shrunk. Requirements:
38
+ # - Must be the full and absolute path (not a relative path)
39
+ # - Path must end in `.png`
40
+ # - Path must be readable
41
+ # - Path must be writeable
42
+ #
43
+ # Returns: Boolean (whether or not the action was successful)
44
+ #
45
+ def shrink image_path
46
+ # check to make sure everything looks kosher before sending data to TinyPNG
47
+ return raise_exception InvalidFileType, 'The file must be a PNG' unless image_path.downcase.match(/\.png$/)
48
+ return raise_exception FileDoesntExist, "Can't find a file at the specified path" unless File.exists? image_path
49
+ return raise_exception FileNotReadable, "Can't read the requested file" unless File.readable? image_path
50
+ return raise_exception FileNotWriteable, "Can't write to the specifed file" unless File.writable? image_path
51
+
52
+ # store current content in case we need to rollback later
53
+ current_content = File.read image_path
54
+
55
+ # passes our quick checks, so let's fire off the request
56
+ response = self.class.post('/api/shrink', :basic_auth => @auth, :body => current_content.force_encoding('BINARY'))
57
+
58
+ # abort unless TinyPNG successfully did its thing
59
+ return raise_exception ShrinkingFailed, response.message unless response.code == 200
60
+
61
+ # only overwrite if the new file is smaller than the old
62
+ if response['output']['ratio'] < 1
63
+ begin
64
+ open(image_path, 'wb') { |f| f.write HTTParty.get(response['output']['url']).parsed_response }
65
+ rescue # if the writing process fails for whatever reason, rollback and raise custom error
66
+ open(image_path, 'wb') { |f| f.write current_content }
67
+ return raise_exception ShrinkingFailed, "Couldn't write new content"
68
+ end
69
+ end
70
+
71
+ true
72
+ end
73
+ end
74
+ end
data/lib/tiny_png.rb ADDED
@@ -0,0 +1,4 @@
1
+ module TinyPng
2
+ end
3
+
4
+ require File.join File.dirname(__FILE__), 'tiny_png', 'tiny_png'
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tiny_png
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Chris Sturgill
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: httparty
16
+ requirement: &2152385040 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2152385040
25
+ description: Use the TinyPNG service to minimize the size of your image files.
26
+ email: chris@thesturgills.com
27
+ executables: []
28
+ extensions: []
29
+ extra_rdoc_files: []
30
+ files:
31
+ - lib/tiny_png/exception_handling.rb
32
+ - lib/tiny_png/exceptions.rb
33
+ - lib/tiny_png/tiny_png.rb
34
+ - lib/tiny_png.rb
35
+ - LICENSE
36
+ - README.md
37
+ homepage: http://chris.thesturgills.com
38
+ licenses: []
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project: tiny_png
57
+ rubygems_version: 1.8.16
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: Make your PNGs tiny
61
+ test_files: []