Class: BrowserCrawler::ScreenshotOperator

Inherits:
Object
  • Object
show all
Defined in:
lib/browser_crawler/screenshot_operator.rb

Overview

Control operations on screenshots

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(save_screenshots: false, save_screenshots_to: nil, format: 'png', filename: nil) ⇒ ScreenshotOperator

Returns a new instance of ScreenshotOperator.



6
7
8
9
10
11
12
13
14
# File 'lib/browser_crawler/screenshot_operator.rb', line 6

def initialize(save_screenshots: false,
               save_screenshots_to: nil,
               format: 'png',
               filename: nil)
  @screenshots_folder = save_screenshots_to
  @format = format
  @save_screenshots = save_screenshots
  @filename_base = filename || 'screenshot'
end

Instance Attribute Details

#filename_baseObject (readonly)

Returns the value of attribute filename_base.



4
5
6
# File 'lib/browser_crawler/screenshot_operator.rb', line 4

def filename_base
  @filename_base
end

#formatObject (readonly)

Returns the value of attribute format.



4
5
6
# File 'lib/browser_crawler/screenshot_operator.rb', line 4

def format
  @format
end

#save_screenshotsObject (readonly)

Returns the value of attribute save_screenshots.



4
5
6
# File 'lib/browser_crawler/screenshot_operator.rb', line 4

def save_screenshots
  @save_screenshots
end

#screenshots_folderObject (readonly)

Returns the value of attribute screenshots_folder.



4
5
6
# File 'lib/browser_crawler/screenshot_operator.rb', line 4

def screenshots_folder
  @screenshots_folder
end

Instance Method Details

#file_path(url: nil) ⇒ Object



20
21
22
# File 'lib/browser_crawler/screenshot_operator.rb', line 20

def file_path(url: nil)
  "#{save_path}/#{filename(url: url)}"
end

#filename(url: nil) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/browser_crawler/screenshot_operator.rb', line 24

def filename(url: nil)
  if !filename_base_default? || url.nil?
    "#{filename_prefix}_#{filename_base}.#{format}"
  else
    path = UrlTools.uri(url: url)&.path&.gsub('/', '%')&.gsub('.', '')
    "#{filename_prefix}_#{path}.#{format}"
  end
end

#save_screenshots?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/browser_crawler/screenshot_operator.rb', line 16

def save_screenshots?
  [screenshots_folder, save_screenshots].any?
end