Skip to content

Instantly share code, notes, and snippets.

@modprobe
Created November 8, 2015 15:24
Show Gist options
  • Save modprobe/5094d14793812ba7e920 to your computer and use it in GitHub Desktop.
Save modprobe/5094d14793812ba7e920 to your computer and use it in GitHub Desktop.
Shell wrapper for the urbandictionary API
#!/usr/bin/env ruby
## Author: Alex Timmermann <modprobe@linux.com>
## CC-0 (Public Domain)
require 'json'
require 'open-uri'
if ARGV.length < 1 or ARGV.length > 2
print "Wrong number of parameters. You dumbass.\n"
exit
end
s = ARGV[0]
s_rep = s.gsub " ", "+"
BASE_URL = "http://api.urbandictionary.com/v0/define?term="
response = open(BASE_URL + s_rep).read
results = JSON.parse(response, symbolize_names: true)
num_of_results = 1
if ARGV.length == 2
num_of_results = ARGV[1].to_i
end
unless results[:list].empty?
1.upto num_of_results do |i|
word = results[:list][i-1][:word]
print "#{word}\n"
print "=" * word.length + "\n\n"
definition = results[:list][i-1][:definition]
print "#{definition}\n\n"
print "-" * `tput cols`.to_i + "\n\n" unless i == num_of_results
end
else
print "No results.\n"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment