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. :smile:
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.