Class: Imageproxy::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/imageproxy/options.rb

Instance Method Summary collapse

Constructor Details

#initialize(path, query_params) ⇒ Options

Returns a new instance of Options.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/imageproxy/options.rb', line 7

def initialize(path, query_params)
  params_from_path = path.split('/').reject { |s| s.nil? || s.empty? }
  command = params_from_path.shift

  @hash = Hash[*params_from_path]
  @hash['command'] = command
  @hash.merge! query_params
  merge_obfuscated
  @hash["source"] = @hash.delete("src") if @hash.has_key?("src")

  unescape_source
  unescape_overlay
  unescape_signature
  check_parameters
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol) ⇒ Object



42
43
44
# File 'lib/imageproxy/options.rb', line 42

def method_missing(symbol)
  @hash[symbol.to_s] || @hash[symbol]
end

Instance Method Details

#check_param(param, rega) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/imageproxy/options.rb', line 34

def check_param(param, rega)
  if @hash.has_key? param
    if (!rega.match(@hash[param]))
      @hash.delete(param)
    end
  end
end

#check_parametersObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/imageproxy/options.rb', line 23

def check_parameters
  check_param('resize', /^[0-9]{1,5}(x[0-9]{1,5})?$/)
  check_param('thumbnail', /^[0-9]{1,5}(x[0-9]{1,5})?$/)
  check_param('rotate', /^(-)?[0-9]{1,3}(\.[0-9]+)?$/)
  check_param('format', /^[0-9a-zA-Z]{2,6}$/)
  check_param('progressive', /^true|false$/i)
  check_param('background', /^#[0-9a-f]{3}([0-9a-f]{3})?|rgba\([0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0-1](.[0-9]+)?\)$/)
  check_param('shape', /^preserve|pad|cut$/i)
  @hash['quality'] = [[@hash['quality'].to_i, 100].min, 0].max.to_s if @hash.has_key?('quality')
end

#to_sObject



46
47
48
49
50
51
52
53
54
# File 'lib/imageproxy/options.rb', line 46

def to_s
  @hash.map do |key, value|
    if key && value
      "#{CGI::escape(key)}=#{CGI::escape(value)}"
    else
      nil
    end
  end.compact.join(', ')
end