Class: Recog::DB
- Inherits:
-
Object
- Object
- Recog::DB
- 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
-
#database_type ⇒ String
readonly
Taken from the
fingerprints/database_type
attribute defaults to an empty string. -
#fingerprints ⇒ Array<Fingerprint>
readonly
Fingerprint objects that can be matched against strings that make sense for the #match_key.
-
#match_key ⇒ String
readonly
Taken from the
fingerprints/matches
attribute, or defaults to the basename of #path without the.xml
extension. - #path ⇒ String readonly
-
#preference ⇒ Float
readonly
Taken from the
fingerprints/preference
attribute, defaults to 0.10. -
#protocol ⇒ String
readonly
Taken from the
fingerprints/protocol
attribute, or defaults to an empty string.
Instance Method Summary collapse
-
#initialize(path) ⇒ DB
constructor
A new instance of DB.
- #parse_fingerprints ⇒ void
Constructor Details
#initialize(path) ⇒ DB
Returns a new instance of 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_type ⇒ String (readonly)
Returns 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 |
#fingerprints ⇒ Array<Fingerprint> (readonly)
Returns Fingerprint objects that can be matched against strings that make sense for the #match_key.
15 16 17 |
# File 'lib/recog/db.rb', line 15 def fingerprints @fingerprints end |
#match_key ⇒ String (readonly)
Returns 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 |
#path ⇒ String (readonly)
11 12 13 |
# File 'lib/recog/db.rb', line 11 def path @path end |
#preference ⇒ Float (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.
32 33 34 |
# File 'lib/recog/db.rb', line 32 def preference @preference end |
#protocol ⇒ String (readonly)
Returns 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_fingerprints ⇒ void
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 |