rack-convert-webp 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d18e03933fdacc9808e1060ae43007f2cb6fb57f
4
+ data.tar.gz: 51a8eae62cbc51f8bf015cd49cc30debb4fbffae
5
+ SHA512:
6
+ metadata.gz: accc174c58aff412d058136489d94b7a31f610337877e028d4e7e94bf3ddd0532d346e767f4a9e21d72bd895c6d574e9b581780460dd265208ccfa9b01de6234
7
+ data.tar.gz: cf4f437556d1c8557d0f2ad5ad83e8491f83e3e4a8822fded7eeadf87d582a25a94cc1348ef67783d2d19ea91af6c67f26359ccb1f7093c70c9d5aff7ae697ad
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-convert-webp.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Yuichi Tateno
2
+
3
+ MIT License
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
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,35 @@
1
+ # Rack::Convert::Webp
2
+
3
+ Auto convert to WebP.
4
+
5
+ ## Installation
6
+
7
+ ```
8
+ gem install rack-convert-webp
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```
14
+ $ rackup -r 'rack/convert/webp' -b "use Rack::Convert::Webp;run Rack::Directory.new '.'"
15
+ $ curl -X GET -I -H "Accept: image/webp" -H "Content-Type: image/jpeg" 'http://localhost:9292/example.jpg'
16
+ HTTP/1.1 200 OK
17
+ Last-Modified: Thu, 20 Mar 2014 14:50:34 GMT
18
+ Content-Type: image/webp
19
+ Content-Length: 19340
20
+ X-Encoding-Time: 42ms
21
+ X-Compress: 24% - original: 78361 webp: 19340
22
+ Connection: keep-alive
23
+ Server: thin 1.6.1 codename Death Proof
24
+
25
+ $ curl -X GET -I -H "Accept: image/webp" -H "Content-Type: image/jpeg" 'http://localhost:9292/example.jpg?quality=10'
26
+ HTTP/1.1 200 OK
27
+ Last-Modified: Thu, 20 Mar 2014 14:50:34 GMT
28
+ Content-Type: image/webp
29
+ Content-Length: 4346
30
+ X-Encoding-Time: 36ms
31
+ X-Compress: 5% - original: 78361 webp: 4346
32
+ Connection: keep-alive
33
+ Server: thin 1.6.1 codename Death Proof
34
+ ```
35
+
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,61 @@
1
+ require "rack/convert/webp/version"
2
+ require 'rack/accept_media_types'
3
+ require 'webp-ffi'
4
+ require 'tempfile'
5
+
6
+ module Rack
7
+ module Convert
8
+ class Webp
9
+ ACCEPT_CONTENT_TYPES = %w(
10
+ image/jpeg
11
+ image/jpg
12
+ image/png
13
+ image/tiff
14
+ )
15
+ DEFAULT_OPTIONS = {quality: 75}
16
+
17
+ def initialize(app, options = {})
18
+ @app = app
19
+ @encode_options = DEFAULT_OPTIONS.merge options
20
+ end
21
+
22
+ def call(env)
23
+ status, headers, response = @app.call(env)
24
+ request = Rack::Request.new(env)
25
+ media_types = request.accept_media_types
26
+
27
+ if response.kind_of?(Rack::File) &&
28
+ media_types.include?("image/webp") &&
29
+ ACCEPT_CONTENT_TYPES.include?(headers["Content-Type"]) &&
30
+ !request.params['original']
31
+ begin
32
+ webp_file = Tempfile.new('webp')
33
+ now = Time.now
34
+ ::WebP.encode(response.path, webp_file.path, @encode_options.merge(encoding_params request.params))
35
+ original_size = ::File.size(response.path)
36
+ webp_size = ::File.size(webp_file.path)
37
+ headers['X-Encoding-Time'] = "#{((Time.now - now).to_f * 1000).to_i}ms"
38
+ response = [webp_file.read]
39
+ headers['X-Compress'] = "#{((webp_size.to_f / original_size) * 100).to_i}% - original: #{original_size} webp: #{webp_size}"
40
+ headers['Content-Length'] = webp_size.to_s
41
+ headers['Content-Type'] = "image/webp"
42
+ ensure
43
+ webp_file.close
44
+ webp_file.unlink
45
+ end
46
+ end
47
+
48
+ [status, headers, response]
49
+ end
50
+
51
+ private
52
+ def encoding_params(params)
53
+ result = {}
54
+ params.each do |key, value|
55
+ result[key.to_sym] = value.to_i
56
+ end
57
+ result
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,7 @@
1
+ module Rack
2
+ module Convert
3
+ class Webp
4
+ VERSION = "0.0.1"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/convert/webp/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack-convert-webp"
8
+ spec.version = Rack::Convert::Webp::VERSION
9
+ spec.authors = ["Yuichi Tateno"]
10
+ spec.email = ["hotchpotch@gmail.com"]
11
+ spec.summary = %q{rack convert webp}
12
+ spec.description = %q{rack convert webp}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "rack"
22
+ spec.add_dependency "webp-ffi"
23
+ spec.add_dependency "rack-accept-media-types"
24
+ spec.add_development_dependency "bundler", "~> 1.5"
25
+ spec.add_development_dependency "rake"
26
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-convert-webp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Yuichi Tateno
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: webp-ffi
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rack-accept-media-types
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.5'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.5'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: rack convert webp
84
+ email:
85
+ - hotchpotch@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - lib/rack/convert/webp.rb
96
+ - lib/rack/convert/webp/version.rb
97
+ - rack-convert-webp.gemspec
98
+ homepage: ''
99
+ licenses:
100
+ - MIT
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.0.3
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: rack convert webp
122
+ test_files: []