Class: Snoopit::Sniffer

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

Overview

Does the actual searching for the given regular expression

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sniffer_params) ⇒ Sniffer

This creates a Sniffer which was specified in the snoopers.json file in the sniffers section

Parameters:

  • sniffer_params (Hash)

    this is a hash from the snoopers.json file in the sniffers section for the assciated Snoopy



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

#afterObject (readonly)

Returns the value of attribute after.



6
7
8
# File 'lib/snoopit/sniffer.rb', line 6

def after
  @after
end

#beforeObject (readonly)

Returns the value of attribute before.



6
7
8
# File 'lib/snoopit/sniffer.rb', line 6

def before
  @before
end

#commentObject (readonly)

Returns the value of attribute comment.



6
7
8
# File 'lib/snoopit/sniffer.rb', line 6

def comment
  @comment
end

#notifiersObject (readonly)

Returns the value of attribute notifiers.



6
7
8
# File 'lib/snoopit/sniffer.rb', line 6

def notifiers
  @notifiers
end

#pre_beforeObject (readonly)

Returns the value of attribute pre_before.



6
7
8
# File 'lib/snoopit/sniffer.rb', line 6

def pre_before
  @pre_before
end

#regexpObject (readonly)

Returns the value of attribute regexp.



6
7
8
# File 'lib/snoopit/sniffer.rb', line 6

def regexp
  @regexp
end

#sniffedObject (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(options=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

Parameters:

  • params (Hash)

    this is a hash from the snoopers.json file in the notify section for the associated Sniffer



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

Parameters:

  • file (String)

    file the sniffer is tracking

  • line_no (Integer)

    line number the sniffer is tracking

  • line (String)

    line the sniffer is tracking



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