Class: HeadlessBrowserTool::Tools::AttachFileTool

Inherits:
BaseTool
  • Object
show all
Defined in:
lib/headless_browser_tool/tools/attach_file_tool.rb

Instance Method Summary collapse

Instance Method Details

#execute(file_field_selector:, file_path:) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/headless_browser_tool/tools/attach_file_tool.rb', line 16

def execute(file_field_selector:, file_path:)
  file_field = browser.find(file_field_selector)

  # Get file info
  file_name = File.basename(file_path)
  file_size = File.size(file_path) if File.exist?(file_path)

  browser.attach_file(file_field_selector, file_path)

  {
    field_selector: file_field_selector,
    file_path: file_path,
    file_name: file_name,
    file_size: file_size,
    field: {
      id: file_field[:id],
      name: file_field[:name],
      accept: file_field[:accept]
    }.compact,
    status: "attached"
  }
end