imgix 3.2.1 → 4.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/imgix/path.rb CHANGED
@@ -1,44 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'base64'
4
- require 'cgi/util'
5
- require 'erb'
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
- ALIASES = {
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
- }
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? ? '&' : '?') + "s=#{signature}"
28
+ url += (has_query? ? "&" : "?") + "s=#{signature}"
53
29
  end
54
30
 
55
31
  @options = prev_options
@@ -61,47 +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
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
- if args.length == 0
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
121
  self.send(to, *args)
79
122
  end
80
123
 
124
+ # E.g. `path.width = 100` would result in `send(":w=", [100])`.
81
125
  define_method "#{from}=" do |*args|
82
126
  self.send("#{to}=", *args)
83
127
  return self
84
128
  end
85
129
  end
86
130
 
87
- def to_srcset(options: {}, **params)
88
- prev_options = @options.dup
89
- @options.merge!(params)
90
-
91
- width = @options['w'.to_sym]
92
- height = @options['h'.to_sym]
93
- aspect_ratio = @options['ar'.to_sym]
94
-
95
- if ((width) || (height && aspect_ratio))
96
- srcset = build_dpr_srcset(options: options, params:@options)
97
- else
98
- srcset = build_srcset_pairs(options: options, params: @options)
99
- end
100
-
101
- @options = prev_options
102
- srcset
103
- end
104
-
105
131
  private
106
132
 
107
133
  def signature
@@ -121,33 +147,34 @@ module Imgix
121
147
  else
122
148
  escaped_key << "=" << ERB::Util.url_encode(val.to_s)
123
149
  end
124
- end.join('&')
150
+ end.join("&")
125
151
  end
126
152
 
127
153
  def has_query?
128
- query.length > 0
154
+ !query.empty?
129
155
  end
130
156
 
131
157
  def build_srcset_pairs(options:, params:)
132
- srcset = ''
158
+ srcset = ""
133
159
 
134
- widths = options['widths'.to_sym] || []
135
- width_tolerance = options['width_tolerance'.to_sym] || DEFAULT_WIDTH_TOLERANCE
136
- min_srcset = options['min_width'.to_sym] || MIN_WIDTH
137
- max_srcset = options['max_width'.to_sym] || MAX_WIDTH
160
+ widths = options[:widths] || []
161
+ width_tolerance = options[:width_tolerance] || DEFAULT_WIDTH_TOLERANCE
162
+ min_width = options[:min_width] || MIN_WIDTH
163
+ max_width = options[:max_width] || MAX_WIDTH
138
164
 
139
165
  if !widths.empty?
140
166
  validate_widths!(widths)
141
167
  srcset_widths = widths
142
- elsif width_tolerance != DEFAULT_WIDTH_TOLERANCE or min_srcset != MIN_WIDTH or max_srcset != MAX_WIDTH
143
- validate_range!(min_srcset, max_srcset)
144
- srcset_widths = TARGET_WIDTHS.call(width_tolerance, min_srcset, max_srcset)
168
+ elsif width_tolerance != DEFAULT_WIDTH_TOLERANCE || min_width != MIN_WIDTH || max_width != MAX_WIDTH
169
+ validate_range!(min_width, max_width)
170
+ validate_width_tolerance!(width_tolerance)
171
+ srcset_widths = TARGET_WIDTHS.call(width_tolerance, min_width, max_width)
145
172
  else
146
173
  srcset_widths = @target_widths
147
174
  end
148
175
 
149
- for width in srcset_widths do
150
- params['w'.to_sym] = width
176
+ srcset_widths.each do |width|
177
+ params[:w] = width
151
178
  srcset += "#{to_url(params)} #{width}w,\n"
152
179
  end
153
180
 
@@ -155,20 +182,18 @@ module Imgix
155
182
  end
156
183
 
157
184
  def build_dpr_srcset(options:, params:)
158
- srcset = ''
185
+ srcset = ""
159
186
 
160
- disable_variable_quality = options['disable_variable_quality'.to_sym] || false
187
+ disable_variable_quality = options[:disable_variable_quality] || false
161
188
  validate_variable_qualities!(disable_variable_quality)
