class Tairb

Constants

VERSION

Public Class Methods

new(path) click to toggle source
# File lib/tairb.rb, line 6
def initialize path
  @path = File.expand_path path
end

Public Instance Methods

run(config={}) click to toggle source
# 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) { |line| ... } click to toggle source
# 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) { |dat| ... } click to toggle source
# 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

Private Instance Methods

seeked_file(bytes, &block) click to toggle source
# File lib/tairb.rb, line 49
def seeked_file bytes, &block
  open(@path, 'r') do |file|
    raise "Not supported file type: #{file.stat.ftype}." unless file.stat.ftype == 'file'
    file.seek(file.stat.size.tap { |size| break size > bytes ? size - bytes : 0 })
    block.call file
  end
end
to_s(line) click to toggle source
# File lib/tairb.rb, line 57
def to_s line
  line.to_s
end
to_tsv(dat) click to toggle source
# File lib/tairb.rb, line 60
def to_tsv dat
  dat.map{ |k, v| "#{k}:#{v}" }.join("\t")
end