Class: Snoopit::Snooper

Inherits:
Object
  • Object
show all
Defined in:
lib/snoopit/snooper.rb

Overview

Coordinates activities between the NotificationManager the Snoopies their Sniffers and the FileTracker

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(notifications = true, db_file = nil, logger = nil, log_level = ::Logger::INFO) ⇒ Snooper

Snoopies are the list of available named snoopers

Parameters:

  • generate (boolean #notifications)

    notifications

  • used (String #db_file)

    for file tracking

  • use (Logger #logger)

    the passed in logger



17
18
19
20
21
22
23
# File 'lib/snoopit/snooper.rb', line 17

def initialize(notifications=true, db_file=nil, logger=nil, log_level=::Logger::INFO)
  @snoopies = { }
  @file_tracker = FileTracker.new db_file unless db_file.nil?
  @notifier = NotificationManager.new if notifications
  Snoopit::Logging.create_logger(logger) unless logger.nil?
  Snoopit.logger.level = log_level
end

Instance Attribute Details

#file_trackerObject

snoopies table of Snoopy instances that snoop files notifier NotificationManager manages distributing notifications file_tracker



11
12
13
# File 'lib/snoopit/snooper.rb', line 11

def file_tracker
  @file_tracker
end

#notifierObject

snoopies table of Snoopy instances that snoop files notifier NotificationManager manages distributing notifications file_tracker



11
12
13
# File 'lib/snoopit/snooper.rb', line 11

def notifier
  @notifier
end

#snoopiesObject

snoopies table of Snoopy instances that snoop files notifier NotificationManager manages distributing notifications file_tracker



11
12
13
# File 'lib/snoopit/snooper.rb', line 11

def snoopies
  @snoopies
end

Instance Method Details

#file_read(snoopy, file_name) ⇒ Object

Have the snoopy sniff a file

Parameters:

  • snoopy (Snoopy)
    • current active snoopy

  • file_name (String)
    • file to be snooped



178
179
180
181
182
183
184
# File 'lib/snoopit/snooper.rb', line 178

def file_read(snoopy, file_name)
  line_no = 0
  File.foreach file_name do |line|
    snoopy.sniff snoopy.input, line_no, line
    line_no += 1
  end
end

#file_track_read(snoopy, file_name) ⇒ Object

Have the snoopy sniff a file that is being tracked

Parameters:

  • snoopy (Snoopy)
    • current active snoopy

  • file_name (String)
    • file to be snooped



169
170
171
172
173
# File 'lib/snoopit/snooper.rb', line 169

def file_track_read(snoopy, file_name)
  @file_tracker.foreach file_name do |line, line_no|
    snoopy.sniff snoopy.input, line_no, line
  end
end

#get_file_list(snoopy) ⇒ Object

Get the files that match the glob expression so the snoopy can snoop the files

Parameters:

  • snoopy (Snoopy)
    • current active snoopy



141
142
143
144
# File 'lib/snoopit/snooper.rb', line 141

def get_file_list(snoopy)
  Snoopit.logger.debug "Snooper directory: #{snoopy.dir}"
  Dir.entries snoopy.dir
end

#get_files(snoopy) ⇒ Object

Get the files the snoopy must sniff out for information

Parameters:

  • snoopy (Snoopy)
    • current active snoopy



116
117
118
119
120
121
122
123
# File 'lib/snoopit/snooper.rb', line 116

def get_files(snoopy)
  if snoopy.glob?
    files = get_glob_list snoopy
  else
    files = get_file_list snoopy
  end
  files
end

#get_glob_list(snoopy) ⇒ Object

Get the files that match the glob expression so the snoopy can snoop the files

Parameters:

  • snoopy (Snoopy)
    • current active snoopy



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/snoopit/snooper.rb', line 127

def get_glob_list(snoopy)
  Snoopit.logger.debug "Snooping glob: #{snoopy.glob}"
  cwd = Dir.getwd
  begin
    Dir.chdir snoopy.dir
    files = Dir.glob snoopy.glob
  ensure
    Dir.chdir cwd
  end
  files
end

#get_snoopers(names = []) ⇒ Object

Get all the snoopers or only the named snoopers

Parameters:

  • names (Array) (defaults to: [])
    • an array of string names



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/snoopit/snooper.rb', line 91

def get_snoopers(names=[])
  snoopers = []
  use_names = (names.size == 0 ? false : true)
  snoopies.each do |key, snooper|
    if use_names
      snoopers << snooper if names.include? key
    else
      snoopers << snooper
    end
  end
  snoopers
end

#load_file(snoopies_file) ⇒ Object

Load the configuration from a file Calls load_snoopers(json_hash) and load_notifiers(json_hash) and

Parameters:

  • snoopies_file (String)

    path to file



28
29
30
31
32
33
# File 'lib/snoopit/snooper.rb', line 28

def load_file(snoopies_file)
  raise ArgumentError.new "Invalid Snooper JSON File: #{snoopies_file}" if (snoopies_file.nil?) || (! File.exist? snoopies_file)
  json_hash = JSON.parse(IO.read(snoopies_file))
  load_snoopers json_hash
  load_notifiers json_hash
end

#load_json(json) ⇒ Object

Load the configuration from a file

Parameters:

  • json (String)

    json string



37
38
39
40
41
# File 'lib/snoopit/snooper.rb', line 37

def load_json(json)
  json_hash = JSON.parse(json)
  load_snoopers json_hash
  load_notifiers json_hash
end

#load_notifiers(json_hash) ⇒ Object

Load notifiers from the notifiers section

Parameters:

  • json_hash (Hash)


55
56
57
# File 'lib/snoopit/snooper.rb', line 55

def load_notifiers(json_hash)
  @notifier.load_notifier_config json_hash['notifiers'] unless @notifier.nil?
end

#load_snoopers(json_hash) ⇒ Object

Load the configuration from a file

Parameters:

  • json_hash (Hash)


45
46
47
48
49
50
51
# File 'lib/snoopit/snooper.rb', line 45

def load_snoopers(json_hash)
  snoopies_json = json_hash['snoopers']
  snoopies_json.each do |name, snooper|
    @snoopies[name] =  Snoopy.new(name, snooper)
  end
  raise ArgumentError.new 'There are no Snoopies in the JSON Snooper ' if @snoopies.size == 0
end

#register_notifier(notifier) ⇒ Object

Register the given notifier that inherits from the Notifier class and implements the notify method



62
63
64
# File 'lib/snoopit/snooper.rb', line 62

def register_notifier(notifier)
  @notifier.register notifier
end

#sniff_it(snoopy, file_name) ⇒ Object

Have the snoopy sniff a file for

Parameters:

  • snoopy (Snoopy)
    • current active snoopy

  • file_name (String)
    • file to be snooped



157
158
159
160
161
162
163
164
# File 'lib/snoopit/snooper.rb', line 157

def sniff_it(snoopy, file_name)
  Snoopit.logger.debug "Sniffing file: #{file_name} with snoopy: #{snoopy.name}"
  unless @file_tracker.nil?
    file_track_read(snoopy, file_name)
  else
    file_read(snoopy, file_name)
  end
end

#snoop(names = []) ⇒ Object

Start the snooping files. If a list of names are given then the only the snoopies in the list will be used

Parameters:

  • names (Array) (defaults to: [])
    • an array of string names



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/snoopit/snooper.rb', line 76

def snoop(names=[])
  snoopers = get_snoopers names
  snoopers.each do |snoopy|
    if (!snoopy.dir.nil?) && (snoopy.dir?)
      snoop_dir snoopy
    else
      snoop_file snoopy
    end
  end
  @notifier.notify snoopers unless @notifier.nil?
  snoopers
end

#snoop_dir(snoopy) ⇒ Object

Have the given snoopy sniff the files in its specified directory

Parameters:

  • snoopy (Snoopy)
    • current active snoopy



106
107
108
109
110
111
112
# File 'lib/snoopit/snooper.rb', line 106

def snoop_dir(snoopy)
  Snoopit.logger.debug "Snooping directory: #{snoopy.dir}"
  get_files(snoopy).each do |file|
    next if File.directory? file
    sniff_it snoopy, "#{snoopy.dir}/#{file}"
  end
end

#snoop_file(snoopy) ⇒ Object

Have the snoopy snoop a file

Parameters:

  • snoopy (Snoopy)
    • current active snoopy



148
149
150
151
152
# File 'lib/snoopit/snooper.rb', line 148

def snoop_file(snoopy)
  raise ArgumentError.new "Could find file #{snoopy.input}" unless File.exist? snoopy.input
  Snoopit.logger.debug "Snooping file: #{snoopy.input} with snoopy: #{snoopy.name}"
  sniff_it snoopy, snoopy.input
end

#unregister_notifier(notifier) ⇒ Object

Unegister the given notifier that inherits from the Notifier class and implements the notify method



69
70
71
# File 'lib/snoopit/snooper.rb', line 69

def unregister_notifier(notifier)
  @notifier.unregister notifier
end