Class: Recog::DB

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

Overview

A collection of fingerprints for matching against a particular kind of fingerprintable data, e.g. an HTTP Server header

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ DB

Returns a new instance of DB.

Parameters:

  • path (String)


21
22
23
24
25
26
27
# File 'lib/recog/db.rb', line 21

def initialize(path)
  @match_key = nil
  @path = path
  @fingerprints = []

  parse_fingerprints
end

Instance Attribute Details

#fingerprintsArray<Fingerprint> (readonly)

Returns Fingerprint objects that can be matched against strings that make sense for the #match_key.

Returns:



14
15
16
# File 'lib/recog/db.rb', line 14

def fingerprints
  @fingerprints
end

#match_keyString (readonly)

Returns Taken from the fingerprints/matches element, or defaults to the basename of #path without the .xml extension.

Returns:

  • (String)

    Taken from the fingerprints/matches element, or defaults to the basename of #path without the .xml extension.



18
19
20
# File 'lib/recog/db.rb', line 18

def match_key
  @match_key
end

#pathString (readonly)

Returns:

  • (String)


10
11
12
# File 'lib/recog/db.rb', line 10

def path
  @path
end

Instance Method Details

#parse_fingerprintsvoid

This method returns an undefined value.



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
# File 'lib/recog/db.rb', line 30

def parse_fingerprints
  xml = nil

  File.open(self.path, "rb") do |fd|
    xml = Nokogiri::XML(fd.read(fd.stat.size))
  end

  raise "#{self.path} is invalid XML: #{xml.errors.join(',')}" unless xml.errors.empty?

  xml.xpath("/fingerprints").each do |fbase|
    if fbase['matches']
      @match_key = fbase['matches'].to_s
    end
  end

  unless @match_key
    @match_key = File.basename(self.path).sub(/\.xml$/, '')
  end

  xml.xpath("/fingerprints/fingerprint").each do |fprint|
    @fingerprints << Fingerprint.new(fprint)
  end

  xml = nil
end