class Google_search_cmdline::Interface
This class starts a interactive interface based on a ‘in’-IO and ‘out-IO. If none is given, then ’stdout’ and ‘stdin’ will be used.
Examples¶ ↑
interface = Google_search_cmdline::Interface.new(:gsc => gsc)
Constants
- ALLOWED_ARGUMENTS
Public Class Methods
new(args = {})
click to toggle source
Constructor.
# File lib/google_search_cmdline_interface.rb, line 8 def initialize(args = {}) @args = args @args.each do |key, val| raise "Invalid argument: '#{key}'." if !ALLOWED_ARGUMENTS.include?(key) end #Used to execute Google-searches. @gsc = @args[:gsc] raise "No ':gsc'-argument was given. This should be a 'Google_search_cmdline' or compatible." if !@gsc #Take IO's from arguments or std-IO's. if @args[:io_out] @io_out = @args[:io_out] else @io_out = $stdout end if @args[:io_in] @io_in = @args[:io_in] else @io_in = $stdin end #If not synced buffers will ruin the application. @io_out.sync = true @io_in.sync = true #Commands that should be called directly on the interface-object. direct_cmds = ["exit", "help"] @io_out.puts "Welcome to Google Search Cmdline." @io_in.each_line do |line_full| begin line = line_full[0, line_full.length - 1] puts "Got line: '#{line}'." if @args[:debug] if match = line.match(/^search\s+(.+)$/) @io_out.puts "Searching - please wait..." search_text = match[1] results = @gsc.search(search_text) self.cmd_search(results) elsif direct_cmds.include?(line) self.__send__("cmd_#{line}") elsif line.to_s.strip.empty? #ignore. else @io_out.puts "Can't understand that command: '#{line}'. Try 'help' to see available commands." end rescue => e @io_out.puts "An error occurred: '#{e.message}'." @io_out.puts "Backtrace:" @io_out.puts e.backtrace.join("\n") @io_out.puts "" end end end
Public Instance Methods
cmd_exit()
click to toggle source
Called when ‘exit’.
# File lib/google_search_cmdline_interface.rb, line 90 def cmd_exit @io_out.puts "Goodbye." exit end
cmd_help()
click to toggle source
Called when ‘help’. Shows a help-message explaining various available commands.
# File lib/google_search_cmdline_interface.rb, line 96 def cmd_help @io_out.puts "Available commands:" @io_out.puts " exit - Ends the application." @io_out.puts " help - Shows this message." @io_out.puts " search [text] - Executes a Google search and shots the results." @io_out.puts "" end
cmd_search(results)
click to toggle source
Called when ‘search [something]’. Shows the results of the search.
# File lib/google_search_cmdline_interface.rb, line 67 def cmd_search(results) count = 0 results.each do |result| count += 1 @io_out.puts "Result #{count} - #{result.title}" @io_out.puts "URL: #{result.url}" @io_out.puts " " if count >= 8 @io_out.puts "The maximum of 8 results was found. Please be more specific." break end end if count <= 0 @io_out.puts "No results was found (which is very very weird)." end @io_out.puts "" end