-
-
Save havenwood/8f5433c21b65e798d6b9 to your computer and use it in GitHub Desktop.
Example Ruby Script to Rank Eggs by Quality of Chicken-life
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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