bin/shorturl in shorturl-0.8.3 vs bin/shorturl in shorturl-0.8.4

- old
+ new

@@ -1,52 +1,36 @@ #!/usr/bin/env ruby require "rubygems" require "shorturl" -require "optparse" -include WWW - -class Array - def pick - self[rand(size)] - end +def usage + puts "Usage: #$0 <url> [<service>]" + puts "Available services:" + ShortURL.valid_services.each { |s| puts "\t#{s}" } + exit(-1) 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)} [<options>] <url>" - opts.on("-h", "--help", "Displays help") { puts opts; exit } - opts.on("-s", "--service <service>", String, - "Uses <service> instead of RubyURL -Available services:\n\t" + service_list) { |s| service = s.to_sym } - opts.parse! + if ARGV.size < 1 + usage 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) + url = ARGV[0] + service = ARGV[1] + + shorturl = if service.nil? + ShortURL.shorten(url) else - puts "Invalid service" - exit(-1) + if ShortURL.valid_services.include?(service.to_sym) + ShortURL.shorten(url, service.to_sym) + else + puts "Invalid service" + exit(-1) + end end - puts shorturl || "Unable to get shortened URL. Maybe the URL is already too short?" + puts shorturl + end main