Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created June 16, 2015 22:58
Show Gist options
  • Save havenwood/8f5433c21b65e798d6b9 to your computer and use it in GitHub Desktop.
Save havenwood/8f5433c21b65e798d6b9 to your computer and use it in GitHub Desktop.
Example Ruby Script to Rank Eggs by Quality of Chicken-life
#!/usr/bin/env ruby
require 'colorize'
require 'oga'
require 'open-uri'
response = open 'http://www.cornucopia.org/organic-egg-scorecard', &:read
html = Oga.parse_html response.force_encoding 'utf-8'
text = html.at_css('#organic-egg-scorecard > tbody').text
scores = text.scan(/\S[[:print:]]*/).each_cons(3).select do |_, by, digit|
by =~ /\Aby/ && digit =~ /\d+/
end.map do |name, _, digit|
rating = case digit
when '5', '4'
:green
when '3'
:yellow
when '2', '1'
:red
end
[name, rating]
end.to_h
scores.each do |k, v|
puts k.colorize v
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment