Class: RubySync::Connectors::FileConnector

Inherits:
BaseConnector show all
Defined in:
lib/ruby_sync/connectors/file_connector.rb

Overview

An abstract class that serves as the base for connectors that poll a filesystem directory for files and process them and/or write received events to a file.

Direct Known Subclasses

CsvFileConnector

Instance Attribute Summary

Attributes inherited from BaseConnector

#is_vault, #name, #once_only, #pipeline

Instance Method Summary collapse

Methods inherited from BaseConnector

#associate, #association_context, #association_for, #association_key_for, #association_to_path_dbm_filename, #associations_for, #can_act_as_vault?, class_for, class_name_for, #clean, #create_operations_for, #dbm_path, #digest, #each_entry, #entry_for_own_association_key, fields, #find_associated, #has_entry_for_key?, #initialize, #is_delete_echo?, #is_echo?, #is_vault?, #mirror_dbm_filename, #own_association_key_for, #path_for_association, #path_for_own_association_key, #path_to_association_dbm_filename, #remove_association, #remove_associations, #remove_mirror, sample_config, #start, #stop, #stopped, #sync_started, #sync_stopped, #test_add, #test_delete, #test_modify

Methods included from Utilities

#as_array, #base_path, #call_if_exists, #connector_called, #effective_operations, #ensure_dir_exists, #find_base_path, #get_preference, #get_preference_file_path, #include_in_search_path, #log_progress, #perform_operations, #pipeline_called, #set_preference, #something_called, #with_rescue

Constructor Details

This class inherits a constructor from RubySync::Connectors::BaseConnector

Instance Method Details

#add(path, operations) ⇒ Object

TODO: Write to a temp file first and then move it to where it will be picked up by the receiving process. TODO: Make this use the same file for multiple records depending upon configuration of maximum lines per file and a timeout



54
55
56
57
58
59
# File 'lib/ruby_sync/connectors/file_connector.rb', line 54

def add path, operations
  File.open(output_file_name, 'a') do |file|
    write_record(file, path, operations)
  end
  return true
end

#each_change(&blk) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ruby_sync/connectors/file_connector.rb', line 29

def each_change(&blk)
  unless in_glob
    log.error "in_glob not set on file connector. No files will be processed"
    return
  end
  base_path # Call it before changing directory
  log.info "#{name}: Scanning #{in_path} for #{in_glob} files..."
  Dir.chdir(in_path) do |path|
    Dir.glob(in_glob) do |filename|
      log.info "#{name}: Processing '#{filename}'"
      each_file_change filename, &blk
      FileUtils.mv filename, "#{filename}.bak"
    end
  end
end

#each_file_change(filename, &blk) ⇒ Object

Called for each filename matching in_glob in in_path



46
47
# File 'lib/ruby_sync/connectors/file_connector.rb', line 46

def each_file_change(filename,&blk)
end

#output_file_nameObject

Generate a unique and appropriate filename within the given path



69
70
71
# File 'lib/ruby_sync/connectors/file_connector.rb', line 69

def output_file_name
 File.join(out_path, Time.now.strftime('%Y%m%d%H%M%S') + out_extension)
end

#startedObject



24
25
26
27
# File 'lib/ruby_sync/connectors/file_connector.rb', line 24

def started
  ensure_dir_exists in_path
  ensure_dir_exists out_path
end

#write_record(file, path, operations) ⇒ Object

Called to append a given record to an open file. Subclasses of FileConnector should override this.

Raises:

  • (Exception)


63
64
65
# File 'lib/ruby_sync/connectors/file_connector.rb', line 63

def write_record file, path, operations
  raise Exception.new("#{name} needs to implement 'write_record file, path, operations'")
end