Class: Carnivore::Files::Util::Fetcher
- Inherits:
-
Object
- Object
- Carnivore::Files::Util::Fetcher
- Includes:
- Utils::Logging, Celluloid
- Defined in:
- lib/carnivore-files/util/fetcher.rb,
lib/carnivore-files/util/poll.rb,
lib/carnivore-files/util/penguin.rb
Overview
Fetch lines from file
Defined Under Namespace
Instance Attribute Summary collapse
-
#delimiter ⇒ String
readonly
String to split messages on.
-
#io ⇒ IO
Underlying IO instance.
-
#messages ⇒ Queue
Messages.
-
#path ⇒ String
readonly
Path to file.
Instance Method Summary collapse
-
#build_io ⇒ TrueClass, FalseClass
Build the IO and monitor.
-
#initialize(args = {}) ⇒ Fetcher
constructor
Create new instance.
-
#retrieve_lines ⇒ Object
Retreive lines from file.
-
#start_fetcher ⇒ Object
Start the line fetcher.
-
#write_line(line) ⇒ Integer
Write line to IO.
Constructor Details
#initialize(args = {}) ⇒ Fetcher
Create new instance
32 33 34 35 36 37 |
# File 'lib/carnivore-files/util/fetcher.rb', line 32 def initialize(args={}) @leftover = '' @path = ::File.(args[:path]) @delimiter = args.fetch(:delimiter, "\n") = Queue.new end |
Instance Attribute Details
#delimiter ⇒ String (readonly)
Returns string to split messages on.
19 20 21 |
# File 'lib/carnivore-files/util/fetcher.rb', line 19 def delimiter @delimiter end |
#io ⇒ IO
Returns underlying IO instance.
24 25 26 |
# File 'lib/carnivore-files/util/fetcher.rb', line 24 def io @io end |
#messages ⇒ Queue
Returns messages.
22 23 24 |
# File 'lib/carnivore-files/util/fetcher.rb', line 22 def end |
#path ⇒ String (readonly)
Returns path to file.
17 18 19 |
# File 'lib/carnivore-files/util/fetcher.rb', line 17 def path @path end |
Instance Method Details
#build_io ⇒ TrueClass, FalseClass
Build the IO and monitor
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/carnivore-files/util/fetcher.rb', line 77 def build_io unless(io) if(::File.exists?(path)) @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. << l end end else wait_for_file build_io end true else false end end |
#retrieve_lines ⇒ Object
Retreive lines from file
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/carnivore-files/util/fetcher.rb', line 57 def retrieve_lines if(io) io.pos = @history_pos if @history_pos data = io.read(4096) while(data = io.read(4096)) @leftover << data data = io.read(4096) 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 ⇒ Object
Start the line fetcher
40 41 42 |
# File 'lib/carnivore-files/util/fetcher.rb', line 40 def start_fetcher raise NotImplementedError end |
#write_line(line) ⇒ Integer
Write line to IO
48 49 50 51 52 53 54 |
# 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 |