DylanFM-pleasevalidate 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History.txt CHANGED
@@ -2,3 +2,10 @@
2
2
 
3
3
  * 1 major enhancement:
4
4
  * Initial release
5
+
6
+ == 0.0.2 2009-14-09
7
+
8
+ * 1 major enhancement:
9
+ * Multiple files validated
10
+ * 1 minor enhancement:
11
+ * Mime type checked to only allow html files to be validated
data/Manifest.txt CHANGED
@@ -1,15 +1,20 @@
1
1
  History.txt
2
2
  LICENSE
3
3
  Manifest.txt
4
+ PostInstall.txt
4
5
  README.rdoc
5
6
  RakeFile
6
7
  bin/pleasevalidate
7
8
  cucumber.yml
8
9
  features/cli_validation.feature
10
+ features/file_types.feature
9
11
  features/steps/cli_validation_steps.rb
12
+ features/steps/file_types_steps.rb
10
13
  features/steps/helper.rb
14
+ features/steps/validate_multiple_files_steps.rb
11
15
  features/steps/validate_steps.rb
12
16
  features/validate.feature
17
+ features/validate_multiple_files.feature
13
18
  lib/please_validate.rb
14
19
  lib/please_validate/cli.rb
15
20
  lib/please_validate/validator.rb
data/PostInstall.txt ADDED
@@ -0,0 +1,2 @@
1
+
2
+ See http://github.com/DylanFM/please_validate for a brief how-to.
data/README.rdoc CHANGED
@@ -43,10 +43,14 @@ A little markup validator.
43
43
 
44
44
  * nokogiri
45
45
  * colored
46
+ * mime/types
46
47
 
47
48
  == INSTALL:
48
49
 
49
- rake install_gem
50
+ Run the following if you haven't already:
51
+ gem sources -a http://gems.github.com
52
+ Install the gem:
53
+ sudo gem install DylanFM-pleasevalidate
50
54
 
51
55
  == LICENSE:
52
56
 
data/RakeFile CHANGED
@@ -10,7 +10,8 @@ $hoe = Hoe.new('pleasevalidate', PleaseValidate::VERSION) do |p|
10
10
  p.rubyforge_name = p.name
11
11
  p.extra_deps = [
12
12
  ['nokogiri'],
13
- ['colored']
13
+ ['colored'],
14
+ ['mime-types']
14
15
  ]
