Class: Snoopit::Snoopy

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

Overview

Snoops around the specified file or directory of files

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, params) ⇒ Snoopy

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

Parameters:

  • name (String)

    the name of the snoopy

  • params (Hash)

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



11
12
13
14
15
16
17
18
# File 'lib/snoopit/snoopy.rb', line 11

def initialize(name, params)
  @name = name
  @output = params['output']
  setup_input params
  setup_dir params unless params['dir'].nil?
  input_check?
  setup_sniffers params
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



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

def dir
  @dir
end

#globObject (readonly)

Returns the value of attribute glob.



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

def glob
  @glob
end

#inputObject (readonly)

Returns the value of attribute input.



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

def input
  @input
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#outputObject (readonly)

Returns the value of attribute output.



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

def output
  @output
end

#sniffersObject (readonly)

Returns the value of attribute sniffers.



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

def sniffers
  @sniffers
end

Instance Method Details

#as_json(options = nil) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/snoopit/snoopy.rb', line 68

def as_json(options=nil)
  {
      name: @name,
      input: @input,
      dir: @dir,
      glob: @glob,
      sniffers: @sniffers
  }
end

#dir?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/snoopit/snoopy.rb', line 50

def dir?
  ! @dir.nil?
end

#glob?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/snoopit/snoopy.rb', line 54

def glob?
  ! @glob.nil?
end

#input_check?Boolean

Ensure we have something to snoop

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


34
35
36
37
38
# File 'lib/snoopit/snoopy.rb', line 34

def input_check?
  return true unless @input.nil?
  return true unless @dir.nil?
  raise ArgumentError.new('Snooper JSON must contain either an input or dir parameter')
end

#setup_dir(params) ⇒ Object

Get the name of the directory to snoop if specified

Parameters:

  • params (Hash)

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



28
29
30
31
# File 'lib/snoopit/snoopy.rb', line 28

def setup_dir(params)
  @dir = params['dir']['path']
  @glob = params['dir']['glob']
end

#setup_input(params) ⇒ Object

Get the name of the file to snoop if specified

Parameters:

  • params (Hash)

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



22
23
24
# File 'lib/snoopit/snoopy.rb', line 22

def setup_input(params)
  @input = params['snoop']
end

#setup_sniffers(params) ⇒ Object

Create the specified sniffers

Parameters:

  • params (Hash)

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

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
# File 'lib/snoopit/snoopy.rb', line 42

def setup_sniffers(params)
  raise ArgumentError.new('Snooper JSON missing sniffers array') if params['sniffers'].nil?
  @sniffers = []
  params['sniffers'].each do |sniffer|
    @sniffers << Sniffer.new(sniffer)
  end
end

#sniff(file, line_no, line) ⇒ Object

Sniff specified line which is from the named file param file [String] file Snoopy is sniffing param line_no [Integer] line_n0 Snoopy is sniffing param line [String] line Snoopy is sniffing



62
63
64
65
66
# File 'lib/snoopit/snoopy.rb', line 62

def sniff(file, line_no, line)
  @sniffers.each do |sniffer|
    sniffer.track file, line_no, line
  end
end

#to_json(*a) ⇒ Object



78
79
80
# File 'lib/snoopit/snoopy.rb', line 78

def to_json(*a)
 as_json.to_json(*a)
end