Skip to content

Instantly share code, notes, and snippets.

@brandoncc
Last active April 5, 2016 21:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brandoncc/b3c3146fede08e596370 to your computer and use it in GitHub Desktop.
Save brandoncc/b3c3146fede08e596370 to your computer and use it in GitHub Desktop.
require "net/http"
require "nokogiri"
unless [1, 2].include? ARGV.length
puts "Usage: TERM [max number of results to display]"
exit
end
def max_results
max = ARGV[1].to_i
max > 1 ? max : 5
end
def get_term(row)
row.css(".desc").inner_text
end
def get_filled_stars(row)
row.css("#abbr-rate .sf").count
end
def get_total_stars(row)
row.css("#abbr-rate .se").count + get_filled_stars(row)
end
def uniquify_results!(results)
results.uniq! do |result|
result[0]
end
end
def trim_results!(results)
results.reject! { |result| result[2].to_i == 0 }
results.pop until results.count <= max_results
end
def display_results(results)
if results.count > 0
results.each do |result|
puts "#{result[0]} (#{result[1]} / #{result[2]})"
end
else
puts "No acronyms found"
end
end
uri = URI("http://www.abbreviations.com/#{ARGV[0]}")
doc = Nokogiri::HTML(Net::HTTP.get(uri))
data = doc.css("table.tdata tr")
results = []
data.each do |row|
term = get_term(row)
stars = get_filled_stars(row)
highest_rating = get_total_stars(row)
results << [term, stars, highest_rating]
end
uniquify_results!(results)
trim_results!(results)
display_results(results)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment