class Plextail::Line

Constants

DEFAULTS

Attributes

file[RW]
hostname[RW]
message[RW]
message_id[RW]
process_id[RW]
raw[RW]
timestamp[RW]
token[RW]
version[RW]

Public Class Methods

new(file, raw, &block) click to toggle source
# File lib/plextail/line.rb, line 12
def initialize(file, raw, &block)
  @file, @raw = file, raw
  @version    = DEFAULTS[:version]
  @timestamp  = current_timestamp
  @hostname   = DEFAULTS[:hostname]
  @message_id = DEFAULTS[:message_id]
  @message    = raw.dup

  block.call self if block_given?
end

Public Instance Methods

to_s() click to toggle source
# File lib/plextail/line.rb, line 23
def to_s
  unless valid?
    raise Plextail::InvalidLineError, "Missing #{missing_parts.join(', ')}"
  end

  string = parts.join(' ')

  "#{string.bytes.to_a.length} #{string}"
end
valid?() click to toggle source
# File lib/plextail/line.rb, line 33
def valid?
  parts.all? { |part| part && part.length > 0 }
end

Private Instance Methods

current_timestamp() click to toggle source
# File lib/plextail/line.rb, line 39
def current_timestamp
  time     = Time.now
  fraction = (".%06i" % time.usec)[0, 7]
  "#{time.strftime("%Y-%m-%dT%H:%M:%S")}#{fraction}+00:00"
end
missing_parts() click to toggle source
# File lib/plextail/line.rb, line 45
def missing_parts
  [
    :version, :timestamp, :hostname, :token, :process_id, :message_id,
    :message
  ].select { |part| send(part).nil? || send(part).length == 0 }
end
parts() click to toggle source
# File lib/plextail/line.rb, line 52
def parts
  [version, timestamp, hostname, token, process_id, message_id, message]
end