Class: Monet::Capture

Inherits:
Object
  • Object
show all
Includes:
Capybara::DSL
Defined in:
lib/monet/capture.rb

Constant Summary collapse

MAX_HEIGHT =
10000

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Capture

Returns a new instance of Capture.



16
17
18
19
20
21
# File 'lib/monet/capture.rb', line 16

def initialize(config)
  @config = Monet::Config.build_config(config)
  @router = Monet::PathRouter.new(@config)

  Capybara.default_driver = @config.driver
end

Instance Method Details

#capture(path) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/monet/capture.rb', line 29

def capture(path)
  visit @router.build_url(path)

  @config.dimensions.each do |width|
    file_path = @router.route_url_path(path, width)

    page.driver.resize(width, MAX_HEIGHT)
    page.driver.render(file_path, full: true)

    thumbnail(file_path) if @config.thumbnail?
  end
end

#capture_allObject



23
24
25
26
27
# File 'lib/monet/capture.rb', line 23

def capture_all
  @config.map.paths.each do |path|
    capture(path)
  end
end

#thumbnail(path) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/monet/capture.rb', line 42

def thumbnail(path)
  img = ChunkyPNG::Image.from_file(path)
  short_edge = [img.width, img.height].min
  save_path = @router.to_thumbnail_path(path)
  save_dir = File.dirname save_path

  puts save_path

  cropped = img.crop(0, 0, short_edge, short_edge)
  resized = cropped.resize(200, 200)

  FileUtils.mkdir_p save_dir unless Dir.exists? save_dir

  resized.save save_path
end