class Carnivore::Files::Util::Fetcher
Fetch lines from file
Attributes
delimiter[R]
@return [String] string to split messages on
io[RW]
@return [IO] underlying IO instance
messages[RW]
@return [Queue] messages
path[R]
@return [String] path to file
Public Class Methods
new(args={})
click to toggle source
Create new instance
@param args [Hash] initialization args @option args [String] :path path to file @option args [String] :delimiter string delimiter to break messages
# File lib/carnivore-files/util/fetcher.rb, line 32 def initialize(args={}) @leftover = '' @path = ::File.expand_path(args[:path]) @delimiter = args.fetch(:delimiter, "\n") @messages = args.fetch(:queue, Queue.new) end
Public Instance Methods
build_io()
click to toggle source
Build the IO and monitor
@return [TrueClass, FalseClass]
# File lib/carnivore-files/util/fetcher.rb, line 76 def build_io unless(io) if(::File.exists?(path)) @history_pos = 0 @io = ::File.open(path, 'r') unless(@waited) @io.seek(0, ::IO::SEEK_END) # fast-forward to EOF else @waited = false retrieve_lines.each do |l| self.messages << l end end else wait_for_file build_io end true else false end end
retrieve_lines()
click to toggle source
Retreive lines from file
# File lib/carnivore-files/util/fetcher.rb, line 57 def retrieve_lines if(io) io.pos = @history_pos if @history_pos @leftover << io.read(4096).to_s while(data = io.read(4096)) @leftover << data.to_s end @history_pos = io.pos result = @leftover.split(delimiter) @leftover.replace @leftover.end_with?(delimiter) ? '' : result.pop.to_s result else [] end end
start_fetcher()
click to toggle source
Start the line fetcher
# File lib/carnivore-files/util/fetcher.rb, line 40 def start_fetcher raise NotImplementedError end
write_line(line)
click to toggle source
Write line to IO
@param line [String] @return [Integer] bytes written
# File lib/carnivore-files/util/fetcher.rb, line 48 def write_line(line) if(io) io.puts(line) else raise 'No IO detected! Failed to write.' end end