Repository is archived
No commit activity in last 3 years
No release in over 3 years
Adds XML finders to Nokogiri that raise if nothing is found.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.5
>= 0
~> 3.2

Runtime

>= 1.0
 Project Readme

nokogiri_bang_finders

This gem says "Nokogiri, if you can't find the XML I want, yell about it."

For example:

doc = Nokogiri::XML("<root><aliens><alien><name>Alf</name></alien></aliens></root>")
doc.at('alien').content # => "Alf"

# without nokogiri_bang_finders
doc.at('robot').content # NoMethodError: undefined method `content' for nil:nilclass

# with nokogiri_bang_finders
doc.at!('robot').content # Nokogiri::XML::NotFound: '["robot"] in {snippet of the XML}'

Specifically, this gem adds the following methods to to Nokogiri::XML::Node and Nokogiri::XML::NodeSet:

  • at!
  • at_xpath!
  • at_css!

Each method just calls its non-bang namesake and, if the result is nil, raises Nokogiri::XML::NotFound.

This gem is so tiny, you could just copy and paste its code. But if it's convenient, use it. 😄

Context

When an exception is raised, its message includes some context from the document, to show "I was looking for your selector in a document that looks like this". By default, up to 200 characters of context are given.

You can specify an integer number of characters of context, like Nokogiri::XML::BangFinders.context_length = 500.

Or you can specify :all to get the whole document in the exception message (though this probably isn't a good idea for production code): Nokogiri::XML::BangFinders.context_length = :all

Installation

$ gem install nokogiri_bang_finders

... or via Bundler.

Requiring

Be sure to require 'nokogiri_bang_finders' after you require 'nokogiri'. It expects Nokogiri::XML::Node and Nokogiri::XML::NodeSet to be defined so that it can add methods to them.

Usage

Use these methods just like you would use the normal Nokogiri versions, but expect an exception if the XML doesn't contain what you're looking for.