Class: Tairb

Inherits:
Object
  • Object
show all
Defined in:
lib/tairb.rb,
lib/tairb/version.rb,
lib/tairb/configuration.rb

Defined Under Namespace

Classes: Configuration

Constant Summary collapse

VERSION =
"0.0.1"

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Tairb

Returns a new instance of Tairb.



6
7
8
# File 'lib/tairb.rb', line 6

def initialize path
  @path = File.expand_path path
end

Instance Method Details

#run(config = {}) ⇒ Object



10
11
12
13
14
# File 'lib/tairb.rb', line 10

def run config={}
  config = Configuration.new self, config

  self.send config.tailf_method, config.bytes, config.filter, &config.script
end

#tailf(bytes = 10000, filter = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/tairb.rb', line 16

def tailf bytes=10000, filter=nil
  seeked_file(bytes) do |file|
    buf = ''
    loop do
      next unless str = file.read_nonblock(10) rescue !sleep(0.1)
      next print str unless block_given?
      buf += str
      lines = buf.split /\r\n?|\n/, -1
      while line = lines.shift
        break buf = line if lines.empty?
        next if filter && !filter.call(line)
        yield line
      end
    end
  end
rescue Interrupt
  # exit
end

#tailf_tsv(bytes = 10000, filter = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/tairb.rb', line 35

def tailf_tsv bytes=10000, filter=nil
  tailf(bytes) do |line|
    line.split("\t").inject({}) do |memo, token|
      key, val = token.split(':', 2)
      memo[key.intern] = val
      memo
    end.tap do |dat|
      next if filter && !filter.call(dat)
      block_given? ? yield(dat) : puts(to_tsv(dat))
    end
  end
end