#!/usr/bin/env ruby require "rubygems" require "shorturl" require "optparse" include WWW class Array def pick self[rand(size)] end end def random_service ShortURL.valid_services.pick end def service_list ShortURL.valid_services.map { |s| s.to_s }.sort.join("\n\t") end def main service = :rubyurl op = OptionParser.new do |opts| opts.banner = "Usage: #{File.basename($0)} [] " opts.on("-h", "--help", "Displays help") { puts opts; exit } opts.on("-s", "--service ", String, "Uses instead of RubyURL Available services:\n\t" + service_list) { |s| service = s.to_sym } opts.parse! end if ARGV.empty? puts op exit(-1) end url = ARGV.shift shorturl = if service == :random ShortURL.shorten(url, random_service) elsif ShortURL.valid_services.include?(service) ShortURL.shorten(url, service) else puts "Invalid service" exit(-1) end puts shorturl || "Unable to get shortened URL. Maybe the URL is already too short?" end main