Brauhaus BeerXML Calculator Example (Coffeescript)

An example showing how to use Brauhaus to calculate OG, FG, IBU, and ABV from BeerXML input.

HTML

<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css">
<script src="https://rawgithub.com/homebrewing/brauhausjs/master/dist/brauhaus.min.js"></script>
<script src="https://rawgithub.com/homebrewing/brauhaus-beerxml/master/dist/brauhaus-beerxml.min.js"></script>
<textarea id="beerxml" placeholder="BeerXML goes here...">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;&lt;RECIPES&gt;&lt;RECIPE&gt;&lt;VERSION&gt;1&lt;/VERSION&gt;&lt;NAME&gt;Aramis Saison&lt;/NAME&gt;&lt;TYPE&gt;Extract&lt;/TYPE&gt;&lt;STYLE&gt;&lt;/STYLE&gt;&lt;BREWER&gt;danielgtaylor&lt;/BREWER&gt;&lt;BATCH_SIZE&gt;18.92705&lt;/BATCH_SIZE&gt;&lt;BOIL_SIZE&gt;11.35623&lt;/BOIL_SIZE&gt;&lt;EFFICIENCY&gt;75&lt;/EFFICIENCY&gt;&lt;HOPS&gt;&lt;HOP&gt;&lt;VERSION&gt;1&lt;/VERSION&gt;&lt;NAME&gt;Aramis hops&lt;/NAME&gt;&lt;ALPHA&gt;8.1&lt;/ALPHA&gt;&lt;AMOUNT&gt;0.0212615166549&lt;/AMOUNT&gt;&lt;AMOUNT_IS_WEIGHT&gt;TRUE&lt;/AMOUNT_IS_WEIGHT&gt;&lt;USE&gt;Boil&lt;/USE&gt;&lt;TIME&gt;60&lt;/TIME&gt;&lt;FORM&gt;Pellet&lt;/FORM&gt;&lt;/HOP&gt;&lt;HOP&gt;&lt;VERSION&gt;1&lt;/VERSION&gt;&lt;NAME&gt;Aramis hops&lt;/NAME&gt;&lt;ALPHA&gt;8.1&lt;/ALPHA&gt;&lt;AMOUNT&gt;0.00708717221828&lt;/AMOUNT&gt;&lt;AMOUNT_IS_WEIGHT&gt;TRUE&lt;/AMOUNT_IS_WEIGHT&gt;&lt;USE&gt;Boil&lt;/USE&gt;&lt;TIME&gt;5&lt;/TIME&gt;&lt;FORM&gt;Pellet&lt;/FORM&gt;&lt;/HOP&gt;&lt;/HOPS&gt;&lt;FERMENTABLES&gt;&lt;FERMENTABLE&gt;&lt;VERSION&gt;1&lt;/VERSION&gt;&lt;NAME&gt;Munich liquid extract&lt;/NAME&gt;&lt;AMOUNT&gt;2.26795&lt;/AMOUNT&gt;&lt;YIELD&gt;75.7346258709&lt;/YIELD&gt;&lt;COLOR&gt;10&lt;/COLOR&gt;&lt;ADD_AFTER_BOIL&gt;FALSE&lt;/ADD_AFTER_BOIL&gt;&lt;/FERMENTABLE&gt;&lt;FERMENTABLE&gt;&lt;VERSION&gt;1&lt;/VERSION&gt;&lt;NAME&gt;Wheat liquid...

CSS

textarea {
    width: 98%;
    height: 200px;
    margin-top: 1em;
}

th {
    display: inline-block;
    text-align: left;
    margin-right: 0.5em;
}

CoffeeScript

# Create and calculate recipe when button is clicked
$('#calc').click ->
    # Get a list of recipes from the BeerXML
    beerxml = $('#beerxml').val()
    recipes = Brauhaus.Recipe.fromBeerXml(beerxml)
    
    # We only care about the first recipe...
    # Let's calculate and display some values!
    recipe = recipes[0]
    recipe.calculate()
    
    # Display calculated recipe stats
    $('#og').html "#{recipe.og.toFixed 3} (#{recipe.ogPlato.toFixed 1}&deg; Plato)"
    $('#fg').html "#{recipe.fg.toFixed 3} (#{recipe.fgPlato.toFixed 1}&deg; Plato)"
    $('#ibu').html "#{recipe.ibu.toFixed 1}"
    $('#color').html "#{Math.round recipe.color}&deg; SRM (#{Math.round Brauhaus.srmToEbc(recipe.color)}&deg; EBC)"
    $('#abv').html "#{recipe.abv.toFixed 2}%"