Class: ScaleDown::Dispatcher
- Inherits:
-
Object
- Object
- ScaleDown::Dispatcher
- Defined in:
- lib/scale_down/dispatcher.rb
Class Method Summary collapse
Instance Method Summary collapse
- #crop? ⇒ Boolean
- #filename ⇒ Object
- #image_options ⇒ Object
-
#initialize(params) ⇒ Dispatcher
constructor
A new instance of Dispatcher.
- #redirect_code ⇒ Object
- #redirect_path ⇒ Object
- #root_file_exists? ⇒ Boolean
- #root_path ⇒ Object
- #scale ⇒ Object
- #scaled_extension ⇒ Object
- #scaled_file_exists? ⇒ Boolean
- #scaled_file_path ⇒ Object
- #scaled_filename ⇒ Object
- #target ⇒ Object
- #target_is_label? ⇒ Boolean
- #valid_hmac? ⇒ Boolean
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
53 54 55 |
# File 'lib/scale_down/dispatcher.rb', line 53 def crop? @crop ||= ! @params[:target].match(/-crop\Z/).nil? end |
#filename ⇒ Object
94 95 96 |
# File 'lib/scale_down/dispatcher.rb', line 94 def filename @params[:filename].split(".")[0...-1].join(".") end |
#image_options ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/scale_down/dispatcher.rb', line 28 def width, height = (target_is_label? ? ScaleDown.labels[target] : target).split("x") { :height => height.to_i, :width => width.to_i, :crop => crop? } end |
#redirect_code ⇒ Object
65 66 67 |
# File 'lib/scale_down/dispatcher.rb', line 65 def redirect_code @params[:filename].match(/(png|jpg)$/) ? 302 : 301 end |
#redirect_path ⇒ Object
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
73 74 75 |
# File 'lib/scale_down/dispatcher.rb', line 73 def root_file_exists? File.exists? root_path end |
#root_path ⇒ Object
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 |
#scale ⇒ Object
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 => end |
#scaled_extension ⇒ Object
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
77 78 79 |
# File 'lib/scale_down/dispatcher.rb', line 77 def scaled_file_exists? File.exists? scaled_file_path end |
#scaled_file_path ⇒ Object
86 87 88 |
# File 'lib/scale_down/dispatcher.rb', line 86 def scaled_file_path File.join(ScaleDown.public_folder, redirect_path) end |
#scaled_filename ⇒ Object
90 91 92 |
# File 'lib/scale_down/dispatcher.rb', line 90 def scaled_filename "#{filename}.#{scaled_extension}" end |
#target ⇒ Object
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
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
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 |