Class: Recog::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/recog/matcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fingerprints, reporter, multi_match) ⇒ Matcher

Returns a new instance of Matcher.

Parameters:

  • fingerprints

    Array of [Recog::Fingerprint] The list of fingerprints from the Recog DB to find possible matches.

  • reporter (Recog::MatchReporter)

    The reporting structure that holds the matches and fails

  • multi_match (Boolean)

    specifies whether or not to use multi-match (true) or not (false)



8
9
10
11
12
# File 'lib/recog/matcher.rb', line 8

def initialize(fingerprints, reporter, multi_match)
  @fingerprints = fingerprints
  @reporter = reporter
  @multi_match = multi_match
end

Instance Attribute Details

#fingerprintsObject (readonly)

Returns the value of attribute fingerprints.



3
4
5
# File 'lib/recog/matcher.rb', line 3

def fingerprints
  @fingerprints
end

#multi_matchObject (readonly)

Returns the value of attribute multi_match.



3
4
5
# File 'lib/recog/matcher.rb', line 3

def multi_match
  @multi_match
end

#reporterObject (readonly)

Returns the value of attribute reporter.



3
4
5
# File 'lib/recog/matcher.rb', line 3

def reporter
  @reporter
end

Instance Method Details

#match_banners(banners_file) ⇒ Object

Parameters:

  • banners_file (String)

    The source of banners to attempt to match against the Recog DB.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/recog/matcher.rb', line 15

def match_banners(banners_file)
  reporter.report do

    fd = $stdin
    file_source = false

    if banners_file and banners_file != "-"
      fd = File.open(banners_file, "rb")
      file_source = true
    end

    fd.each_line do |line|
      reporter.increment_line_count

      line = line.to_s.unpack("C*").pack("C*").strip.gsub(/\\[rn]/, '')
      found_extractions = false

      all_extractions = []
      fingerprints.each do |fp|
        extractions = fp.match(line)
        if extractions
          found_extractions = true
          extractions['data'] = line
          if multi_match
            all_extractions << extractions
          else
            reporter.match "MATCH: #{extractions.inspect}"
            break
          end
        end
      end

      if found_extractions
        match_prefix = all_extractions.size > 1 ? 'MATCHES' : 'MATCH'
        reporter.match "#{match_prefix}: #{all_extractions.map(&:inspect).join(',')}" if multi_match
      else
        reporter.failure "FAIL: #{line}"
      end

      if reporter.stop?
        break
      end

    end

    fd.close if file_source

  end
end