jpg_or_webp 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8f9129d8fb99714d3a78f738f94a4deed7065f4b9ee99b0315f517d74b7f6d2a
4
+ data.tar.gz: 044ba486471eda06a6e7dd79a4885d8d0c6259fee6a00d9f47ac5759ee8597d4
5
+ SHA512:
6
+ metadata.gz: ca66da061b95ca668b6ee1de5686c9413b8e40840f4929aa292027f32b37c6e895a58e001ad939a0cfa2a61f2a569cca4ab4abfe0b39ee94a3b2c3f12ab1fe0f
7
+ data.tar.gz: 5bdb265825c3c74c78f8945a44e103179d4182b018aed0694e3d1f96db72090d1e51411bd65e05a5ea0f838ed4271332ee891ff2c340d1ad3795a6ba529e13dd
data/.editorconfig ADDED
@@ -0,0 +1,2 @@
1
+ [*.rb]
2
+ indent_size = 2
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,7 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+ NewCops: enable
4
+ Style/Documentation:
5
+ Enabled: false
6
+ Metrics/BlockLength:
7
+ Max: 30
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.1.2
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at aldavigdis@aldavigdis.is. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in jpg_or_webp.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,102 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ jpg_or_webp (0.1.1)
5
+ browser (~> 5.3)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (7.0.4.2)
11
+ concurrent-ruby (~> 1.0, >= 1.0.2)
12
+ i18n (>= 1.6, < 2)
13
+ minitest (>= 5.1)
14
+ tzinfo (~> 2.0)
15
+ ast (2.4.2)
16
+ browser (5.3.1)
17
+ concurrent-ruby (1.2.0)
18
+ csv (3.2.6)
19
+ diff-lcs (1.5.0)
20
+ ffi (1.15.5)
21
+ fileutils (1.7.0)
22
+ i18n (1.12.0)
23
+ concurrent-ruby (~> 1.0)
24
+ json (2.6.3)
25
+ language_server-protocol (3.17.0.3)
26
+ listen (3.8.0)
27
+ rb-fsevent (~> 0.10, >= 0.10.3)
28
+ rb-inotify (~> 0.9, >= 0.9.10)
29
+ logger (1.5.3)
30
+ minitest (5.17.0)
31
+ parallel (1.22.1)
32
+ parser (3.2.0.0)
33
+ ast (~> 2.4.1)
34
+ rainbow (3.1.1)
35
+ rake (13.0.6)
36
+ rb-fsevent (0.11.2)
37
+ rb-inotify (0.10.1)
38
+ ffi (~> 1.0)
39
+ rbs (2.8.4)
40
+ regexp_parser (2.6.2)
41
+ rexml (3.2.5)
42
+ rspec (3.12.0)
43
+ rspec-core (~> 3.12.0)
44
+ rspec-expectations (~> 3.12.0)
45
+ rspec-mocks (~> 3.12.0)
46
+ rspec-core (3.12.0)
47
+ rspec-support (~> 3.12.0)
48
+ rspec-expectations (3.12.2)
49
+ diff-lcs (>= 1.2.0, < 2.0)
50
+ rspec-support (~> 3.12.0)
51
+ rspec-mocks (3.12.3)
52
+ diff-lcs (>= 1.2.0, < 2.0)
53
+ rspec-support (~> 3.12.0)
54
+ rspec-support (3.12.0)
55
+ rubocop (1.44.1)
56
+ json (~> 2.3)
57
+ parallel (~> 1.10)
58
+ parser (>= 3.2.0.0)
59
+ rainbow (>= 2.2.2, < 4.0)
60
+ regexp_parser (>= 1.8, < 3.0)
61
+ rexml (>= 3.2.5, < 4.0)
62
+ rubocop-ast (>= 1.24.1, < 2.0)
63
+ ruby-progressbar (~> 1.7)
64
+ unicode-display_width (>= 2.4.0, < 3.0)
65
+ rubocop-ast (1.24.1)
66
+ parser (>= 3.1.1.0)
67
+ ruby-progressbar (1.11.0)
68
+ securerandom (0.2.2)
69
+ steep (1.3.0)
70
+ activesupport (>= 5.1)
71
+ csv (>= 3.0.9)
72
+ fileutils (>= 1.1.0)
73
+ json (>= 2.1.0)
74
+ language_server-protocol (>= 3.15, < 4.0)
75
+ listen (~> 3.0)
76
+ logger (>= 1.3.0)
77
+ parallel (>= 1.0.0)
78
+ parser (>= 3.1)
79
+ rainbow (>= 2.2.2, < 4.0)
80
+ rbs (>= 2.8.0)
81
+ securerandom (>= 0.1)
82
+ strscan (>= 1.0.0)
83
+ terminal-table (>= 2, < 4)
84
+ strscan (3.0.5)
85
+ terminal-table (3.0.2)
86
+ unicode-display_width (>= 1.1.1, < 3)
87
+ tzinfo (2.0.6)
88
+ concurrent-ruby (~> 1.0)
89
+ unicode-display_width (2.4.2)
90
+
91
+ PLATFORMS
92
+ x86_64-linux
93
+
94
+ DEPENDENCIES
95
+ jpg_or_webp!
96
+ rake (~> 13.0)
97
+ rspec (~> 3.0)
98
+ rubocop (~> 1.21)
99
+ steep (~> 1.3)
100
+
101
+ BUNDLED WITH
102
+ 2.3.25
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Alda Vigdís Skarphéðinsdóttir
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,119 @@
1
+ # JpgOrWebp
2
+
3
+ Sniffs the useragent to choose the more appropriate image format between JPEG
4
+ and WebP.
5
+
6
+ ## What is it good for?
7
+
8
+ While we may want to serve WebP images to every browser that supports it (and
9
+ also make Google Lighthouse and PageSpeed Insights scores higher), some browser
10
+ do not support the format yet.
11
+
12
+ JPEG support can be assumed to be enabled by default while some browsers and
13
+ many legacy versions of the same browsers may or may not support modern image
14
+ formats like WebP.
15
+
16
+ While we can use something like the `<picture>` and `<source>` elements with the
17
+ appropriate `type` attribute, in some cases such as legacy applications, we may
18
+ prefer to stick with the good-old `<img>` element and use its `srcset` and
19
+ `sizes` attributes to serve responsive images.
20
+
21
+ In a Rails application that uses Active Storage or anything similar to keep
22
+ record of and serve each image and their various variants, this gem may be used
23
+ as an optimisation tool.
24
+
25
+ ## Installation
26
+
27
+ Install the gem and add to the application's Gemfile by executing:
28
+
29
+ $ bundle add jpg_or_webp
30
+
31
+ If bundler is not being used to manage dependencies:
32
+
33
+ $ gem install jpg_or_webp
34
+
35
+ ## Usage
36
+
37
+ ### In a Rack (Rails or Sinatra) app
38
+
39
+ In Rails and Sinatra, we can use `request.user_agent` to feed the current
40
+ request's useragent string into `JpgOrWebp`.
41
+
42
+ ```ruby
43
+ jpg_or_webp = JpgOrWebp.new(request.user_agent)
44
+ jpg_or_webp.image_format
45
+ => "jpg"
46
+ ```
47
+
48
+ ### Use with Active Storage
49
+
50
+ Let's say you want to pull an image variant from Active Storage based on the
51
+ current useragent's image format support. Then you can do it like this:
52
+
53
+ ```ruby
54
+ book = Book.find('666')
55
+ jpg_or_webp = JpgOrWebp.new(request.user_agent)
56
+
57
+ book_cover = book.cover_image.variant(
58
+ resize_to_limit: [1600, nil],
59
+ format: jpg_or_webp.image_format,
60
+ saver: { quality: 80 }
61
+ )
62
+ =>
63
+ #<ActiveStorage::VariantWithRecord:0x00007f1b770207a8
64
+ @blob=
65
+ #<ActiveStorage::Blob:0x00007f1b7706dd50
66
+ id: 12636,
67
+ key: "7oei4398h63ry3yfsnwwbc6t9xtp",
68
+ filename: "666.jpg",
69
+ content_type: "image/jpeg",
70
+ metadata: {"identified"=>true, "width"=>1500, "height"=>2100, "analyzed"=>true},
71
+ service_name: "google",
72
+ byte_size: 3385916,
73
+ checksum: "lkTrv2es/VwXH7MmGJg9/w==",
74
+ created_at: Thu, 06 Jan 2022 19:41:22.104881000 UTC +00:00>,
75
+ @variation=#<ActiveStorage::Variation:0x00007f1b77020960 @transformations={:format=>"jpg", :resize_to_limit=>[1600, nil], :saver=>{:quality=>80}}>>
76
+ ```
77
+
78
+ ### Other uses
79
+
80
+ If you are not using `JpgOrWebp` in the context of a Rack application but are
81
+ able to get a useragent string from somewhere else, you can feed it manually
82
+ into JpgOrWebp like so:
83
+
84
+ ```ruby
85
+ useragent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'
86
+ jpg_or_webp = JpgOrWebp.new(useragent)
87
+ jpg_or_webp.image_format
88
+ => "webp"
89
+ ```
90
+
91
+ ## Development
92
+
93
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
94
+ `rake spec` to run the tests. You can also run `bin/console` for an interactive
95
+ prompt that will allow you to experiment.
96
+
97
+ To install this gem onto your local machine, run `bundle exec rake install`.
98
+ To release a new version, update the version number in `version.rb`, and then
99
+ run `bundle exec rake release`, which will create a git tag for the version,
100
+ push git commits and the created tag, and push the `.gem` file to
101
+ [rubygems.org](https://rubygems.org).
102
+
103
+ ## Contributing
104
+
105
+ Bug reports and pull requests are welcome on GitHub at
106
+ https://github.com/aldvigdis/jpg_or_webp. This project is intended to be a safe,
107
+ welcoming space for collaboration, and contributors are expected to adhere to
108
+ the [code of conduct](https://github.com/aldvigdis/jpg_or_webp/blob/master/CODE_OF_CONDUCT.md).
109
+
110
+ ## License
111
+
112
+ The gem is available as open source under the terms of the
113
+ [MIT License](https://opensource.org/licenses/MIT).
114
+
115
+ ## Code of Conduct
116
+
117
+ Everyone interacting in the JpgOrWebp project's codebases, issue trackers, chat
118
+ rooms and mailing lists is expected to follow the
119
+ [code of conduct](https://github.com/aldavigdis/jpg_or_webp/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/Steepfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ target :lib do
4
+ signature 'sig'
5
+ check 'lib'
6
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'browser'
4
+
5
+ class JpgOrWebp
6
+ VERSION = '0.1.1'
7
+ DEFAULT_FORMAT = 'webp' # Defaulting on using WebP.
8
+ DEFAULT_USER_AGENT = 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; ' \
9
+ 'Trident/4.0)' # Yes, it's IE. Don't kinkshame me.
10
+
11
+ def initialize(user_agent = DEFAULT_USER_AGENT)
12
+ @user_agent = user_agent
13
+ @browser = Browser.new(@user_agent)
14
+ end
15
+
16
+ def image_format
17
+ return 'jpg' if @browser.ie?
18
+ return 'jpg' if @browser.safari? && @browser.platform.mac?('<11.6')
19
+ return 'jpg' if @browser.platform.ios?('<14')
20
+ return 'jpg' if @browser.platform.kai_os?
21
+
22
+ DEFAULT_FORMAT
23
+ end
24
+ end
@@ -0,0 +1,12 @@
1
+ class JpgOrWebp
2
+ VERSION: String
3
+ DEFAULT_FORMAT: String
4
+ DEFAULT_USER_AGENT: String
5
+
6
+ @user_agent: String?
7
+ @browser: untyped
8
+
9
+ def initialize : (?String user_agent) -> void
10
+
11
+ def image_format: () -> (String)
12
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jpg_or_webp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Alda Vigdís Skarphéðinsdóttir
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-02-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: steep
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '13.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '13.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.21'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.21'
69
+ - !ruby/object:Gem::Dependency
70
+ name: browser
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.3'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '5.3'
83
+ description: Useragent sniffing tool to choose the more appropriate image format between
84
+ JPEG and WebP.
85
+ email:
86
+ - aldavigdis@aldavigdis.is
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".editorconfig"
92
+ - ".rspec"
93
+ - ".rubocop.yml"
94
+ - ".ruby-version"
95
+ - CODE_OF_CONDUCT.md
96
+ - Gemfile
97
+ - Gemfile.lock
98
+ - LICENSE.txt
99
+ - README.md
100
+ - Rakefile
101
+ - Steepfile
102
+ - lib/jpg_or_webp.rb
103
+ - sig/jpg_or_webp.rbs
104
+ homepage: https://github.com/aldavigdis/jpg_or_webp/
105
+ licenses:
106
+ - MIT
107
+ metadata:
108
+ homepage_uri: https://github.com/aldavigdis/jpg_or_webp/
109
+ source_code_uri: https://github.com/aldavigdis/jpg_or_webp/
110
+ rubygems_mfa_required: 'true'
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: 2.6.0
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubygems_version: 3.3.7
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Find out if we should serve JPEG or WebP images in Ruby.
130
+ test_files: []