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



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)



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

def database_type
  @database_type
end

#fingerprintsArray<Fingerprint> (readonly)



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

def fingerprints
  @fingerprints
end

#match_keyString (readonly)



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

def match_key
  @match_key
end

#pathString (readonly)



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

def path
  @path
end

#preferenceFloat (readonly)



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

def preference
  @preference
end

#protocolString (readonly)



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

def protocol
  @protocol
end

Instance Method Details

#parse_fingerprintsvoid



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