imgix 3.4.0 → 4.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +2 -0
- data/.travis.yml +2 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +1 -1
- data/README.md +2 -37
- data/imgix.gemspec +17 -19
- data/lib/imgix.rb +4 -4
- data/lib/imgix/client.rb +64 -55
- data/lib/imgix/path.rb +89 -77
- data/lib/imgix/version.rb +1 -1
- metadata +7 -54
- data/.github/CODEOWNERS +0 -2
- data/.github/ISSUE_TEMPLATE/bug_report.md +0 -28
- data/.github/ISSUE_TEMPLATE/feature_request.md +0 -27
- data/.github/ISSUE_TEMPLATE/question.md +0 -17
- data/.github/pull_request_template.md +0 -73
- data/lib/imgix/param_helpers.rb +0 -19
- data/test/test_helper.rb +0 -12
- data/test/units/domains_test.rb +0 -17
- data/test/units/param_helpers_test.rb +0 -23
- data/test/units/path_test.rb +0 -182
- data/test/units/purge_test.rb +0 -70
- data/test/units/srcset_test.rb +0 -755
- data/test/units/url_test.rb +0 -80
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a91b4da0e3b9063761f986110ff0ab379938986186642e08ec59be0ae5601cad
|
4
|
+
data.tar.gz: 61a69f51d3d453480fa9f5acc496b245f65f94d7088a704fe0ee31fc4f4146dc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ae1e01d77d40bdccf28cadebb0d4a75704f1da9d0d6619c102b0e05bae680f3801675d1674b67b0fb94fb3ed5e1576e386d8c31cda5bcab252a0e997099245ff
|
7
|
+
data.tar.gz: b715696707c314d2a9bf047ff32ba579cf12fca5115601cc59d5db88e8407a09012a1783bb194c10a5bcc910726b8c4ca0d81f44c52e4ce109c17563baa7f4bb
|
data/.rubocop.yml
ADDED
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -3,6 +3,11 @@
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
4
4
|
This project adheres to [Semantic Versioning](http://semver.org/).
|
5
5
|
|
6
|
+
## [4.0.0](https://github.com/imgix/imgix-rb/compare/3.4.0...4.0.0) - August 26, 2020
|
7
|
+
|
8
|
+
* deprecate: host-usage, param_helpers, and prefix-1-ary ([#91](https://github.com/imgix/imgix-rb/pull/91))
|
9
|
+
* fix: update purge operation api keys ([#93](https://github.com/imgix/imgix-rb/pull/93))
|
10
|
+
|
6
11
|
## [3.4.0](https://github.com/imgix/imgix-rb/compare/3.3.1...3.4.0) - August 12, 2020
|
7
12
|
|
8
13
|
* fix: deprecate api key versions ([#87](https://github.com/imgix/imgix-rb/pull/87))
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -48,36 +48,13 @@ Initialize a client with a `:domain` and your `:secure_url_token`. By default, H
|
|
48
48
|
|
49
49
|
Call `Imgix::Client#path` with the resource path to get an `Imgix::Path` object back. You can then manipulate the path parameters, and call `Imgix#Path#to_url` when you're done.
|
50
50
|
|
51
|
-
```
|
51
|
+
```rb
|
52
52
|
client = Imgix::Client.new(domain: 'your-subdomain.imgix.net', secure_url_token: 'your-token')
|
53
53
|
|
54
54
|
client.path('/images/demo.png').to_url(w: 200)
|
55
55
|
#=> https://your-subdomain.imgix.net/images/demo.png?w=200&s=2eadddacaa9bba4b88900d245f03f51e
|
56
|
-
|
57
|
-
# OR
|
58
|
-
path = client.path('/images/demo.png')
|
59
|
-
path.width = 200
|
60
|
-
path.to_url
|
61
|
-
|
62
|
-
# OR
|
63
|
-
client.path('/images/demo.png').width(200).height(300).to_url
|
64
|
-
|
65
|
-
# Some other tricks
|
66
|
-
path.defaults.width(300).to_url # Resets parameters
|
67
|
-
path.rect(x: 0, y: 50, width: 200, height: 300).to_url # Rect helper
|
68
|
-
```
|
69
|
-
|
70
|
-
**Deprecation Notice:** Usage of `:host` has been deprecated and will become invalid in the next major release.
|
71
|
-
|
72
|
-
In the previous version, `:host` can be used to specify a domain for an `Imgix::Client` like so:
|
73
|
-
|
74
|
-
```ruby
|
75
|
-
Imgix::Client.new(host: 'demo.imgix.net', secure_url_token: 'token')
|
76
56
|
```
|
77
57
|
|
78
|
-
Code using `:host` like the above will continue to work until the next major release. While using `:host` will remain valid until the next major release, its usage will result in a deprecation warning. In order to resolve these deprecation warnings before upgrading to the new release, use `:domain` instead of `:host`.
|
79
|
-
|
80
|
-
|
81
58
|
## Srcset Generation
|
82
59
|
|
83
60
|
The imgix gem allows for generation of custom `srcset` attributes, which can be invoked through `Imgix::Path#to_srcset`. By default, the `srcset` generated will allow for responsive size switching by building a list of image-width mappings.
|
@@ -218,21 +195,9 @@ https://testing.imgix.net/image.jpg?w=100&dpr=4&q=23 4x,
|
|
218
195
|
https://testing.imgix.net/image.jpg?w=100&dpr=5&q=20 5x
|
219
196
|
```
|
220
197
|
|
221
|
-
## Multiple Parameters
|
222
|
-
|
223
|
-
When the imgix api requires multiple parameters you have to use the method rather than an accessor.
|
224
|
-
|
225
|
-
For example to use the noise reduction:
|
226
|
-
|
227
|
-
``` ruby
|
228
|
-
path.noise_reduction(50,50)
|
229
|
-
```
|
230
|
-
|
231
198
|
## Purge Cache
|
232
199
|
|
233
|
-
|
234
|
-
|
235
|
-
If you need to remove or update an image on imgix, you can purge it from our cache by initializing a client with your api_key, then calling Imgix::Client#purge with the resource path.
|
200
|
+
If you need to remove or update an image on imgix, you can purge it from our cache by initializing a client with your [API key](http://dashboard.imgix.com/api-keys), then calling `Imgix::Client#purge` with the resource path.
|
236
201
|
|
237
202
|
```ruby
|
238
203
|
client = Imgix::Client.new(domain: 'your-subdomain.imgix.net', api_key: 'your-key')
|
data/imgix.gemspec
CHANGED
@@ -1,30 +1,28 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative
|
3
|
+
require_relative "lib/imgix/version"
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
|
-
spec.name =
|
6
|
+
spec.name = "imgix"
|
7
7
|
spec.version = Imgix::VERSION
|
8
|
-
spec.authors = [
|
9
|
-
spec.email = [
|
10
|
-
spec.description =
|
11
|
-
spec.summary =
|
12
|
-
spec.homepage =
|
13
|
-
spec.license =
|
8
|
+
spec.authors = ["Kelly Sutton", "Sam Soffes", "Ryan LeFevre", "Antony Denyer", "Paul Straw", "Sherwin Heydarbeygi"]
|
9
|
+
spec.email = ["kelly@imgix.com", "sam@soff.es", "ryan@layervault.com", "email@antonydenyer.co.uk", "paul@imgix.com", "sherwin@imgix.com"]
|
10
|
+
spec.description = "Easily create and sign imgix URLs."
|
11
|
+
spec.summary = "Official Ruby Gem for easily creating and signing imgix URLs."
|
12
|
+
spec.homepage = "https://github.com/imgix/imgix-rb"
|
13
|
+
spec.license = "MIT"
|
14
14
|
|
15
15
|
spec.metadata = {
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
16
|
+
"bug_tracker_uri" => "https://github.com/imgix/imgix-rb/issues",
|
17
|
+
"changelog_uri" => "https://github.com/imgix/imgix-rb/blob/main/CHANGELOG.md",
|
18
|
+
"documentation_uri" => "https://www.rubydoc.info/gems/imgix/#{spec.version}",
|
19
|
+
"source_code_uri" => "https://github.com/imgix/imgix-rb/tree/#{spec.version}"
|
20
20
|
}
|
21
21
|
|
22
|
-
spec.files =
|
23
|
-
|
24
|
-
|
25
|
-
spec.require_paths = ['lib']
|
22
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
23
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|.github)/}) }
|
24
|
+
end
|
26
25
|
|
27
|
-
spec.
|
28
|
-
spec.
|
29
|
-
spec.add_development_dependency 'webmock'
|
26
|
+
spec.require_paths = ["lib"]
|
27
|
+
spec.required_ruby_version = ">= 1.9.0"
|
30
28
|
end
|
data/lib/imgix.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
3
|
+
require "imgix/version"
|
4
|
+
require "imgix/client"
|
5
|
+
require "imgix/path"
|
6
6
|
|
7
7
|
module Imgix
|
8
8
|
# regex pattern used to determine if a domain is valid
|
@@ -22,7 +22,7 @@ module Imgix
|
|
22
22
|
increment_percentage = tolerance || DEFAULT_WIDTH_TOLERANCE
|
23
23
|
|
24
24
|
unless increment_percentage.is_a?(Numeric) && increment_percentage > 0
|
25
|
-
width_increment_error =
|
25
|
+
width_increment_error = "error: `width_tolerance` must be a positive `Numeric` value"
|
26
26
|
raise ArgumentError, width_increment_error
|
27
27
|
end
|
28
28
|
|
data/lib/imgix/client.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
3
|
+
require "digest"
|
4
|
+
require "net/http"
|
5
|
+
require "uri"
|
6
|
+
require "json"
|
7
7
|
|
8
8
|
module Imgix
|
9
9
|
class Client
|
@@ -11,87 +11,96 @@ module Imgix
|
|
11
11
|
|
12
12
|
def initialize(options = {})
|
13
13
|
options = DEFAULTS.merge(options)
|
14
|
-
|
15
|
-
|
16
|
-
host_deprecated = "Warning: The identifier `host' has been deprecated and " \
|
17
|
-
"will\nappear as `domain' in the next major version, e.g. " \
|
18
|
-
"`@host'\nbecomes `@domain', `options[:host]' becomes " \
|
19
|
-
"`options[:domain]'.\n"
|
20
|
-
|
21
|
-
if host
|
22
|
-
warn host_deprecated
|
23
|
-
@host = host
|
24
|
-
elsif domain
|
25
|
-
@host = domain
|
26
|
-
else
|
27
|
-
@host = host
|
28
|
-
end
|
14
|
+
@domain = options[:domain]
|
29
15
|
|
30
|
-
|
16
|
+
validate_domain!
|
31
17
|
|
32
18
|
@secure_url_token = options[:secure_url_token]
|
33
19
|
@api_key = options[:api_key]
|
34
20
|
@use_https = options[:use_https]
|
35
21
|
@include_library_param = options.fetch(:include_library_param, true)
|
36
|
-
@library = options.fetch(:library_param,
|
22
|
+
@library = options.fetch(:library_param, "rb")
|
37
23
|
@version = options.fetch(:library_version, Imgix::VERSION)
|
38
24
|
end
|
39
25
|
|
40
26
|
def path(path)
|
41
|
-
p = Path.new(
|
27
|
+
p = Path.new(prefix, @secure_url_token, path)
|
42
28
|
p.ixlib("#{@library}-#{@version}") if @include_library_param
|
43
29
|
p
|
44
30
|
end
|
45
31
|
|
46
32
|
def purge(path)
|
47
|
-
|
48
|
-
"Warning: Your `api_key` will no longer work after upgrading to\n" \
|
49
|
-
"imgix-rb version >= 4.0.0.\n"
|
50
|
-
warn api_key_deprecated
|
51
|
-
|
52
|
-
api_key_error = 'A valid api key is required to send purge requests'
|
33
|
+
api_key_error = "A valid API key is required to send purge requests"
|
53
34
|
raise api_key_error if @api_key.nil?
|
54
35
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
36
|
+
endpoint = URI.parse("https://api.imgix.com/api/v1/purge")
|
37
|
+
# Ensure the path has been prefixed with '/'.
|
38
|
+
path = path.start_with?("/") ? path : "/#{path}"
|
39
|
+
url = prefix + path
|
59
40
|
|
60
|
-
req =
|
61
|
-
req.basic_auth @api_key, ''
|
62
|
-
req.set_form_data({ url: url })
|
41
|
+
req = create_request(endpoint, url, :json_data_from)
|
63
42
|
|
64
|
-
sock = Net::HTTP.new(
|
43
|
+
sock = Net::HTTP.new(endpoint.host, endpoint.port)
|
65
44
|
sock.use_ssl = true
|
66
|
-
|
67
|
-
|
68
|
-
res
|
45
|
+
sock.start { |http| http.request(req) }
|
69
46
|
end
|
70
47
|
|
71
|
-
def prefix
|
72
|
-
|
73
|
-
"in the next major version.\n"
|
74
|
-
warn msg
|
75
|
-
new_prefix
|
48
|
+
def prefix
|
49
|
+
"#{@use_https ? 'https' : 'http'}://#{@domain}"
|
76
50
|
end
|
77
51
|
|
78
|
-
|
79
|
-
|
52
|
+
private
|
53
|
+
|
54
|
+
# Create a request object by specifying it's endpoint, resource, and
|
55
|
+
# an optional data_fmt.
|
56
|
+
#
|
57
|
+
# `endpoint` must be a valid URI object
|
58
|
+
# `resource` must be a valid URL designating the resource to be purged
|
59
|
+
# `data_fmt` must be a valid method or Proc object
|
60
|
+
#
|
61
|
+
# Specify a `data_fmt` method when a resource (URL) requires
|
62
|
+
# additional formatting before being included in the request body.
|
63
|
+
# By default, the data format is specified by the `json_data_from`
|
64
|
+
# method.
|
65
|
+
def create_request(endpoint, resource, data_fmt = :json_data_from)
|
66
|
+
req = Net::HTTP::Post.new(endpoint.path)
|
67
|
+
req["Content-Type"] = "application/vnd.api+json"
|
68
|
+
req["Authorization"] = "Bearer #{@api_key}"
|
69
|
+
req["User-Agent"] = "imgix #{@library}-#{@version}"
|
70
|
+
|
71
|
+
if data_fmt.is_a?(Proc)
|
72
|
+
req.body = data_fmt.call(resource)
|
73
|
+
elsif data_fmt.is_a?(Symbol)
|
74
|
+
req.body = send(data_fmt, resource)
|
75
|
+
else
|
76
|
+
fmt_arg_error = "`fmt' is required to be of class Symbol or " \
|
77
|
+
"Proc but was found to be\n\s\sof class #{data_fmt.class}\n"
|
78
|
+
raise ArgumentError, fmt_arg_error
|
79
|
+
end
|
80
|
+
|
81
|
+
req
|
80
82
|
end
|
81
83
|
|
82
|
-
|
84
|
+
def json_data_from(url)
|
85
|
+
{
|
86
|
+
data: {
|
87
|
+
attributes: {
|
88
|
+
url: url
|
89
|
+
},
|
90
|
+
type: "purges"
|
91
|
+
}
|
92
|
+
}.to_json
|
93
|
+
end
|
83
94
|
|
84
|
-
def
|
85
|
-
|
86
|
-
raise ArgumentError,
|
95
|
+
def validate_domain!
|
96
|
+
domain_error = "The :domain option must be specified"
|
97
|
+
raise ArgumentError, domain_error if @domain.nil?
|
87
98
|
|
88
|
-
domain_error =
|
89
|
-
|
99
|
+
domain_error = "Domains must be passed in as fully-qualified"\
|
100
|
+
"domain names and should not include a protocol"\
|
90
101
|
'or any path element, i.e. "example.imgix.net"'\
|
91
102
|
|
92
|
-
if @
|
93
|
-
raise ArgumentError, domain_error
|
94
|
-
end
|
103
|
+
raise ArgumentError, domain_error if @domain.match(DOMAIN_REGEX).nil?
|
95
104
|
end
|
96
105
|
end
|
97
106
|
end
|
data/lib/imgix/path.rb
CHANGED
@@ -1,44 +1,20 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require 'imgix/param_helpers'
|
3
|
+
require "base64"
|
4
|
+
require "cgi/util"
|
5
|
+
require "erb"
|
7
6
|
|
8
7
|
module Imgix
|
9
8
|
class Path
|
10
|
-
include ParamHelpers
|
11
9
|
|
12
|
-
|
13
|
-
width: :w,
|
14
|
-
height: :h,
|
15
|
-
rotation: :rot,
|
16
|
-
noise_reduction: :nr,
|
17
|
-
sharpness: :sharp,
|
18
|
-
exposure: :exp,
|
19
|
-
vibrance: :vib,
|
20
|
-
saturation: :sat,
|
21
|
-
brightness: :bri,
|
22
|
-
contrast: :con,
|
23
|
-
highlight: :high,
|
24
|
-
shadow: :shad,
|
25
|
-
gamma: :gam,
|
26
|
-
pixelate: :px,
|
27
|
-
halftone: :htn,
|
28
|
-
watermark: :mark,
|
29
|
-
text: :txt,
|
30
|
-
format: :fm,
|
31
|
-
quality: :q
|
32
|
-
}.freeze
|
33
|
-
|
34
|
-
def initialize(prefix, secure_url_token, path = '/')
|
10
|
+
def initialize(prefix, secure_url_token, path = "/")
|
35
11
|
@prefix = prefix
|
36
12
|
@secure_url_token = secure_url_token
|
37
13
|
@path = path
|
38
14
|
@options = {}
|
39
15
|
|
40
16
|
@path = CGI.escape(@path) if /^https?/ =~ @path
|
41
|
-
@path = "/#{@path}" if @path[0] !=
|
17
|
+
@path = "/#{@path}" if @path[0] != "/"
|
42
18
|
@target_widths = TARGET_WIDTHS.call(DEFAULT_WIDTH_TOLERANCE, MIN_WIDTH, MAX_WIDTH)
|
43
19
|
end
|
44
20
|
|
@@ -49,7 +25,7 @@ module Imgix
|
|
49
25
|
url = @prefix + path_and_params
|
50
26
|
|
51
27
|
if @secure_url_token
|
52
|
-
url += (has_query? ?
|
28
|
+
url += (has_query? ? "&" : "?") + "s=#{signature}"
|
53
29
|
end
|
54
30
|
|
55
31
|
@options = prev_options
|
@@ -61,53 +37,97 @@ module Imgix
|
|
61
37
|
self
|
62
38
|
end
|
63
39
|
|
40
|
+
def ixlib(lib_version)
|
41
|
+
@options[:ixlib] = lib_version
|
42
|
+
end
|
43
|
+
|
44
|
+
def ixlib=(lib_version)
|
45
|
+
@options[:ixlib] = lib_version
|
46
|
+
end
|
47
|
+
|
48
|
+
def to_srcset(options: {}, **params)
|
49
|
+
prev_options = @options.dup
|
50
|
+
@options.merge!(params)
|
51
|
+
|
52
|
+
width = @options[:w]
|
53
|
+
height = @options[:h]
|
54
|
+
aspect_ratio = @options[:ar]
|
55
|
+
|
56
|
+
srcset = if width || (height && aspect_ratio)
|
57
|
+
build_dpr_srcset(options: options, params: @options)
|
58
|
+
else
|
59
|
+
build_srcset_pairs(options: options, params: @options)
|
60
|
+
end
|
61
|
+
|
62
|
+
@options = prev_options
|
63
|
+
srcset
|
64
|
+
end
|
65
|
+
|
66
|
+
ALIASES = {
|
67
|
+
width: :w,
|
68
|
+
height: :h,
|
69
|
+
rotation: :rot,
|
70
|
+
noise_reduction: :nr,
|
71
|
+
sharpness: :sharp,
|
72
|
+
exposure: :exp,
|
73
|
+
vibrance: :vib,
|
74
|
+
saturation: :sat,
|
75
|
+
brightness: :bri,
|
76
|
+
contrast: :con,
|
77
|
+
highlight: :high,
|
78
|
+
shadow: :shad,
|
79
|
+
gamma: :gam,
|
80
|
+
pixelate: :px,
|
81
|
+
halftone: :htn,
|
82
|
+
watermark: :mark,
|
83
|
+
text: :txt,
|
84
|
+
format: :fm,
|
85
|
+
quality: :q,
|
86
|
+
fill_color: :fillcolor
|
87
|
+
}.freeze
|
88
|
+
|
89
|
+
# Define query parameters on a Path (via method_missing).
|
90
|
+
# Normally, when overriding method_missing, it is a best practice
|
91
|
+
# to fall back to super, but this method works differently.
|
92
|
+
#
|
93
|
+
# method_missing intercepts messages sent to objects of this class
|
94
|
+
# and acts as a getter, setter, and deleter. If there are no args,
|
95
|
+
# e.g. `path.width`, then this method acts like a getter.
|
96
|
+
#
|
97
|
+
# Likewise, if the first argument is nil and the method name exists
|
98
|
+
# as a key in @options, e.g. `path.param_name = nil`, then this
|
99
|
+
# method acts like a deleter and the `param_name` is removed from
|
100
|
+
# the list of @options.
|
101
|
+
#
|
102
|
+
# Finally, in _all_ other cases, the `method` name is used as the
|
103
|
+
# `key` and the `*args` are used as the value.
|
64
104
|
def method_missing(method, *args, &block)
|
65
105
|
key = method.to_s.gsub('=', '')
|
66
|
-
|
106
|
+
|
107
|
+
if args.length == 0 # Get, or
|
67
108
|
return @options[key]
|
68
|
-
elsif args.first.nil? && @options.has_key?(key)
|
109
|
+
elsif args.first.nil? && @options.has_key?(key) # Delete, or
|
69
110
|
@options.delete(key) and return self
|
70
111
|
end
|
71
112
|
|
72
|
-
@options[key] = args.join(',')
|
113
|
+
@options[key] = args.join(',') # Set the option.
|
73
114
|
self
|
74
115
|
end
|
75
116
|
|
117
|
+
# Use ALIASES to define setters for a subset of imgix parameters.
|
118
|
+
# E.g. `path.width(100)` would result in `send(:w, [100])`.
|
76
119
|
ALIASES.each do |from, to|
|
77
120
|
define_method from do |*args|
|
78
|
-
warn "Warning: `Path.#{from}' has been deprecated and " \
|
79
|
-
"will be removed in the next major version (along " \
|
80
|
-
"with all parameter `ALIASES`).\n"
|
81
121
|
self.send(to, *args)
|
82
122
|
end
|
83
123
|
|
124
|
+
# E.g. `path.width = 100` would result in `send(":w=", [100])`.
|
84
125
|
define_method "#{from}=" do |*args|
|
85
|
-
warn "Warning: `Path.#{from}=' has been deprecated and " \
|
86
|
-
"will be removed in the next major version (along " \
|
87
|
-
"with all parameter `ALIASES`).\n"
|
88
126
|
self.send("#{to}=", *args)
|
89
127
|
return self
|
90
128
|
end
|
91
129
|
end
|
92
130
|
|
93
|
-
def to_srcset(options: {}, **params)
|
94
|
-
prev_options = @options.dup
|
95
|
-
@options.merge!(params)
|
96
|
-
|
97
|
-
width = @options[:w]
|
98
|
-
height = @options[:h]
|
99
|
-
aspect_ratio = @options[:ar]
|
100
|
-
|
101
|
-
srcset = if width || (height && aspect_ratio)
|
102
|
-
build_dpr_srcset(options: options, params: @options)
|
103
|
-
else
|
104
|
-
build_srcset_pairs(options: options, params: @options)
|
105
|
-
end
|
106
|
-
|
107
|
-
@options = prev_options
|
108
|
-
srcset
|
109
|
-
end
|
110
|
-
|
111
131
|
private
|
112
132
|
|
113
133
|
def signature
|
@@ -127,15 +147,15 @@ module Imgix
|
|
127
147
|
else
|
128
148
|
escaped_key << "=" << ERB::Util.url_encode(val.to_s)
|
129
149
|
end
|
130
|
-
end.join(
|
150
|
+
end.join("&")
|
131
151
|
end
|
132
152
|
|
133
153
|
def has_query?
|
134
|
-
query.
|
154
|
+
!query.empty?
|
135
155
|
end
|
136
156
|
|
137
157
|
def build_srcset_pairs(options:, params:)
|
138
|
-
srcset =
|
158
|
+
srcset = ""
|
139
159
|
|
140
160
|
widths = options[:widths] || []
|
141
161
|
width_tolerance = options[:width_tolerance] || DEFAULT_WIDTH_TOLERANCE
|
@@ -162,7 +182,7 @@ module Imgix
|
|
162
182
|
end
|
163
183
|
|
164
184
|
def build_dpr_srcset(options:, params:)
|
165
|
-
srcset =
|
185
|
+
srcset = ""
|
166
186
|
|
167
187
|
disable_variable_quality = options[:disable_variable_quality] || false
|
168
188
|
validate_variable_qualities!(disable_variable_quality)
|
@@ -173,9 +193,7 @@ module Imgix
|
|
173
193
|
target_ratios.each do |ratio|
|
174
194
|
params[:dpr] = ratio
|
175
195
|
|
176
|
-
unless disable_variable_quality
|
177
|
-
params[:q] = quality || DPR_QUALITY[ratio]
|
178
|
-
end
|
196
|
+
params[:q] = quality || DPR_QUALITY[ratio] unless disable_variable_quality
|
179
197
|
|
180
198
|
srcset += "#{to_url(params)} #{ratio}x,\n"
|
181
199
|
end
|
@@ -184,15 +202,13 @@ module Imgix
|
|
184
202
|
end
|
185
203
|
|
186
204
|
def validate_width_tolerance!(width_tolerance)
|
187
|
-
width_increment_error =
|
205
|
+
width_increment_error = "error: `width_tolerance` must be a positive `Numeric` value"
|
188
206
|
|
189
|
-
if !width_tolerance.is_a?(Numeric) || width_tolerance <= 0
|
190
|
-
raise ArgumentError, width_increment_error
|
191
|
-
end
|
207
|
+
raise ArgumentError, width_increment_error if !width_tolerance.is_a?(Numeric) || width_tolerance <= 0
|
192
208
|
end
|
193
209
|
|
194
210
|
def validate_widths!(widths)
|
195
|
-
widths_error =
|
211
|
+
widths_error = "error: `widths` must be an array of positive `Numeric` values"
|
196
212
|
raise ArgumentError, widths_error unless widths.is_a?(Array)
|
197
213
|
|
198
214
|
all_positive_integers = widths.all? { |i| i.is_a?(Integer) && i > 0 }
|
@@ -200,18 +216,14 @@ module Imgix
|
|
200
216
|
end
|
201
217
|
|
202
218
|
def validate_range!(min_srcset, max_srcset)
|
203
|
-
range_numeric_error =
|
204
|
-
unless min_srcset.is_a?(Numeric) && max_srcset.is_a?(Numeric)
|
205
|
-
raise ArgumentError, range_numeric_error
|
206
|
-
end
|
219
|
+
range_numeric_error = "error: `min_width` and `max_width` must be positive `Numeric` values"
|
220
|
+
raise ArgumentError, range_numeric_error unless min_srcset.is_a?(Numeric) && max_srcset.is_a?(Numeric)
|
207
221
|
|
208
|
-
unless min_srcset > 0 && max_srcset > 0
|
209
|
-
raise ArgumentError, range_numeric_error
|
210
|
-
end
|
222
|
+
raise ArgumentError, range_numeric_error unless min_srcset > 0 && max_srcset > 0
|
211
223
|
end
|
212
224
|
|
213
225
|
def validate_variable_qualities!(disable_quality)
|
214
|
-
disable_quality_error =
|
226
|
+
disable_quality_error = "error: `disable_quality` must be a Boolean value"
|
215
227
|
unless disable_quality.is_a?(TrueClass) || disable_quality.is_a?(FalseClass)
|
216
228
|
raise ArgumentError, disable_quality_error
|
217
229
|
end
|