162
189
 
163
- target_ratios = [1,2,3,4,5]
164
- quality = params['q'.to_sym]
190
+ target_ratios = [1, 2, 3, 4, 5]
191
+ quality = params[:q]
165
192
 
166
- for ratio in target_ratios do
167
- params['dpr'.to_sym] = ratio
193
+ target_ratios.each do |ratio|
194
+ params[:dpr] = ratio
168
195
 
169
- unless disable_variable_quality
170
- params['q'.to_sym] = quality || DPR_QUALITY[ratio]
171
- end
196
+ params[:q] = quality || DPR_QUALITY[ratio] unless disable_variable_quality
172
197
 
173
198
  srcset += "#{to_url(params)} #{ratio}x,\n"
174
199
  end
@@ -176,30 +201,31 @@ module Imgix
176
201
  srcset[0..-3]
177
202
  end
178
203
 
204
+ def validate_width_tolerance!(width_tolerance)
205
+ width_increment_error = "error: `width_tolerance` must be a positive `Numeric` value"
206
+
207
+ raise ArgumentError, width_increment_error if !width_tolerance.is_a?(Numeric) || width_tolerance <= 0
208
+ end
209
+
179
210
  def validate_widths!(widths)
180
- unless widths.is_a? Array
181
- raise ArgumentError, "The widths argument must be passed a valid array of integers"
182
- else
183
- positive_integers = widths.all? {|i| i.is_a?(Integer) and i > 0}
184
- unless positive_integers
185
- raise ArgumentError, "A custom widths array must only contain positive integer values"
186
- end
187
- end
211
+ widths_error = "error: `widths` must be an array of positive `Numeric` values"
212
+ raise ArgumentError, widths_error unless widths.is_a?(Array)
213
+
214
+ all_positive_integers = widths.all? { |i| i.is_a?(Integer) && i > 0 }
215
+ raise ArgumentError, widths_error unless all_positive_integers
188
216
  end
189
217
 
190
- def validate_range!(min_srcset, max_srcset)
191
- if min_srcset.is_a? Numeric and max_srcset.is_a? Numeric
192
- unless min_srcset > 0 and max_srcset > 0
193
- raise ArgumentError, "The min and max arguments must be passed positive Numeric values"
194
- end
195
- else
196
- raise ArgumentError, "The min and max arguments must be passed positive Numeric values"
197
- end
218
+ def validate_range!(min_width, max_width)
219
+ range_numeric_error = "error: `min_width` and `max_width` must be positive `Numeric` values"
220
+ raise ArgumentError, range_numeric_error unless min_width.is_a?(Numeric) && max_width.is_a?(Numeric)
221
+
222
+ raise ArgumentError, range_numeric_error unless min_width > 0 && max_width > 0
198
223
  end
199
224
 
200
- def validate_variable_qualities!(disable_variable_quality)
201
- unless disable_variable_quality.is_a?(TrueClass) || disable_variable_quality.is_a?(FalseClass)
202
- raise ArgumentError, "The disable_variable_quality argument must be passed a Boolean value"
225
+ def validate_variable_qualities!(disable_quality)
226
+ disable_quality_error = "error: `disable_quality` must be a Boolean value"
227
+ unless disable_quality.is_a?(TrueClass) || disable_quality.is_a?(FalseClass)
228
+ raise ArgumentError, disable_quality_error
203
229
  end
204
230
  end
205
231
  end
data/lib/imgix/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Imgix
4
- VERSION = '3.2.1'
4
+ VERSION = "4.0.1"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: imgix
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.1
4
+ version: 4.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kelly Sutton
@@ -10,39 +10,11 @@ authors:
10
10
  - Antony Denyer
11
11
  - Paul Straw
12
12
  - Sherwin Heydarbeygi
