Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 119 lines (105 sloc) 3.79 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
require 'bundler'
Bundler::GemHelper.install_tasks

task :default => 'spec'

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec) do |t|
  t.rspec_opts = "-c -f d"
end
RSpec::Core::RakeTask.new(:rcov) do |t|
  t.rspec_opts = "-c -f d"
  t.rcov = true
  t.rcov_opts = ["--exclude", "spec,gems/,rubygems/"]
end

require 'yard'
YARD::Rake::YardocTask.new(:doc) do |t|
  version = Beerxml::VERSION
  t.options = ["--title", "beerxml #{version}", "--files", "LICENSE"]
end

desc "Parse the XML version of the BJCP style guide"
# available at http://www.bjcp.org/stylecenter.php
# This creates Beerxml::Style objects.
# These are then dumped as a beerxml-formatted <STYLES>...</STYLES> tag.
#
# If you just want to use the styles, there's no need to run this rake task
# since they're already included in the library.
# Just call Beerxml::Style.predefined(:bjcp)
#
# Important: BJCP does not currently allow hosting of the guidelines. This
# means that we can't import the tasting notes, descriptions, etc., into the
# app. I've spoken with BJCP to clarify exactly what this means, and it means
# we can import the category information and basic stats like gravities, IBUs,
# etc. But none of the descriptive text fields.
#
# PLEASE do not import the descriptive data into your applications without
# explicit permission from BJCP, they've worked hard on these guides and let's
# not abuse their trust.
task :parse_styleguide, :file_name do |t, args|
  file_name = args.file_name || "data/local/styleguide2008.xml"
  require 'open-uri'
  require 'beerxml'
  styles = []

  read_minmax = proc do |node, style, prefix|
    style.send("#{prefix}_min=", (node>'low').text)
    style.send("#{prefix}_max=", (node>'high').text)
  end

  xml = Nokogiri::XML(open(file_name))
  (xml.root>'class').each do |class_node|
    class_name = class_node['type']

    (class_node>'category').each do |category_node|
      category_name = (category_node>'name').first.text
      category_number = category_node['id']
      style_type = case class_name
      when 'beer'
        if category_name =~ /lager/i
          'Lager'
        else
          'Ale'
        end
      else
        class_name.capitalize
      end

      (category_node>'subcategory').each do |node|
        # the actual style records are here
        style = Beerxml::Style.new(:category => category_name,
                                   :category_number => category_number,
                                   :style_guide => 'BJCP')
        style.name = (node>'name').first.text
        style.style_letter = node['id'][1..-1]
        style.type = style_type
        read_minmax.call((node>'stats'>'og'), style, 'og')
        read_minmax.call((node>'stats'>'fg'), style, 'fg')
        read_minmax.call((node>'stats'>'ibu'), style, 'ibu')
        read_minmax.call((node>'stats'>'srm'), style, 'color')
        read_minmax.call((node>'stats'>'abv'), style, 'abv')

        styles << style
      end
    end
  end
  puts Beerxml::Style.to_beerxml_collection(styles).to_s
end

desc "Parse the HTML malt chart"
task :parse_malt_chart do |t|
  require 'open-uri'
  require 'beerxml'
  path = "http://www.homebrewtalk.com/wiki/index.php/Malts_Chart"
  fermentables = []

  root = Nokogiri::HTML(open(path)).root
  root.css("table:first tr").each do |row|
    tds = row.css("td")
    next if tds.size < 5
    f_name = tds[0].text.strip
    f_yield = tds[2].text.to_f
    f_srm = tds[3].text.to_f
    f_mash = tds[4].text.strip == "X"

    if f_yield > 0
      fermentables << Beerxml::Fermentable.new({
        :type => 'Grain',
        :name => f_name,
        :yield => f_yield,
        :color => f_srm,
        :recommend_mash => f_mash,
      })
    end
  end

  puts Beerxml::Fermentable.to_beerxml_collection(fermentables).to_s
end
Something went wrong with that request. Please try again.