sprockets-webp 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 642506ee4f9e939c6db5a5bc7162cdac1c92d48b
4
- data.tar.gz: 030e4ff9ae8e2e17fc0af84c2d8e709585ecbf25
3
+ metadata.gz: fb75dccba1ce27877e4f94b528e42d5d112127f3
4
+ data.tar.gz: 7feb113e4861e0b0af0aa8916c937654ad12fc1d
5
5
  SHA512:
6
- metadata.gz: d24725eaadae7d2643f70c74a60531c0aff1e1b7d472c0ec45809595873b9f78445a2bca5c35246103ae179f56ef9eba80c1d649964fa9d11c851f4c83c6377f
7
- data.tar.gz: 9835ac60ebcb656697b5825d77b15df2149459d734382955231fbfa74ddb2877e17409c5adcce74ed339a8a4fb4d8c96844d76a5becddab89a868d6dbc03040f
6
+ metadata.gz: b7734379b8f8f239302d684f420f2456a5948606421540ead672c409a5a135b1256a373d4b51d2b58d7f2b6752e613bf0bc6f627f11e816bfd4ee5a7200e7291
7
+ data.tar.gz: 97231c8f497b8a496292b8285778c1e2d020974c8ba300614a8e5beae2916c555d1b42215540a685aa041639392a3056f12ecbff8619d3bfe0468bd3e98cf8ff
data/README.md CHANGED
@@ -1,6 +1,15 @@
1
1
  # Sprockets::WebP
2
2
 