13
- autorequire:
13
+ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
- date: 2019-11-20 00:00:00.000000000 Z
17
- dependencies:
18
- - !ruby/object:Gem::Dependency
19
- name: addressable
20
- requirement: !ruby/object:Gem::Requirement
21
- requirements:
22
- - - ">="
23
- - !ruby/object:Gem::Version
24
- version: '0'
25
- type: :runtime
26
- prerelease: false
27
- version_requirements: !ruby/object:Gem::Requirement
28
- requirements:
29
- - - ">="
30
- - !ruby/object:Gem::Version
31
- version: '0'
32
- - !ruby/object:Gem::Dependency
33
- name: webmock
34
- requirement: !ruby/object:Gem::Requirement
35
- requirements:
36
- - - ">="
37
- - !ruby/object:Gem::Version
38
- version: '0'
39
- type: :development
40
- prerelease: false
41
- version_requirements: !ruby/object:Gem::Requirement
42
- requirements:
43
- - - ">="
44
- - !ruby/object:Gem::Version
45
- version: '0'
16
+ date: 2021-03-25 00:00:00.000000000 Z
17
+ dependencies: []
46
18
  description: Easily create and sign imgix URLs.
47
19
  email:
48
20
  - kelly@imgix.com
@@ -56,6 +28,7 @@ extensions: []
56
28
  extra_rdoc_files: []
57
29
  files:
58
30
  - ".gitignore"
31
+ - ".rubocop.yml"
59
32
  - ".travis.yml"
60
33
  - CHANGELOG.md
61
34
  - Contributing.markdown
@@ -66,24 +39,17 @@ files:
66
39
  - imgix.gemspec
67
40
  - lib/imgix.rb
68
41
  - lib/imgix/client.rb
69
- - lib/imgix/param_helpers.rb
70
42
  - lib/imgix/path.rb
71
43
  - lib/imgix/version.rb
72
- - test/test_helper.rb
73
- - test/units/domains_test.rb
74
- - test/units/path_test.rb
75
- - test/units/purge_test.rb
76
- - test/units/srcset_test.rb
77
- - test/units/url_test.rb
78
44
  homepage: https://github.com/imgix/imgix-rb
79
45
  licenses:
80
46
  - MIT
81
47
  metadata:
82
48
  bug_tracker_uri: https://github.com/imgix/imgix-rb/issues
83
- changelog_uri: https://github.com/imgix/imgix-rb/blob/master/CHANGELOG.md
84
- documentation_uri: https://www.rubydoc.info/gems/imgix/3.2.1
85
- source_code_uri: https://github.com/imgix/imgix-rb/tree/3.2.1
86
- post_install_message:
49
+ changelog_uri: https://github.com/imgix/imgix-rb/blob/main/CHANGELOG.md
50
+ documentation_uri: https://www.rubydoc.info/gems/imgix/4.0.1
51
+ source_code_uri: https://github.com/imgix/imgix-rb/tree/4.0.1
52
+ post_install_message:
87
53
  rdoc_options: []
88
54
  require_paths:
89
55
  - lib
@@ -98,15 +64,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
98
64
  - !ruby/object:Gem::Version
99
65
  version: '0'
100
66
  requirements: []
101
- rubyforge_project:
102
- rubygems_version: 2.6.13
103
- signing_key:
67
+ rubygems_version: 3.2.3
68
+ signing_key:
104
69
  specification_version: 4
105
70
  summary: Official Ruby Gem for easily creating and signing imgix URLs.
106
- test_files:
107
- - test/test_helper.rb
108
- - test/units/domains_test.rb
109
- - test/units/path_test.rb
110
- - test/units/purge_test.rb
111
- - test/units/srcset_test.rb
112
- - test/units/url_test.rb
71
+ test_files: []
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Imgix
4
- module ParamHelpers
5
- def rect(position)
6
- @options[:rect] = position and return self if position.is_a?(String)
7
-
8
- @options[:rect] = [
9
- position[:x] || position[:left],
10
- position[:y] || position[:top],
11
- position[:width] || (position[:right] - position[:left]),
12
- position[:height] || (position[:bottom] - position[:top])
13
- ].join(',')
14
-
15
- return self
16
- end
17
- end
18
- end
data/test/test_helper.rb DELETED
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rubygems'
4
- require 'bundler'
5
- Bundler.require :test
6
-
7
- require 'minitest/autorun'
8
- require 'imgix'
9
- require 'webmock/minitest'
10
- include WebMock::API
11
-
12
- class Imgix::Test < MiniTest::Test
13
- end