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)


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

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



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

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:



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

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.



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

def match_key
  @match_key
end

#pathString (readonly)

Returns:

  • (String)


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

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.



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

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



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

def protocol
  @protocol
end

Instance Method Details

#parse_fingerprintsvoid

This method returns an undefined value.



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 52

def parse_fingerprints
  xml = nil

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

  raise "#{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

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

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

  xml = nil
end