15
16
  p.extra_dev_deps = [
16
17
  ['newgem', ">= #{::Newgem::VERSION}"]
@@ -0,0 +1,16 @@
1
+ Feature: only HTML files are valid
2
+ In order to validate HTML files
3
+ As a developer
4
+ I want to make sure only HTML files are validated
5
+
6
+ Scenario: HTML file requessted
7
+ Given there is an HTML file
8
+ When the HTML file is validated
9
+ Then there should be no errors
10
+
11
+ Scenario: Bad file requested
12
+ Given there is a non-HTML file
13
+ When the non-HTML file is requested
14
+ Then there should be an error
15
+
16
+
@@ -0,0 +1,24 @@
1
+ Given /^there is an HTML file$/ do
2
+ Given "there is a valid XHTML file"
3
+ end
4
+
5
+ When /^the HTML file is validated$/ do
6
+ When "the file is validated"
7
+ end
8
+
9
+ Then /^there should be no errors$/ do
10
+ @result.should be_an_instance_of(Hash)
11
+ end
12
+
13
+ Given /^there is a non\-HTML file$/ do
14
+ @file = 'cucumber.yml'
15
+ File.exist?(@file).should be_true
16
+ end
17
+
18
+ When /^the non\-HTML file is requested$/ do
19
+ #Nothing to go here?
20
+ end
21
+
22
+ Then /^there should be an error$/ do
23
+ lambda { PleaseValidate::Validator.file(@file) }.should raise_error
24
+ end
@@ -0,0 +1,29 @@
1
+ Given /^there are 2 files for validation$/ do
2
+ @files = ['examples/valid.html','examples/invalid.html']
3
+ @files.each do |file|
4
+ File.exist?(file).should be_true
5
+ end
6
+ end
7
+
8
+ When /^they are both requested for validation$/ do
9
+ @results = PleaseValidate::Validator.files(@files)
10
+ end
11
+
12
+ Then /^the result should address both files$/ do
13
+ @results.should be_a_kind_of Array
14
+ @results.should have(2).things
15
+ @files.each do |file|
16
+ @results.any? { |r| r[:file] == file }.should be_true
17
+ end
18
+ end
19
+
20
+ When /^they are both requested for validation via cli$/ do
21
+ PleaseValidate::CLI.execute(@stdout_io = StringIO.new, @files)
22
+ @stdout_io.rewind
23
+ @stdout = @stdout_io.read
24
+ end
25
+
26
+ Then /^the result should mention both files$/ do
27
+ @stdout.should =~ /Valid: #{@file}/
28
+ @stdout.should =~ /Invalid: #{@file}/
29
+ end
@@ -0,0 +1,15 @@
1
+ Feature: Validates multiple files
2
+ In order to develop efficiently
3
+ As a developer
4
+ I want to validate multiple files at once
5
+
6
+ Scenario: validate 2 files
7
+ Given there are 2 files for validation
8
+ When they are both requested for validation
9
+ Then the result should address both files
10
+
11
+ Scenario: validate 2 files through cli
12
+ Given there are 2 files for validation
13
+ When they are both requested for validation via cli
14
+ Then the result should mention both files
15
+
@@ -2,9 +2,11 @@ require 'rubygems'
2
2
  require 'net/http'
3
3
  require 'cgi'
4
4
  require 'nokogiri'
5
+ require 'mime/types'
5
6
 
6
7
  Dir.glob(File.join(File.dirname(__FILE__), "please_validate", "*.rb")).each { |f| require f }
7
8
 
8
9
  module PleaseValidate
9
- VERSION = '0.0.1'
10
+ VERSION = '0.0.2'
11
+ ACCEPTED_MIMES = ['text/html']
10
12
  end
@@ -37,30 +37,33 @@ module PleaseValidate
37
37
 
38
38
  # Takes the requested file, passes it to validate for validation and displays the result with the display method
39
39
  def initialize(arguments)
40
- begin
41
- @file = arguments[0]
42
- @result = validate
43
- @msg = display
44
- rescue Exception => e
45
- @msg = "Validation failed"
46
- end
40
+ @files = arguments
41
+ @results = validate
42
+ @msg = display
47
43
  end
48
44
 
49
45
  # Calls the validator class's file method for the requested file
50
46
  def validate
51
- PleaseValidate::Validator.file(@file)
47
+ PleaseValidate::Validator.files(@files)
52
48
  end
53
49
 
54
50
  # Displays the file validation's results
55
51
  def display
56
- msg = "#{@result[:status].to_s.capitalize}: #{@file}".send(@result[:status] == :valid ? :on_green : :on_red)
57
- if @result[:status] == :invalid
58
- msg += "\n#{@result[:error_count]} error#{@result[:error_count] == 1 ? nil:'s'}:"
59
- @result[:errors].each do |error|
60
- msg += "\nLine #{error[:line]}, Column #{error[:col]}".red + ": #{error[:message]}"
52
+ @results.inject('') do |msg,result|
53
+ if result.is_a? Hash
54
+ msg += "#{result[:status].to_s.capitalize}: #{result[:file]}".send(result[:status] == :valid ? :on_green : :on_red)
55
+ if result[:status] == :invalid
56
+ msg += "\n#{result[:error_count]} error#{result[:error_count] == 1 ? nil:'s'}:"
57
+ result[:errors].each do |error|
58
+ msg += "\nLine #{error[:line]}, Column #{error[:col]}".red + ": #{error[:message]}"
59
+ end
60
+ end
61
+ elsif result.is_a? String
62
+ msg += result
61
63
  end
64
+ msg += "\n\n"
65
+ msg
62
66
  end
63
- msg
64
67
  end
65
68
 
66
69
  #Displays the message for the command line result
@@ -4,31 +4,40 @@ module PleaseValidate
4
4
 
5
5
  class << self
6
6
  # Read requested file's contents and send to the w3c validator api then call the parse_response method to sort the response out.
7
- def file(file_path)
7
+ def file(file)
8
8
  begin
9
- raise "Please specify a file to validate" unless file_path
10
- raise "The specified file doesn't exist" unless File.exist?(file_path)
11
- response = File.open(file_path, 'r') do |f|
9
+ raise "please specify a file to validate" unless file
10
+ raise "#{file} doesn't exist" unless File.exist? file
11
+ raise "#{file} must have a content type of text/html" unless file_valid? file
12
+ response = File.open(file, 'r') do |f|
12
13
  Net::HTTP.start('validator.w3.org').post(
13
14
  '/check',
14
15
  "fragment=#{CGI.escape(f.read)}&output=xml",
15
16
  {'Content-Type' => 'application/x-www-form-urlencoded'}
16
17
  )
17
18
  end
18
- parse_response response
19
- rescue Exception => e
20
- puts e
21
- raise
19
+ parse_response file, response
20
+ end
21
+ end
22
+
23
+ # Takes an array of files and validates them all
24
+ def files(files)
25
+ files.uniq.collect do |file|
26
+ begin
27
+ self.file(file)
28
+ rescue Exception => e
29
+ "Validation failed: #{e}"
30
+ end
22
31
  end
23
-
24
32
  end
25
33
 
26
34
  # Takes an XML response from the file method's call to the w3c and parses it into a nice little hash
27
- def parse_response(response)
35
+ def parse_response(filename, response)
28
36
  # Use Nokogiri to parse the xml
29
37
  response_data = Nokogiri::XML.parse(response.body)
30
38
  # Begin building the return hash
31
39
  result = {
40
+ :file => filename,
32
41
  :status => response['x-w3c-validator-status'].downcase.to_sym,
33
42
  :error_count => response['x-w3c-validator-errors'].to_i,
34
43
  :errors => Array.new
@@ -48,6 +57,12 @@ module PleaseValidate
48
57
  end
49
58
  result
50
59
  end
60
+
61
+ # Takes a file path and checks to see if it's mime type is OK. Currently using the first mime type returned by the mime-types gem.
62
+ def file_valid?(file)
63
+ mime = MIME::Types.type_for(file).first
64
+ !mime.nil? && ACCEPTED_MIMES.include?(mime.content_type)
65
+ end
51
66
  end
52
67
 
53
68
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: DylanFM-pleasevalidate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dylan Fogarty-MacDonald
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-10 00:00:00 -07:00
12
+ date: 2009-03-14 00:00:00 -07:00
13
13
  default_executable: pleasevalidate
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -32,6 +32,16 @@ dependencies:
32
32
  - !ruby/object:Gem::Version
33
33
  version: "0"
34
34
  version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: mime-types
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
35
45
  - !ruby/object:Gem::Dependency
36
46
  name: newgem
37
47
  type: :development
@@ -62,20 +72,26 @@ extensions: []
62
72
  extra_rdoc_files:
63
73
  - History.txt
64
74
  - Manifest.txt
75
+ - PostInstall.txt
65
76
  - README.rdoc
66
77
  files:
67
78
  - History.txt
68
79
  - LICENSE
69
80
  - Manifest.txt
81
+ - PostInstall.txt
70
82
  - README.rdoc
71
83
  - RakeFile
72
84
  - bin/pleasevalidate
73
85
  - cucumber.yml
74
86
  - features/cli_validation.feature
87
+ - features/file_types.feature
75
88
  - features/steps/cli_validation_steps.rb
89
+ - features/steps/file_types_steps.rb
76
90
  - features/steps/helper.rb
91
+ - features/steps/validate_multiple_files_steps.rb
77
92
  - features/steps/validate_steps.rb
78
93
  - features/validate.feature
94
+ - features/validate_multiple_files.feature
79
95
  - lib/please_validate.rb
80
96
  - lib/please_validate/cli.rb
81
97
  - lib/please_validate/validator.rb