class Wraith::GalleryGenerator
Constants
- MATCH_FILENAME
Attributes
wraith[R]
Public Class Methods
new(config, multi)
click to toggle source
# File lib/wraith/gallery.rb, line 13 def initialize(config, multi) @wraith = Wraith::Wraith.new(config) @location = wraith.directory @multi = multi @folder_manager = Wraith::FolderManager.new(config) end
Public Instance Methods
check_failed_shots()
click to toggle source
# File lib/wraith/gallery.rb, line 171 def check_failed_shots if @multi return false elsif @failed_shots == false logger.warn "Failures detected:" @dirs.each do |dir, sizes| sizes.to_a.sort.each do |size, files| file = dir.gsub("__", "/") if !files.include?(:diff) logger.warn "\t Unable to create a diff image for #{file}" elsif files[:data] > wraith.threshold logger.warn "\t #{file} failed at a resolution of #{size} (#{files[:data]}% diff)" end end end return true else false end end
data_check(size_dict, dirname, filepath)
click to toggle source
# File lib/wraith/gallery.rb, line 108 def data_check(size_dict, dirname, filepath) size_dict[:data] = File.read("#{dirname}/#{filepath}").to_f end
data_group(group, size_dict, dirname, filepath)
click to toggle source
# File lib/wraith/gallery.rb, line 81 def data_group(group, size_dict, dirname, filepath) case group when "diff" diff_check(size_dict, filepath) when "data" data_check(size_dict, dirname, filepath) else variant_check(size_dict, group) end end
diff_check(size_dict, filepath)
click to toggle source
# File lib/wraith/gallery.rb, line 102 def diff_check(size_dict, filepath) size_dict[:diff] = { :filename => filepath, :thumb => @thumbnail } end
figure_out_url(group, category)
click to toggle source
# File lib/wraith/gallery.rb, line 62 def figure_out_url(group, category) root = wraith.domains["#{group}"] return "" if root.nil? path = get_path(category) url = root + path url end
generate_gallery(with_path = "")
click to toggle source
# File lib/wraith/gallery.rb, line 140 def generate_gallery(with_path = "") dest = "#{@location}/gallery.html" directories = parse_directories(@location) template = File.expand_path("gallery_template/#{wraith.gallery_template}.erb", File.dirname(__FILE__)) generate_html(@location, directories, template, dest, with_path) report_gallery_status dest end
generate_html(location, directories, template, destination, path)
click to toggle source
# File lib/wraith/gallery.rb, line 150 def generate_html(location, directories, template, destination, path) template = File.read(template) locals = { :location => location, :directories => directories, :path => path, :threshold => wraith.threshold } html = ERB.new(template).result(ErbBinding.new(locals).get_binding) File.open(destination, "w") do |outf| outf.write(html) end end
get_group_from_match(match)
click to toggle source
# File lib/wraith/gallery.rb, line 74 def get_group_from_match(match) group = match[2] dash = match[2].rindex("-") group = match[2][dash + 1..-1] unless dash.nil? group end
get_path(category)
click to toggle source
# File lib/wraith/gallery.rb, line 70 def get_path(category) wraith.paths[category]["path"] || wraith.paths[category] end
match(categories, dirname)
click to toggle source
# File lib/wraith/gallery.rb, line 34 def match(categories, dirname) categories.each do |category| @dirs[category] = {} Dir.foreach("#{dirname}/#{category}") do |filename| match = MATCH_FILENAME.match(filename) matcher(match, filename, dirname, category) unless match.nil? end end @folder_manager.tidy_shots_folder(@dirs) @failed_shots = @folder_manager.threshold_rate(@dirs) sorting_dirs(@dirs) end
matcher(match, filename, dirname, category)
click to toggle source
# File lib/wraith/gallery.rb, line 48 def matcher(match, filename, dirname, category) @size = match[1].to_i @group = get_group_from_match match @filepath = category + "/" + filename @thumbnail = "thumbnails/#{category}/#{filename}" @url = figure_out_url @group, category @dirs[category][@size] = { :variants => [] } if @dirs[category][@size].nil? size_dict = @dirs[category][@size] data_group(@group, size_dict, dirname, @filepath) end
parse_directories(dirname)
click to toggle source
# File lib/wraith/gallery.rb, line 20 def parse_directories(dirname) @dirs = {} categories = Dir.foreach(dirname).select do |category| if [".", "..", "thumbnails"].include? category false elsif File.directory? "#{dirname}/#{category}" true else false end end match(categories, dirname) end
prompt_user_to_open_gallery(dest)
click to toggle source
# File lib/wraith/gallery.rb, line 194 def prompt_user_to_open_gallery(dest) logger.info "\nView the gallery in your browser:" logger.info "\t file://" + Dir.pwd + "/" + dest end
report_gallery_status(dest)
click to toggle source
# File lib/wraith/gallery.rb, line 164 def report_gallery_status(dest) logger.info "Gallery generated" failed = check_failed_shots prompt_user_to_open_gallery dest exit 1 if failed end
select_size_with_biggest_diff(sizes)
click to toggle source
# File lib/wraith/gallery.rb, line 128 def select_size_with_biggest_diff(sizes) begin sizes.max_by { |_size, dict| dict[:data] } rescue fail MissingImageError end end
sort_alphabetically(dirs)
click to toggle source
# File lib/wraith/gallery.rb, line 136 def sort_alphabetically(dirs) dirs.sort_by { |category, _sizes| category } end
sort_by_diffs(dirs)
click to toggle source
# File lib/wraith/gallery.rb, line 121 def sort_by_diffs(dirs) dirs.sort_by do |_category, sizes| size = select_size_with_biggest_diff sizes -1 * size[1][:data] end end
sorting_dirs(dirs)
click to toggle source
# File lib/wraith/gallery.rb, line 112 def sorting_dirs(dirs) if %w(diffs_only diffs_first).include?(wraith.mode) @sorted = sort_by_diffs dirs else @sorted = sort_alphabetically dirs end Hash[@sorted] end
variant_check(size_dict, group)
click to toggle source
# File lib/wraith/gallery.rb, line 92 def variant_check(size_dict, group) size_dict[:variants] << { :name => group, :filename => @filepath, :thumb => @thumbnail, :url => @url } size_dict[:variants].sort! { |a, b| a[:name] <=> b[:name] } end