Class: Imgix::Path

Inherits:
Object
  • Object
show all
Includes:
ParamHelpers
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
}

Instance Method Summary collapse

Methods included from ParamHelpers

#rect

Constructor Details

#initialize(prefix, secure_url_token, path = '/') ⇒ Path

Returns a new instance of Path.



34
35
36
37
38
39
40
41
42
43
# File 'lib/imgix/path.rb', line 34

def initialize(prefix, secure_url_token, path = '/')
  @prefix = prefix
  @secure_url_token = secure_url_token
  @path = path
  @options = {}

  @path = CGI.escape(@path) if /^https?/ =~ @path
  @path = "/#{@path}" if @path[0] != '/'
  @target_widths = TARGET_WIDTHS.call(DEFAULT_WIDTH_TOLERANCE, MIN_WIDTH, MAX_WIDTH)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/imgix/path.rb', line 64

def method_missing(method, *args, &block)
  key = method.to_s.gsub('=', '')
  if args.length == 0
    return @options[key]
  elsif args.first.nil? && @options.has_key?(key)
    @options.delete(key) and return self
  end

  @options[key] = args.join(',')
  self
end

Instance Method Details

#defaultsObject



59
60
61
62
# File 'lib/imgix/path.rb', line 59

def defaults
  @options = {}
  self
end

#to_srcset(options: {}, **params) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/imgix/path.rb', line 87

def to_srcset(options: {}, **params)
  prev_options = @options.dup
  @options.merge!(params)

  width = @options['w'.to_sym]
  height = @options['h'.to_sym]
  aspect_ratio = @options['ar'.to_sym]

  if ((width) || (height && aspect_ratio))
    srcset = build_dpr_srcset(options: options, params:@options)
  else
    srcset = build_srcset_pairs(options: options, params: @options)
  end

  @options = prev_options
  srcset
end

#to_url(opts = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/imgix/path.rb', line 45

def to_url(opts = {})
  prev_options = @options.dup
  @options.merge!(opts)

  url = @prefix + path_and_params

  if @secure_url_token
    url += (has_query? ? '&' : '?') + "s=#{signature}"
  end

  @options = prev_options
  url
end