class SimpleHotFolder::HotFolder

Constants

IGNORE_FOLDERS

Public Class Methods

new(input_path, error_path) click to toggle source

Create hot folder that listens for files.

@param [String] input_path Input folder path @param [String] error_path Error folder path @return [HotFolder]

# File lib/simple_hot_folder/hot_folder.rb, line 14
def initialize(input_path, error_path)
  @input_path = input_path
  @error_path = error_path
  @validate_file = default_validate_file_function
  @stop = false
end

Public Instance Methods

listen_input!() { |item| ... } click to toggle source

Yield a {Item} for each file/folder in the input path. Each file/folder is automatically deleted after the yield block is executed.

@yieldparam [Item] item The file/folder.

# File lib/simple_hot_folder/hot_folder.rb, line 44
def listen_input!
  while true
    process_input! { |item| yield item }
    return if @stop
    sleep(1)
  end
end
process_input!() { |item| ... } click to toggle source

Yield a {Item} for each file/folder in the input path. Each file/folder is automatically deleted after the yield block is executed.

@yieldparam [Item] item The file/folder.

# File lib/simple_hot_folder/hot_folder.rb, line 26
def process_input!
  entries = read_input(@input_path)
  entries.each do |item|
    begin
      return if @stop
      yield item
      FileUtils.rm(item.path) if File.exist?(item.path)
    rescue Exception => e
      move_file_to_error!(item, e)
    end
  end
end
stop_listening_after_this_item() click to toggle source
# File lib/simple_hot_folder/hot_folder.rb, line 52
def stop_listening_after_this_item
  @stop = true
end

Private Instance Methods

default_validate_file_function() click to toggle source
# File lib/simple_hot_folder/hot_folder.rb, line 87
def default_validate_file_function
  ->(file) { 
    true
  }
end
move_file_to_error!(item, exception) click to toggle source
# File lib/simple_hot_folder/hot_folder.rb, line 58
def move_file_to_error!(item, exception)
  FileUtils.mv(item.path, @error_path) if File.exist?(item.path)
  write_error_file(item, exception.message)
end
processable?(item) click to toggle source
# File lib/simple_hot_folder/hot_folder.rb, line 80
def processable?(item)
  return false if !File.file?(item.path)
  return false if IGNORE_FOLDERS.include?(item.name)
  return false if item.name.start_with?('.')
  true
end
read_input(path) click to toggle source
# File lib/simple_hot_folder/hot_folder.rb, line 68
def read_input(path)
  Dir.entries(path)
    .map { |name| Item.new(name, "#{path}/#{name}") }
    .keep_if { |item| processable?(item) }
    # .each { |item| p item.path }
    # .keep_if { |item| @validate_file.(item) }
end
validate_item(item) click to toggle source
# File lib/simple_hot_folder/hot_folder.rb, line 76
def validate_item(item)
  item
end
write_error_file(item, text) click to toggle source
# File lib/simple_hot_folder/hot_folder.rb, line 63
def write_error_file(item, text)
  path = "#{@error_path}/#{item.name}.txt"
  File.open(path, 'w') { |file| file.write(text) }
end