Skip to content

Instantly share code, notes, and snippets.

Created May 15, 2012 18:50
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Ruck beer list scraper
#!/usr/bin/env ruby
# A Thor-based commandline tool for all BeerMenu.
# Based on @jsteiner's Ruby equivalent of @jlet's Python Ruck
# beer list scraper
# @jsteiner's Original:
# @jlet's Original: ttps://
require 'open-uri'
require 'nokogiri'
require 'thor'
class BeerMenu < Thor
ATTRIBUTES = %w(name ounces serving abv price efficiency)
desc 'list [PLACE]', 'list of beer info for PLACE'
def list place
doc = Nokogiri::HTML open "{place}"
beers = []
order = ATTRIBUTES.include?(ARGV[1]) ? ARGV[1] : 'name'
doc.css('table.beermenu tbody tr').each do |tr|
beer = {}
serving_info = /(\d+)\s*oz\.?\s+(\w+)/.match(tr.css(".serving").text)
beer[:name] = tr.css("td:first-child a").text
beer[:ounces], beer[:serving] = serving_info[1].to_i, serving_info[2]
beer[:abv] = (tr.css('.abv').text + '0').to_f
beer[:price] = (tr.css('.price').text.sub('$', '')).to_f
beer[:efficiency] = (beer[:ounces] * beer[:abv]) / beer[:price]
beers << beer
beers.sort_by { |beer| beer[order.to_sym] }.each do |beer|
puts "%-45s %2d-oz %-6s %4.1f%% ABV, $%-5.2f (Eff'cy: %5.2f)" %
beer[:name], beer[:ounces], beer[:serving],
beer[:abv], beer[:price], beer[:efficiency]
desc 'search LOCATION', 'search for location on and return ID'
def search place
doc = Nokogiri::HTML open "{URI.escape(place)}"
locations = []
doc.css('#search a').each do |a|
id = a.attr('href').match /^\/places\/(.+)/
next unless id
locations << {
name: a.inner_text,
id: id[1]
puts 'Search Results:'
locations.each do |place|
puts "#{place[:id]} - #{place[:name]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment