Class: Snoopit::Sniffer
- Inherits:
-
Object
- Object
- Snoopit::Sniffer
- Defined in:
- lib/snoopit/sniffer.rb
Overview
Does the actual searching for the given regular expression
Instance Attribute Summary collapse
-
#after ⇒ Object
readonly
Returns the value of attribute after.
-
#before ⇒ Object
readonly
Returns the value of attribute before.
-
#comment ⇒ Object
readonly
Returns the value of attribute comment.
-
#notifiers ⇒ Object
readonly
Returns the value of attribute notifiers.
-
#pre_before ⇒ Object
readonly
Returns the value of attribute pre_before.
-
#regexp ⇒ Object
readonly
Returns the value of attribute regexp.
-
#sniffed ⇒ Object
readonly
Returns the value of attribute sniffed.
Instance Method Summary collapse
- #as_json(options = nil) ⇒ Object
-
#initialize(sniffer_params) ⇒ Sniffer
constructor
This creates a
Sniffer
which was specified in thesnoopers.json
file in thesniffers
section. -
#setup_notifiers(params) ⇒ Object
Set up the specified notifier parameters.
- #to_json(*a) ⇒ Object
-
#track(file, line_no, line) ⇒ Object
This sniffer tracks through a file until it detects a match.
-
#tracking(line) ⇒ Object
This is tracking the lines after the match The
Detected
instance will let us know when we are finished collecting lines.
Constructor Details
#initialize(sniffer_params) ⇒ Sniffer
This creates a Sniffer
which was specified in the snoopers.json
file in the sniffers
section
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/snoopit/sniffer.rb', line 10 def initialize(sniffer_params) @before = sniffer_params['lines']['before'].nil? ? 2 : sniffer_params['lines']['before'] @pre_before = Register.new @before @after = sniffer_params['lines']['after'].nil? ? 2 : sniffer_params['lines']['after'] @comment = sniffer_params['comment'] @regexp = Regexp.new sniffer_params['regexp'] @notifiers = {} setup_notifiers sniffer_params @sniffed = [] end |
Instance Attribute Details
#after ⇒ Object (readonly)
Returns the value of attribute after.
6 7 8 |
# File 'lib/snoopit/sniffer.rb', line 6 def after @after end |
#before ⇒ Object (readonly)
Returns the value of attribute before.
6 7 8 |
# File 'lib/snoopit/sniffer.rb', line 6 def before @before end |
#comment ⇒ Object (readonly)
Returns the value of attribute comment.
6 7 8 |
# File 'lib/snoopit/sniffer.rb', line 6 def comment @comment end |
#notifiers ⇒ Object (readonly)
Returns the value of attribute notifiers.
6 7 8 |
# File 'lib/snoopit/sniffer.rb', line 6 def notifiers @notifiers end |
#pre_before ⇒ Object (readonly)
Returns the value of attribute pre_before.
6 7 8 |
# File 'lib/snoopit/sniffer.rb', line 6 def pre_before @pre_before end |
#regexp ⇒ Object (readonly)
Returns the value of attribute regexp.
6 7 8 |
# File 'lib/snoopit/sniffer.rb', line 6 def regexp @regexp end |
#sniffed ⇒ Object (readonly)
Returns the value of attribute sniffed.
6 7 8 |
# File 'lib/snoopit/sniffer.rb', line 6 def sniffed @sniffed end |
Instance Method Details
#as_json(options = nil) ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/snoopit/sniffer.rb', line 47 def as_json(=nil) { before: @before, after: @after, comment: @comment, regexp: @regexp.to_json, sniffed: @sniffed, notifiers: @notifiers } end |
#setup_notifiers(params) ⇒ Object
Set up the specified notifier parameters
23 24 25 |
# File 'lib/snoopit/sniffer.rb', line 23 def setup_notifiers(params) @notifiers = params['notify'] unless params['notify'].nil? end |
#to_json(*a) ⇒ Object
58 59 60 |
# File 'lib/snoopit/sniffer.rb', line 58 def to_json(*a) as_json.to_json(*a) end |
#track(file, line_no, line) ⇒ Object
This sniffer tracks through a file until it detects a match
31 32 33 34 35 36 37 |
# File 'lib/snoopit/sniffer.rb', line 31 def track(file, line_no, line) matched = @regexp.match(line) do |m| @sniffed << Detected.new(@comment, @pre_before, @after, line, file, line_no) end @pre_before.push_front line if matched.nil? tracking line end |
#tracking(line) ⇒ Object
This is tracking the lines after the match The Detected
instance will let us know when we are finished collecting lines
41 42 43 44 45 |
# File 'lib/snoopit/sniffer.rb', line 41 def tracking(line) @sniffed.each do |detected| detected.track line unless detected.finished? end end |