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

Constant Summary collapse

DEFAULT_FP_PREFERENCE =

Default Fingerprint database preference when it isn't specified in file Do not use a value below 0.10 so as to allow users to specify lower values in their own custom XML that will always run last.

0.10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ DB

Returns a new instance of DB.

Parameters:

  • path (String)


39
40
41
42
43
44
45
46
47
48
# File 'lib/recog/db.rb', line 39

def initialize(path)
  @match_key = nil
  @protocol = ''
  @database_type = ''
  @preference = DEFAULT_FP_PREFERENCE.to_f
  @path = path
  @fingerprints = []

  parse_fingerprints
end

Instance Attribute Details

#database_typeString (readonly)

Returns Taken from the fingerprints/database_type attribute defaults to an empty string.

Returns:

  • (String)

    Taken from the fingerprints/database_type attribute defaults to an empty string



26
27
28
# File 'lib/recog/db.rb', line 26

def database_type
  @database_type
end

#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 attribute, or defaults to the basename of #path without the .xml extension.

Returns:

  • (String)

    Taken from the fingerprints/matches attribute, 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

#preferenceFloat (readonly)

Returns Taken from the fingerprints/preference attribute, defaults to 0.10. Used when ordering databases, highest numbers are given priority and are processed first.

Returns:

  • (Float)

    Taken from the fingerprints/preference attribute, defaults to 0.10. Used when ordering databases, highest numbers are given priority and are processed first.



31
32
33
# File 'lib/recog/db.rb', line 31

def preference
  @preference
end

#protocolString (readonly)

Returns Taken from the fingerprints/protocol attribute, or defaults to an empty string.

Returns:

  • (String)

    Taken from the fingerprints/protocol attribute, or defaults to an empty string



22
23
24
# File 'lib/recog/db.rb', line 22

def protocol
  @protocol
end

Instance Method Details

#parse_fingerprintsvoid

This method returns an undefined value.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/recog/db.rb', line 51

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|

    @match_key = fbase['matches'].to_s if fbase['matches']
    @protocol = fbase['protocol'].to_s if fbase['protocol']
    @database_type = fbase['database_type'].to_s if fbase['database_type']
    @preference = fbase['preference'].to_f if fbase['preference']

  end

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

  xml.xpath('/fingerprints/fingerprint').each do |fprint|
    @fingerprints << Fingerprint.new(fprint, @match_key, @protocol)
  end

  xml = nil
end