Class: Imgix::Path
- Inherits:
-
Object
- Object
- Imgix::Path
- Defined in:
- lib/imgix/path.rb
Constant Summary collapse
- ALIASES =
{ width: :w, height: :h, rotation: :rot, noise_reduction: :nr, sharpness: :sharp, exposure: :exp, vibrance: :vib, saturation: :sat, brightness: :bri, contrast: :con, highlight: :high, shadow: :shad, gamma: :gam, pixelate: :px, halftone: :htn, watermark: :mark, text: :txt, format: :fm, quality: :q, fill_color: :fillcolor }.freeze
Instance Method Summary collapse
- #defaults ⇒ Object
-
#initialize(prefix, secure_url_token, path = "/") ⇒ Path
constructor
A new instance of Path.
- #ixlib(lib_version) ⇒ Object
- #ixlib=(lib_version) ⇒ Object
-
#method_missing(method, *args, &block) ⇒ Object
Define query parameters on a Path (via method_missing).
- #to_srcset(options: {}, **params) ⇒ Object
- #to_url(opts = {}) ⇒ Object
Constructor Details
#initialize(prefix, secure_url_token, path = "/") ⇒ Path
Returns a new instance of Path.
10 11 12 13 14 15 |
# File 'lib/imgix/path.rb', line 10 def initialize(prefix, secure_url_token, path = "/") @prefix = prefix @secure_url_token = secure_url_token @path = path @options = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Define query parameters on a Path (via method_missing). Normally, when overriding method_missing, it is a best practice to fall back to super, but this method works differently.
method_missing intercepts messages sent to objects of this class and acts as a getter, setter, and deleter. If there are no args, e.g. ‘path.width`, then this method acts like a getter.
Likewise, if the first argument is nil and the method name exists as a key in @options, e.g. ‘path.param_name = nil`, then this method acts like a deleter and the `param_name` is removed from the list of @options.
Finally, in all other cases, the ‘method` name is used as the `key` and the `*args` are used as the value.
102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/imgix/path.rb', line 102 def method_missing(method, *args, &block) key = method.to_s.gsub('=', '') if args.length == 0 # Get, or return @options[key] elsif args.first.nil? && @options.has_key?(key) # Delete, or @options.delete(key) and return self end @options[key] = args.join(',') # Set the option. self end |
Instance Method Details
#defaults ⇒ Object
33 34 35 36 |
# File 'lib/imgix/path.rb', line 33 def defaults @options = {} self end |
#ixlib(lib_version) ⇒ Object
38 39 40 |
# File 'lib/imgix/path.rb', line 38 def ixlib(lib_version) @options[:ixlib] = lib_version end |
#ixlib=(lib_version) ⇒ Object
42 43 44 |
# File 'lib/imgix/path.rb', line 42 def ixlib=(lib_version) @options[:ixlib] = lib_version end |
#to_srcset(options: {}, **params) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/imgix/path.rb', line 46 def to_srcset(options: {}, **params) = @options.dup @options.merge!(params) width = @options[:w] height = @options[:h] aspect_ratio = @options[:ar] srcset = if width || height build_dpr_srcset(options: , params: @options) else build_srcset_pairs(options: , params: @options) end @options = srcset end |
#to_url(opts = {}) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/imgix/path.rb', line 17 def to_url(opts = {}) sanitized_path = sanitize_path(@path) = @options.dup @options.merge!(opts) current_path_and_params = path_and_params(sanitized_path) url = @prefix + current_path_and_params if @secure_url_token url += (has_query? ? "&" : "?") + "s=#{signature(current_path_and_params)}" end @options = url end |