Class: Indy::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/indy/source.rb

Overview

A StringIO interface to the underlying log source.

Defined Under Namespace

Classes: Invalid

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Source) initialize(param)

Creates a Source object.

Parameters:

  • param (String, Hash)

    The source content String, filepath String, or :cmd => 'command' Hash

Raises:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/indy/source.rb', line 25

def initialize(param)
  raise Indy::Source::Invalid if param.nil?
  if param.respond_to?(:keys)
    set_connection(:cmd, param[:cmd]) if param[:cmd]
    set_connection(:file, param[:file]) if ( param[:file] and File.size(param[:file].path) > 0 )
    set_connection(:string, param[:string]) if param[:string]
  elsif param.respond_to?(:read) and param.respond_to?(:rewind)
      set_connection(:file, param)
  elsif param.respond_to?(:to_s) and param.respond_to?(:length)
    # fall back to source being the string passed in
    set_connection(:string, param)
  else
    raise Indy::Source::Invalid
  end


end

Instance Attribute Details

- (Object) connection (readonly)

log source connection string (cmd, filename or log data)



12
13
14
# File 'lib/indy/source.rb', line 12

def connection
  @connection
end

- (Object) io (readonly)

the StringIO object



15
16
17
# File 'lib/indy/source.rb', line 15

def io
  @io
end

- (Object) type (readonly)

log source type. :cmd, :file, or :string



9
10
11
# File 'lib/indy/source.rb', line 9

def type
  @type
end

Instance Method Details

- (Object) exec_command(command_string)

Execute the source's connection string, returning an IO object

Parameters:

  • command_string (String)

    string of command that will return log contents



88
89
90
91
92
93
94
95
96
# File 'lib/indy/source.rb', line 88

def exec_command(command_string)
  begin
    io = IO.popen(command_string)
    return nil if io.eof?
  rescue
    nil
  end
  io
end

- (Object) lines

array of log lines from source



111
112
113
114
# File 'lib/indy/source.rb', line 111

def lines
  load_data unless @lines
  @lines
end

- (Object) load_data

read source data and populate instance variables

TODO: hmmm... not called when Source#open is called directly, but #load_data would call open again. :(



121
122
123
124
125
126
# File 'lib/indy/source.rb', line 121

def load_data
  self.open
  @lines = @io.readlines
  @io.rewind
  @num_lines = @lines.count
end

- (Object) num_lines

the number of lines in the source



103
104
105
106
# File 'lib/indy/source.rb', line 103

def num_lines
  load_data unless @num_lines
  @num_lines
end

- (Object) open(time_search = nil)

Return a StringIO object to provide access to the underlying log source



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/indy/source.rb', line 54

def open(time_search=nil)
  begin

    case @type
    when :cmd
      @io = StringIO.new( exec_command(@connection).read )
      raise "Failed to execute command (#{@connection})" if @io.nil?

    when :file
      @connection.rewind
      @io = StringIO.new(@connection.read)
      raise "Failed to open file: #{@connection}" if @io.nil?

    when :string
      @io = StringIO.new( @connection )

    else
      raise RuntimeError, "Invalid log source type: #{@type.inspect}"
    end

  rescue Exception => e
    raise Indy::Source::Invalid, "Unable to open log source. (#{e.message})"
  end

  # scope_by_time(source_io) if time_search

  @io
end

- (Object) set_connection(type, value)

set the source connection type and connection_string



46
47
48
49
# File 'lib/indy/source.rb', line 46

def set_connection(type, value)
  @type = type
  @connection = value
end