Class: Wraith::GalleryGenerator

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/wraith/gallery.rb

Defined Under Namespace

Classes: ErbBinding

Constant Summary collapse

MATCH_FILENAME =
/(\S+)_(\S+)\.\S+/

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger, logger

Constructor Details

#initialize(config, multi) ⇒ GalleryGenerator

Returns a new instance of GalleryGenerator.



13
14
15
16
17
18
# 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

Instance Attribute Details

#wraithObject (readonly)

Returns the value of attribute wraith.



9
10
11
# File 'lib/wraith/gallery.rb', line 9

def wraith
  @wraith
end

Instance Method Details

#check_failed_shotsObject



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# 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) ⇒ Object



108
109
110
# 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) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# 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) ⇒ Object



102
103
104
105
106
# 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) ⇒ Object



62
63
64
65
66
67
68
# 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


140
141
142
143
144
145
146
147
148
# 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) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
# 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) ⇒ Object



74
75
76
77
78
79
# 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) ⇒ Object



70
71
72
# File 'lib/wraith/gallery.rb', line 70

def get_path(category)
  wraith.paths[category]["path"] || wraith.paths[category]
end

#match(categories, dirname) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# 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) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# 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) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# 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


194
195
196
197
# 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


164
165
166
167
168
169
# 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) ⇒ Object



128
129
130
131
132
133
134
# 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) ⇒ Object



136
137
138
# File 'lib/wraith/gallery.rb', line 136

def sort_alphabetically(dirs)
  dirs.sort_by { |category, _sizes| category }
end

#sort_by_diffs(dirs) ⇒ Object



121
122
123
124
125
126
# 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) ⇒ Object



112
113
114
115
116
117
118
119
# 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) ⇒ Object



92
93
94
95
96
97
98
99
100
# 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