Class: ScaleDown::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/scale_down/dispatcher.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Dispatcher

Returns a new instance of Dispatcher.



24
25
26
# File 'lib/scale_down/dispatcher.rb', line 24

def initialize(params)
  @params = params
end

Class Method Details

.process(params) ⇒ Object



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

def process(params)
  dispatcher = new(params)

  ScaleDown.logger.info "Dipatcher#process #{dispatcher.root_path}"
  $0= "scale_down Dipatcher#process #{dispatcher.root_path}"

  return ["Missing file #{dispatcher.root_path}", 404] unless dispatcher.root_file_exists?
  return [dispatcher.redirect_path, dispatcher.redirect_code] if dispatcher.scaled_file_exists?

  return ["Invalid HMAC signature", 403] unless dispatcher.valid_hmac?
  return ["File failed to scale. The file may be corrupt.", 500] unless dispatcher.scale

  [dispatcher.redirect_path, dispatcher.redirect_code]
end

Instance Method Details

#crop?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/scale_down/dispatcher.rb', line 53

def crop?
  @crop ||= ! @params[:target].match(/-crop\Z/).nil?
end

#filenameObject



94
95
96
# File 'lib/scale_down/dispatcher.rb', line 94

def filename
  @params[:filename].split(".")[0...-1].join(".")
end

#image_optionsObject



28
29
30
31
32
33
34
35
36
# File 'lib/scale_down/dispatcher.rb', line 28

def image_options
  width, height =  (target_is_label? ? ScaleDown.labels[target] : target).split("x")

  {
    :height => height.to_i,
    :width  => width.to_i,
    :crop   => crop?
  }
end

#redirect_codeObject



65
66
67
# File 'lib/scale_down/dispatcher.rb', line 65

def redirect_code
  @params[:filename].match(/(png|jpg)$/) ? 302 : 301
end

#redirect_pathObject



69
70
71
# File 'lib/scale_down/dispatcher.rb', line 69

def redirect_path
  ["/"+@params[:path], @params[:target], scaled_filename].join("/")
end

#root_file_exists?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/scale_down/dispatcher.rb', line 73

def root_file_exists?
  File.exists? root_path
end

#root_pathObject



81
82
83
84
# File 'lib/scale_down/dispatcher.rb', line 81

def root_path
  root = @params[:path].gsub(/\/scaled$/,"")
  File.join(ScaleDown.public_folder, root, @params[:filename])
end

#scaleObject



38
39
40
41
42
43
# File 'lib/scale_down/dispatcher.rb', line 38

def scale
  ScaleDown::Image.scale \
    :file    => root_path,
    :out     => scaled_file_path,
    :options => image_options
end

#scaled_extensionObject



98
99
100
101
# File 'lib/scale_down/dispatcher.rb', line 98

def scaled_extension
  ext = @params[:filename].split(".").last
  ext == "png" ? ext : "jpg"
end

#scaled_file_exists?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/scale_down/dispatcher.rb', line 77

def scaled_file_exists?
  File.exists? scaled_file_path
end

#scaled_file_pathObject



86
87
88
# File 'lib/scale_down/dispatcher.rb', line 86

def scaled_file_path
  File.join(ScaleDown.public_folder, redirect_path)
end

#scaled_filenameObject



90
91
92
# File 'lib/scale_down/dispatcher.rb', line 90

def scaled_filename
  "#{filename}.#{scaled_extension}"
end

#targetObject



45
46
47
# File 'lib/scale_down/dispatcher.rb', line 45

def target
  @dimensions ||= @params[:target].split(/-crop\Z/).first
end

#target_is_label?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/scale_down/dispatcher.rb', line 49

def target_is_label?
  @target_is_label ||= ScaleDown.labels.keys.include?(target)
end

#valid_hmac?Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
# File 'lib/scale_down/dispatcher.rb', line 57

def valid_hmac?
  if target_is_label?
    true
  else
    ScaleDown.valid_hmac?(@params)
  end
end