3
- This gem provides a Rails Asset Pipeline hook for converting PNG and JPEG assets to WebP format.
3
+ [![unstable](https://rawgithub.com/hughsk/stability-badges/master/dist/unstable.svg)](http://github.com/hughsk/stability-badges)
4
+
5
+ [![Gem Version](https://badge.fury.io/rb/sprockets-webp.png)](http://badge.fury.io/rb/sprockets-webp)
6
+ [![Code Climate](https://codeclimate.com/github/kavu/sprockets-webp.png)](https://codeclimate.com/github/kavu/sprockets-webp)
7
+ [![Dependency Status](https://gemnasium.com/kavu/sprockets-webp.png)](https://gemnasium.com/kavu/sprockets-webp)
8
+ [![Still Maintained](http://stillmaintained.com/kavu/sprockets-webp.png)](http://stillmaintained.com/kavu/sprockets-webp)
9
+
10
+ [![Coderwall](https://api.coderwall.com/kavu/endorsecount.png)](https://coderwall.com/kavu)
11
+
12
+ This gem provides a Rails Asset Pipeline hook for converting PNG and JPEG assets to the WebP format.
4
13
 
5
14
  ## Requirements
6
15
 
@@ -10,19 +19,23 @@ The main requirement is obviously [libwebp](https://developers.google.com/speed/
10
19
 
11
20
  If you're using Rails 4 you need to add gem to the ```:production``` group in to your application's Gemfile:
12
21
 
13
- group :produciton do
14
- # ...
15
- gem 'sprockets-webp'
16
- # ...
17
- end
22
+ ```ruby
23
+ group :production do
24
+ # ...
25
+ gem 'sprockets-webp'
26
+ # ...
27
+ end
28
+ ```
18
29
 
19
30
  In case of Rails 3, add it to the ```:assets``` group:
20
31
 
21
- group :assets do
22
- # ...
23
- gem 'sprockets-webp'
24
- # ...
25
- end
32
+ ```ruby
33
+ group :assets do
34
+ # ...
35
+ gem 'sprockets-webp'
36
+ # ...
37
+ end
38
+ ```
26
39
 
27
40
  And then execute:
28
41
 
@@ -34,11 +47,15 @@ Drop some PNGs and JPGs into ```app/assets/images``` and you can test converter
34
47
 
35
48
  ### Rails 3 Notice
36
49
 
37
- Minimal required version of Rails 3 is ```3.2.9```, because of Sprockets ```~> 2.2``` dependency requirement.
50
+ Minimal required version of Rails 3 is ```3.2.9```, because of Sprockets ```~> 2.2``` dependency requirement. In Rails 4 it just works.
51
+
52
+ ## Configuration
53
+
54
+ You can configure encode options for webp by using encode_options:
38
55
 
39
- ### Rails 4 Notice
56
+ Sprockets::WebP.encode_options = { quality: 90, lossless: 1, method: 5, alpha_filtering: 2 }
40
57
 
41
- I don't have any Rails 4 live apps right now, so I can't test this gem in "real", non synthetic, environment. So, if you have some problems, ideas or suggestions caused your Rails 4 application, please, feel free to contact me.
58
+ More options you can find in [web-ffi readme](https://github.com/le0pard/webp-ffi#encode-webp-image).
42
59
 
43
60
  ## Contributing
44
61
 
@@ -3,3 +3,19 @@
3
3
  require 'sprockets/webp/version'
4
4
  require 'sprockets/webp/converter'
5
5
  require 'sprockets/webp/railtie' if defined?(Rails::Engine)
6
+
7
+ module Sprockets
8
+ module WebP
9
+ # configure web encode options
10
+ #
11
+ # Sprockets::WebP.encode_options = { quality: 90, lossless: 1, method: 5, alpha_filtering: 2 }
12
+ #
13
+ class << self
14
+ attr_writer :encode_options
15
+
16
+ def encode_options
17
+ @encode_options ||= { quality: 90, lossless: 1, method: 5, alpha_filtering: 2 }
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,39 +1,68 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'tempfile'
4
+ require 'logger'
4
5
  require 'fileutils'
5
6
  require 'webp-ffi'
6
7
 
7
8
  module Sprockets
8
9
  module WebP
9
10
  class Converter
10
- def self.process(app, context, data)
11
- # Application Config alias
12
- config = app.config.assets
13
-
14
- # If Application Assets Digests enabled - Add Digest
15
- digest = config.digest ? "-#{context.environment.digest.update(data).hexdigest}" : nil
16
- file_name = context.logical_path # Original File name w/o extension
17
- file_ext = context.pathname.extname # Original File extension
18
- webp_file = "#{file_name}#{digest}#{file_ext}.webp" # WebP File fullname
19
-
20
- # WebP File Pathname
21
- webp_path = Pathname.new File.join(app.root, 'public', config.prefix, webp_file)
22
-
23
- # Create Directory for both Files, unless already exists
24
- FileUtils.mkdir_p(webp_path.dirname) unless Dir.exists?(webp_path.dirname)
25
-
26
- # Create Temp File with Original File binary data
27
- Tempfile.open('webp') do |file|
28
- file.binmode
29
- file.write(data)
30
- file.close
31
-
32
- # Encode Original File Temp copy to WebP File Pathname
33
- ::WebP.encode(file.path, webp_path.to_path)
11
+ class << self
12
+
13
+ attr_reader :context
14
+
15
+ def process(app, context, data)
16
+ @context = context
17
+ # Application Config alias
18
+ config = app.config.assets
19
+
20
+ # If Application Assets Digests enabled - Add Digest
21
+ digest = config.digest ? "-#{context.environment.digest.update(data).hexdigest}" : nil
22
+ file_name = context.logical_path # Original File name w/o extension
23
+ file_ext = context.pathname.extname # Original File extension
24
+ webp_file = "#{file_name}#{digest}#{file_ext}.webp" # WebP File fullname
25
+
26
+ # WebP File Pathname
27
+ webp_path = Pathname.new File.join(app.root, 'public', config.prefix, webp_file)
28
+
29
+ # Create Directory for both Files, unless already exists
30
+ FileUtils.mkdir_p(webp_path.dirname) unless Dir.exists?(webp_path.dirname)
31
+
32
+ # encode to webp
33
+ encode_to_webp(data, webp_path.to_path, webp_file)
34
+
35
+ data
36
+ end
37
+
38
+ private
39
+
40
+ def encode_to_webp(data, webp_path, webp_file = "")
41
+ # Create Temp File with Original File binary data
42
+ Tempfile.open('webp') do |file|
43
+ file.binmode
44
+ file.write(data)
45
+ file.close
46
+
47
+ # Encode Original File Temp copy to WebP File Pathname
48
+ begin
49
+ ::WebP.encode(file.path, webp_path, Sprockets::WebP.encode_options)
50
+ rescue => e
51
+ logger.warn "Webp convertion error of image #{webp_file}. Error info: #{e.message}"
52
+ end
53
+ end
54
+ end
55
+
56
+ def logger
57
+ if @context && @context.environment
58
+ @context.environment.logger
59
+ else
60
+ logger = Logger.new($stderr)
61
+ logger.level = Logger::FATAL
62
+ logger
63
+ end
34
64
  end
35
65
 
36
- data
37
66
  end
38
67
  end
39
68
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Sprockets
4
4
  module WebP
5
- VERSION = '0.2.0'
5
+ VERSION = '0.3.0'
6
6
  end
7
7
  end
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
20
20
  spec.required_ruby_version = '>= 1.9.3'
21
21
 
22
22
  spec.add_dependency 'sprockets', '~> 2.2'
23
- spec.add_dependency 'webp-ffi', '~> 0.1.7'
23
+ spec.add_dependency 'webp-ffi', '~> 0.2.0'
24
24
 
25
25
  spec.add_development_dependency 'bundler', '~> 1.3'
26
26
  spec.add_development_dependency 'rake'
metadata CHANGED
@@ -1,69 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sprockets-webp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Max Riveiro
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-20 00:00:00.000000000 Z
11
+ date: 2014-01-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sprockets
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '2.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '2.2'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: webp-ffi
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 0.1.7
33
+ version: 0.2.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ~>
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 0.1.7
40
+ version: 0.2.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: '1.3'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ~>
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.3'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rake
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  description: Sprockets converter of PNG and JPEG assets to WebP
@@ -73,7 +73,7 @@ executables: []
73
73
  extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
- - .gitignore
76
+ - ".gitignore"
77
77
  - Gemfile
78
78
  - LICENSE.txt
79
79
  - README.md
@@ -93,17 +93,17 @@ require_paths:
93
93
  - lib
94
94
  required_ruby_version: !ruby/object:Gem::Requirement
95
95
  requirements:
96
- - - '>='
96
+ - - ">="
97
97
  - !ruby/object:Gem::Version
98
98
  version: 1.9.3
99
99
  required_rubygems_version: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - '>='
101
+ - - ">="
102
102
  - !ruby/object:Gem::Version
103
103
  version: '0'
104
104
  requirements: []
105
105
  rubyforge_project:
106
- rubygems_version: 2.0.3
106
+ rubygems_version: 2.2.0
107
107
  signing_key:
108
108
  specification_version: 4
109
109
  summary: Sprockets converter of PNG and JPEG assets to WebP