Last active
January 18, 2016 12:35
-
-
Save MatteoRagni/04491e868f300164904e to your computer and use it in GitHub Desktop.
A simple command line thesaurus dictionary. Thesaurus service provided by [words.bighugelabs.com]
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 | |
# Simple thesaurus script | |
# Thesaurus service provided by [words.bighugelabs.com] | |
require 'net/http' | |
require 'getoptlong' | |
require 'colorize' | |
require 'yaml' | |
require 'json' | |
require 'pry-byebug' | |
class Thesaurus | |
def initialize(conf) | |
conf_loading conf | |
end | |
def console | |
banner | |
puts "press CTRL+C to close and exit".yellow | |
continue = true | |
while continue do | |
trap "SIGINT" do continue = false; end | |
print " >>> ".blue | |
words = gets.chomp.split %r{;,\s*} | |
words.each do |word| | |
request word | |
end | |
end | |
puts "Info: GOODBYE!".yellow | |
end | |
def sigle_request(input) | |
banner | |
words = input.chomp.split %r{;,} | |
words.each do |word| | |
request word | |
end | |
end | |
# [ '--help', '-h', GetOptLong::NO_ARGUMENT ], | |
# [ '--conf', '-c', GetOptLong::REQUIRED_ARGUMENT ], | |
# [ '--word', '-w', GetOptLong::REQUIRED_ARGUMENT ], | |
# [ '--api', '-a', GetOptLong::REQUIRED_ARGUMENT ] | |
def self.help | |
banner | |
puts <<-EOS | |
-h, --help | |
Shows this help | |
-c, --conf [file path] | |
Request to use a different configuration file. If not specified, configuration | |
loaded is the one saved in: #{ENV["HOME"]}/.config/thesaurus.yaml | |
-w, --word ["word list"] | |
Execute search for word list, do not launch console. Word list is a comma separated list | |
-a, --api [api string] | |
Forces to use a different api string | |
EOS | |
end | |
def reload_conf(f) | |
conf_loading(f) | |
end | |
def force_token(s) | |
raise ArgumentError, "token must be a String" unless s.is_a? String | |
raise ArgumentError, "token must be at least 30 chars long" unless s.size > 30 # TODO | |
@defaults[:api] = s | |
end | |
def self.banner | |
puts <<-EOS | |
_____ _ | |
|_ _| |_ ___ ___ __ _ _ _ _ _ _ _ ___ | |
| | | ' \\/ -_|_-</ _` | || | '_| || (_-< | |
|_| |_||_\\___/__/\\__,_|\\_,_|_| \\_,_/__/ | |
Thesaurus service provided by [http://words.bighugelabs.com] | |
EOS | |
end | |
def banner | |
Thesaurus.banner | |
end | |
private | |
def conf_loading(f) | |
raise ArgumentError, "conf must be a string" unless f.is_a? String | |
raise ArgumentError, "conf file #{f} does not exists" unless File.exist? f | |
@defaults = YAML.load_file(f) | |
raise RuntimeError, "configuration file does not contains a valid api key" unless @defaults[:api] | |
raise RuntimeError, "configuration file does not contains a valid api key" unless @defaults[:api].is_a? String | |
raise RuntimeError, "configuration file does not contains a valid api key" unless @defaults[:api].size > 30 # TODO: Check real size (33?) | |
raise RuntimeError, "configuration file does not contains a valid api version" unless @defaults[:version] | |
@defaults[:version] = @defaults[:version].to_s | |
raise RuntimeError, "configuration file does not contains a valid api version" unless @defaults[:version] == "2" # TODO: as for now only version 2 is supported | |
raise RuntimeError, "configuration file does not contains a valid api method" unless @defaults[:method] | |
raise RuntimeError, "configuration file does not contains a valid api method" unless @defaults[:method].is_a? String | |
raise RuntimeError, "configuration file does not contains a valid api method" unless @defaults[:method] == "json" # TODO: as for now only json is supported | |
end | |
def generate_request(word) | |
"http://words.bighugelabs.com/api/#{@defaults[:version]}/#{@defaults[:api]}/#{word}/json" | |
end | |
def print_results(ary) | |
raise ArgumentError, "input is not an array" unless ary.is_a? Array | |
index = 0 | |
size = ary.size | |
ret = "" | |
ary.each_with_index do |e, i| | |
ret << " " * 6 if index == 0 | |
index += 1 | |
ret << e | |
ret << ", " if i != size - 1 | |
ret << "\n" if index == 6 | |
index = 0 if index == 6 | |
end | |
return ret.white | |
end | |
def request(word) | |
puts "Info: Searching for word " + word.to_s.white | |
uri = URI(generate_request word) | |
response = Net::HTTP.get_response uri | |
if response.code == "200" | |
object = JSON.parse response.body | |
object.keys.each do |k| | |
puts "-> #{k}".light_blue | |
object[k].keys.each do |j| | |
puts " -> #{j}: ".light_blue | |
print print_results object[k][j] | |
puts | |
end | |
end | |
else | |
# handling error code | |
case response.code | |
when "303" | |
puts "Warning: Word not found. Where you searching for ".yellow + response.body.chomp.white + "?".yellow | |
when "404" | |
puts "Error: Cannot perform request! (Server code #{response.code})".red | |
when "500" | |
if response.message.chomp == "Usage Exceeded" | |
puts "Error: Cannot perform request! You have Exceeded your search limit".red | |
elsif response.message.chomp == "Inactive key" | |
puts "Error: Cannot perform request! Your key is inactive!".red | |
end | |
end | |
end | |
end | |
end | |
if __FILE__ == $0 then | |
opts = GetoptLong.new( | |
[ '--help', '-h', GetoptLong::NO_ARGUMENT ], | |
[ '--conf', '-c', GetoptLong::REQUIRED_ARGUMENT ], | |
[ '--word', '-w', GetoptLong::REQUIRED_ARGUMENT ], | |
[ '--api', '-a', GetoptLong::REQUIRED_ARGUMENT ] | |
) | |
defaults = "#{ENV["HOME"]}/.config/thesaurus.yaml" | |
words = nil | |
override_api = nil | |
opts.each do |opt, arg| | |
case opt | |
when "--help" | |
Thesaurus.help | |
exit 0 | |
when "--conf" | |
defaults = arg | |
when "--word" | |
words = arg | |
when "--api" | |
override_api = arg | |
end | |
end | |
if (not File.exists? defaults) and override_api | |
puts "First run! Do you want to install a new configuration file in #{defaults}? [y/N]: ".yellow | |
re = gets.chomp.upcase | |
if %|Y YES S SI OUI|.include? re | |
config = { api: override_api, version: 2, method: "json" } | |
File.open(defaults, "w") { |f| f.puts config.to_yaml } | |
else | |
puts "File not saved. Exiting now!".yellow | |
exit 0 | |
end | |
end | |
th = Thesaurus.new defaults | |
if override_api | |
th.force_token override_api | |
end | |
if words | |
th.single_request words | |
else | |
th.console | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment