Sha256: 23d0b116dfec20d4659db83a2f4d64ab12f490c9a28e11a22acf4294c3297d29

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

require "thor"
require "punky_brewster"
require "ostruct"
require "nokogiri"
require "open-uri"

module PunkyBrewster
  class CLI < Thor
    map "--version" => :version

    desc "version", "Show version"
    def version
      say "Punky Brewster version #{VERSION}"
    end

    desc "list", "Show current beer list"
    def list
      response = open("http://www.punkybrewster.co.nz")
      document = Nokogiri::HTML(response)
      content = document.at_css('#content')
      divide = content.at_css('.styled-hr')
      past_divide = false
      beers = []

      content.traverse do |node|
        past_divide = true if node == divide
        next unless past_divide

        if node.name == 'h2'
          beers << OpenStruct.new
          beers.last.name = node.text.upcase.gsub(/\s+/, ' ')
        elsif price = node.text.scan(/^\$(\d+\.\d+)\/L$/).flatten.first
          beers.last.price = price.to_f
        elsif abv = node.text.scan(/(\d+\.\d+)\s*%/).flatten.first
          beers.last.abv = abv.to_f
        end
      end

      rows = beers.sort_by(&:name).map do |beer|
        [beer.name, "$%0.2f/L" % beer.price, "%0.1f%" % beer.abv]
      end

      print_table rows
    end

    default_task :list
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
punky_brewster-1.0.0 lib/punky_brewster/cli.rb