class Urban::CLI

Attributes

dictionary[RW]

Public Class Methods

new() click to toggle source
# File lib/urban/cli.rb, line 10
def initialize
  @dictionary = Urban::Dictionary.new
end

Public Instance Methods

run(args = ARGV) click to toggle source
# File lib/urban/cli.rb, line 14
def run(args = ARGV)

  options = parse(args)
  results = lookup(options)

  case
  when results.definitions
    generate_output results, options
  when results.phrase
    error "no definitions found for #{results.phrase.upcase}."
  else
    $stdout.puts options.version ? version : usage
  end

rescue SocketError
  error "no internet connection available."
rescue OptionParser::InvalidOption => e
  error "#{e.message}\nTry `urban --help' for more information."
rescue Object => e
  error e.message
end

Private Instance Methods

error(message) click to toggle source
# File lib/urban/cli.rb, line 38
def error(message)
  $stderr.puts "urban: #{message}"
end
generate_output(entry, options) click to toggle source
# File lib/urban/cli.rb, line 42
def generate_output(entry, options)
  output = "\n#{entry.phrase.upcase}\n\n"
  if options.all
    output << "#{entry.definitions.join("\n\n")}\n\n"
  else
    output << "#{entry.definitions.first}\n\n"
  end
  output << "URL: #{entry.url}\n\n" if options.url

  $stdout.puts output
end
lookup(options) click to toggle source
# File lib/urban/cli.rb, line 71
def lookup(options)
  case
  when options.random then dictionary.random
  when options.search then dictionary.search(options.phrase)
  else OpenStruct.new
  end
end
parse(args) click to toggle source
# File lib/urban/cli.rb, line 54
def parse(args)
  options = OpenStruct.new

  options_parser = OptionParser.new do |o|
    o.on("-a", "--all") { options.all = true }
    o.on("-r", "--random") { options.random = true }
    o.on("-u", "--url") { options.url = true }
    o.on("-h", "--help")
    o.on("-v", "--version") { options.version = true }
  end

  options_parser.parse!(args)
  options.phrase = args.join(" ")
  options.search = !options.phrase.empty?
  options
end
usage() click to toggle source
# File lib/urban/cli.rb, line 83
    def usage
      <<-EOS
Usage: urban [OPTION]... [PHRASE]
Search http://urbandictionary.com for definitions of phrases

Options:
    -a, --all                   List all definitions
    -r, --random                Return a random phrase and definition
    -u, --url                   Print the definition's url after the definition
    -h, --help                  Show this message
    -v, --version               Show version information

Examples:
    urban cookie monster        Search for "cookie monster" and print its
                                first definition
    urban -a cookie monster     Search for "cookie monster" and print all of
                                its available definitions
    urban -r                    Print a random phrase and its first definition
    urban -ra                   Print a random phrase and all of its available
                                definitions

      EOS
    end
version() click to toggle source
# File lib/urban/cli.rb, line 79
def version
  "Urban #{Urban::VERSION} (c) Thomas Miller"
end