Skip to content

ddidier/ndd-url_checker

Repository files navigation

NDD URL Checker

Build Status Dependency Status Code Climate Test Coverage Documentation Coverage Yard Documentation

An URL validator.

The API documentation can be find at RubyDoc.

Prerequisites

This gem requires Ruby 2.x and is tested against some versions.

Usage

This gem provides several types of URL checker which may be composed using the decorator pattern. An URL checker exposes a #check(*urls) method which has 2 variants:

A status has a code reflecting the result of the URL check. For the time being:

  • valid codes are direct and redirected
  • invalid codes are failed, too_many_redirects and unknown_host

### BlockingUrlChecker

NDD::UrlChecker::BlockingUrlChecker provides a serial URL checker using the standard Net:HTTP implementation.

checker = NDD::UrlChecker::BlockingUrlChecker.new

status = checker.check('http://www.invalid123456789.com/')
status.valid? # => false
status.code   # => :unknown_host

status = checker.check('http://www.google.com/')
status.valid? # => true
status.code   # => :direct

statuses = checker.check('http://www.invalid123456789.com/', 'http://www.google.com/')
statuses[0].uri    # => 'http://www.invalid123456789.com/'
statuses[0].valid? # => false
statuses[0].code   # => :unknown_host
statuses[1].uri    # => 'http://www.google.com/'
statuses[1].valid? # => true
statuses[1].code   # => :direct

ParallelUrlChecker

But this will be very time consuming if there is a lot of URL to check. Meet NDD::UrlChecker::ParallelUrlChecker which enables a very significant processing boost. For the time being, only a forked implementation is provided but a threaded one is planed.

checker = NDD::UrlChecker::ParallelUrlChecker.new(parallelism: 100)
checker.check('http://www.invalid123456789.com/')
checker.check('http://www.google.com/')

### ReportingUrlChecker

For a nice looking report, use NDD::UrlChecker::ReportingUrlChecker which enables reporting capabilities using ERB templates. Several built-in templates are provided: CSV, HTML and JSON.

checker = NDD::UrlChecker:: ReportingUrlChecker.new(delegate_checker)
statuses = checker.check('http://www.invalid123456789.com/', 'http://www.google.com/')
report_as_text = checker.report(:csv, '/some/report.csv')
report_as_text = checker.report(:html, '/some/report.html')
report_as_text = checker.report(:json, '/some/report.json')
report_as_text = checker.report('/some/template.erb', '/some/report.html')

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/ddidier/ndd-url_checker.

License

The gem is available as open source under the terms of the MIT License.