NRB::Beerxml – A BeerXML parser
Install
Add this line to your application's Gemfile:
gem 'nrb-beerxml'
And then execute:
% bundle
Or install it yourself as:
% gem install nrb-beerxml
Parse
require `nrb/beerxml`
parser = NRB::BeerXML::Parser.new
=> #<NRB::BeerXML::Parser:0x007fbe4fb58d70 @builder=#<NRB::BeerXML::Builder:0x007fbe4fb58a00>, @reader=Nokogiri::XML>
parser.parse file_path
=> #<NRB::BeerXML::Equipment:0x007fbe501e4298 @name="Brew Pot", ...>
parse
can handle a String
pathname or an IO
containing the BeerXML.
Builder
The parser uses a builder class to construct NRB::BeerXML::Record
objects. If you would rather get back some other class of object (for example a ActiveRecord
model) just give it your own builder class at initialization.
parser = NRB::BeerXML::Parser.new buider: MyBuilder
Your builder should implement a build(type)
method which returns a new object of the requested type. type
will be an underscored string.
Reader
NRB::BeerXML uses Nokogiri by default. If you want to use another reader, simply pass the class name to the Parser's initializer.
parser = NRB::BeerXML::Parser.new reader: SomeXMLReader
The reader should implement a parse(stream)
method which returns a document. The document object should implement #children
, #name
, #root
, #text
& #text?
Bonus
If BeerXML
is not in your namespace before require 'nrb/beerxml'
you can leave off the NRB
prefix when referencing classes.
defined?(BeerXML) || BeerXML = NRB::BeerXML
Contribute
- Fork me
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some beers'
) - Push to the branch (
git push origin my-new-beer
) - Create a Pull Request