lib/verifier.rb in inqlude-0.0.6 vs lib/verifier.rb in inqlude-0.0.7

- old
+ new

@@ -14,49 +14,90 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. class Verifier + class Result + attr_accessor :valid, :errors, :name + + def initialize + @valid = false + @errors = Array.new + end + + def valid? + @valid + end + + def print_result + print "Verify manifest #{@name}..." + if valid? + puts "ok" + else + puts "error" + @errors.each do |error| + puts " #{error}" + end + end + end + end + def initialize settings @settings = settings @allowed_keys = [ "schema_version", "name", "version", "release_date", "summary", "urls", "licenses", "description", "authors", "maturity", "platforms", "packages", "keywords", "dependencies", "filename", "libraryname" ] + @mandatory_keys = [ "schema_version", "name", "version", "release_date", + "summary", "urls", "licenses", "description", "maturity", + "platforms", "packages" ] end def verify manifest - @errors = Array.new + @result = Result.new - filename = manifest["filename"] - expected_filename = "#{manifest["libraryname"]}.#{manifest["release_date"]}.manifest" - - print "Verify manifest #{filename}..." - - if filename != expected_filename - @errors.push "Expected file name: #{expected_filename}" + if !manifest["filename"] + @result.errors = "Unable to determine filename" + @result.name = "<unknown>" + else + @result.name = manifest["filename"] end - - if manifest["release_date"] == "1970-01-01" - @errors.push "Invalid release date: #{manifest["release_date"]}" + if !manifest["libraryname"] + @result.errors = "Unable to determine libraryname" end - - manifest.keys.each do |key| - if !@allowed_keys.include? key - @errors.push "Illegal entry: #{key}" + + if @result.errors.empty? + filename = manifest["filename"] + expected_filename = "#{manifest["libraryname"]}.#{manifest["release_date"]}.manifest" + + if filename != expected_filename + @result.errors.push "Expected file name: #{expected_filename}" end + + if manifest["release_date"] == "1970-01-01" + @result.errors.push "Invalid release date: #{manifest["release_date"]}" + end + + manifest.keys.each do |key| + if !@allowed_keys.include? key + @result.errors.push "Illegal entry: #{key}" + end + end + + @mandatory_keys.each do |key| + if !manifest.keys.include? key + @result.errors.push "Mandatory attribute is missing: #{key}" + end + end end - if @errors.empty? - puts "ok" - return true + if @result.errors.empty? + @result.valid = true + return @result else - puts "error" - @errors.each do |error| - puts " #{error}" - end - return false + @result.valid = false + return @result end end end