module PhotoCook::Resize::Assemble

Public Class Methods

assemble_public_path(root) click to toggle source

Path to public directory

Arguments:

root => /application

Returns /application/public

NOTE: This method performs no validation

# File lib/photo-cook/resize/assemble.rb, line 93
def assemble_public_path(root)
  File.join(root, PhotoCook.public_dir)
end
assemble_resize_uri(source_uri, width, height, mode) click to toggle source

Returns URI which points to PhotoCook::Resize::Middleware

Arguments:

source_uri  => /uploads/photos/1/car.png
width       => :auto
height      => 640
mode        => fit

Returns /uploads/photos/1/resized/width=auto&height=640&mode=fit/car.png

NOTE: This method performs no validation NOTE: This method is very hot

# File lib/photo-cook/resize/assemble.rb, line 20
def assemble_resize_uri(source_uri, width, height, mode)
  source_uri.split('/').insert(-2, PhotoCook::Resize.cache_dir, Command.assemble(width, height, mode)).join('/')
end
assemble_source_path_from_normal_uri(root, normal_uri) click to toggle source
# File lib/photo-cook/resize/assemble.rb, line 65
def assemble_source_path_from_normal_uri(root, normal_uri)
  File.join(assemble_public_path(root), normal_uri)
end
assemble_source_path_from_resize_uri(root, resize_uri) click to toggle source

Path where source photo is stored

Arguments:

root       => /application
resize_uri => /uploads/photos/1/resized/width=auto&height=640&mode=fit/car.png

Returns /application/public/uploads/photos/1/car.png

NOTE: This method performs no validation

# File lib/photo-cook/resize/assemble.rb, line 61
def assemble_source_path_from_resize_uri(root, resize_uri)
  assemble_source_path_from_normal_uri(root, disassemble_resize_uri(resize_uri))
end
assemble_store_path(root, source_path, assembled_command) click to toggle source

Path where resized photo is stored

Arguments:

root              => /application
source_path       => /application/public/uploads/photos/1/car.png
assembled_command => width=auto&height=640&mode=fit

Returns /application/public/resized/uploads/photos/1/width=auto&height=640&mode=fit/car.png

NOTE: This method performs no validation

# File lib/photo-cook/resize/assemble.rb, line 79
def assemble_store_path(root, source_path, assembled_command)
  public         = assemble_public_path(root)
  photo_location = dirname_or_blank(source_path.split(public).last)
  File.join(public, PhotoCook::Resize.cache_dir, photo_location, assembled_command, File.basename(source_path))
end
disassemble_resize_uri(resize_uri) click to toggle source

Strips resize command from URI. Inverse of assemble_resize_uri

Arguments:

resize_uri => /uploads/photos/1/resized/width=auto&height=640&mode=fit/car.png

Returns /uploads/photos/1/car.png

NOTE: This method performs no validation

# File lib/photo-cook/resize/assemble.rb, line 32
def disassemble_resize_uri(resize_uri)
  # Take URI:
  # /uploads/photos/1/resized/width=auto&height=640&mode=fit/car.png
  #
  # Split by separator:
  # ["", "uploads", "photos", "1", "resized", "width=auto&height=640&mode=fit", "car.png"]
  #
  sections = resize_uri.split('/')

  # Delete PhotoCook directory:
  # ["", "uploads", "photos", "1", "width=auto&height=640&mode=fit", "car.png"]
  sections.delete_at(-3)

  # Delete command string:
  # ["", "uploads", "photos", "1", "car.png"]
  sections.delete_at(-2)

  sections.join('/')
end
resize_uri?(uri) click to toggle source
# File lib/photo-cook/resize/assemble.rb, line 97
def resize_uri?(uri)
  sections = uri.split('/')

  # Check if PhotoCook cache directory exists:
  #   sections[-3] => resized
  sections[-3] == PhotoCook::Resize.cache_dir &&

  # Check if valid resize command exists:
  #   sections[-2] => width=auto&height=640&mode=fit
  matches_regex?(sections[-2], Command.regex)
end

Private Class Methods

dirname_or_blank(path) click to toggle source
# File lib/photo-cook/resize/assemble.rb, line 110
def dirname_or_blank(path)
  File.dirname(path).sub(/\A\.\z/, '')
end
matches_regex?(string, regex) click to toggle source
# File lib/photo-cook/resize/assemble.rb, line 116
def matches_regex?(string, regex)
  regex.match?(string)
end