lib/cpe.rb in cpe-0.1.1 vs lib/cpe.rb in cpe-0.3.0
- old
+ new
@@ -1,11 +1,24 @@
# = cpe.rb
#
# Copyright (c) Chris Wuest <chris@chriswuest.com>
# Expectr is freely distributable under the terms of an MIT-style license.
# See LICENSE.txt or http://www.opensource.org/licenses/mit-license.php.
+#
+# Fixes for Ruby pre-1.9
+if RUBY_VERSION =~ /^1.[^9]/
+ #
+ # Ruby does not implement KeyError before 1.9
+ #
+ class KeyError < IndexError
+ def initialize message = nil
+ super message || "Key not found"
+ end
+ end
+end
+
# == Description
# Cpe is a small library built to simplify working with the Common Platform
# Enumeration spec managed by Mitre. See http://cpe.mitre.org/ for further
# details.
#
@@ -62,22 +75,27 @@
raise KeyError unless /\/[oah]/.match @part
raise KeyError if @vendor.to_s.empty? and @product.to_s.empty? and @version.to_s.empty? and @update.to_s.empty? and @edition.to_s.empty? and @language.to_s.empty?
return ["cpe", @part, @vendor, @product, @version, @update, @edition, @language].join(":").downcase
end
+ #
+ # Test for equality of generated CPE strings
+ def == cpe
+ raise ArgumentError unless cpe.kind_of? Cpe
+ self.generate == cpe.generate
+ end
+
#
# Parse pre-existing CPE string and return new Cpe object
#
- # String parsing is permissive regarding the number of trailing colons
+ # String parsing is permissive regarding the number of trailing colons and whitespace
# provided, filling in empty strings if needed.
#
- # === Bugs
- #
- # * Presently unable to pass File object as input
- #
def Cpe.parse cpe
- raise ArgumentError unless cpe.kind_of? String
- raise ArgumentError, "CPE malformed" unless /^cpe:\/[hoa]:/.match cpe and !/ /.match cpe
+ raise ArgumentError unless cpe.kind_of? String or cpe.kind_of? File
+ cpe = cpe.read if cpe.kind_of? File
+ cpe = cpe.downcase.chomp
+ raise ArgumentError, "CPE malformed" unless /^cpe:\/[hoa]:/.match cpe and !/[\s\n]/.match cpe
data = Hash.new
discard, data[:part], data[:vendor], data[:product], data[:version],
data[:update], data[:edition], data[:language] = cpe.split(/:/, 8)