Class: LivingValidator::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/living-validator.rb

Overview

Validator class. Should be accesses statically

Class Method Summary collapse

Class Method Details

.check(url, options = {}) ⇒ LivingValidator::Result, boolean

Perform the validation on a given URL against the validator endpoint. Has two options ‘:endpoint` and `:lax_type`. The endpoint option configures where the validator instance is running. It defaults to validator.nu. The lax type option allows for lax type checking. The default for this is false. Make sure to test the result of this method for a false falue as this is the failure output.

Parameters:

  • string

    url The url of the page to validate

  • hash

    options (optional) The options to use.

Returns:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/living-validator.rb', line 34

def self.check(url, options = {})
	# Determine the endpoint url
	endpoint = options[:endpoint] || ENDPOINT
	# Determine if you should use the lax content type
	lax_type = options[:lax_type] || LAX_TYPE

	# Build the URL to query the validator
	req_url = 'http://' + endpoint + '/?out=json' + ((lax_type)? '&laxtype=yes':'') + '&doc=' + CGI::escape(url.to_s)

	# Query the validator
	response = false
	begin
		req_uri = URI.parse(req_url)
		response = HTTParty.get(req_url)
		response = JSON.parse(response.body)
	rescue 
		response = false
	end

	# Build a results object if the query succeeded
	unless response == false
		result = Result.new(url.to_s, response['messages'])
		response = result
	end

	# Return the results object
	response
end