From: James Britt Date: 2004-08-17T05:40:43+09:00 Subject: Re: Ruby as a first language Gawnsoft wrote: >>No, seriously, I've written a number of simple scripts to fetch web >>pages. Usually they just construct a URL based on some command-line >>input, then make a system call to launch a browser. I search Google >>this way, as well as look for stuff on buy.com, check TV listings, read >>the news, grep/navigate my local wiki, a few other things. > > > Could I see them please? > Here's a simple case: google # file g.rb $: << File.dirname( __FILE__ ) require 'browser' $url = 'http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=' if ARGV.size < 1 puts "You need to enter at least one search word." else $url << ARGV.join("+") $stderr.puts "Calling #{$url}" Browser.new( 'FB' ).get( $url ) end # end of file Next is the code for browser.rb. It's meant to run on Windows. I've never bothered to try it on Linux; executable paths would have to change, as well as the calling convention (e.g., 'call someApp.exe'), but maybe not much else. (Actually, I think the big issue for me is that when I launch Mozilla or Firefox , and there is already an instance running, it stupidly insists I select another profile, or create a new one, or I can't use the new instance. That doesn't happen on Windows.) I believe I use a thread to launch the browser so that the script ends and I get my command prompt back; I run my browser scripts from the command line, like so: C:\> g some search words Windows handles the file extension stuff automagically, as I've added the script directory to my PATH variable. But, without using a thread, the script would just wait until I closed the browser, which is annoying. Anyway, once I put together the Browser class I needn't think about it when I write other scripts for pulling up web sites. (And 'less thinking' is one of the goals of coding. :) ) # browser.rb class Browser DEFAULT = 'FB' CB = 'CB' IE = 'IE' MOZ = 'MOZ' OP = 'OP' FB = 'FB' @@exe = Hash.new( 'explorer') @@exe['IE'] = 'explorer' @@exe['CB'] = '"E:/Program Files/Crazy Browser/Crazy Browser.exe"' @@exe['OP'] = 'e:/Program Files/Opera7/opera.exe' @@exe['MOZ'] = 'e:/Program Files/mozilla.org/Mozilla/mozilla.exe' @@exe['FB'] = 'E:/MozillaFirebird/MozillaFirebird.exe ' def initialize( browser_name = Browser::DEFAULT ) @broswser = browser_name @b_exe = select_exe( browser_name ) $stderr.puts( "Using broswer #@broswser ") end def get( url ) $stderr.puts( "get is using #@b_exe" ) Thread.new( url ){ |t| `call "#{@b_exe}" "#{url}"` } end def select_exe( bname ) @@exe[ bname ] end def self.get( url, browser_name = ( @broswser ? @broswser : Browser::DEFAULT ) ) exe = Browser.new(browser_name).select_exe( browser_name ) Thread.new( url ){ |t| `start "#{exe}" "#{url}"` } end end # End of browser.rb Lookng at the code now, there's cruft and goofiness, but it works so I haven't bothered to clean stuff up. But for a newcomer, it might be worthwhile to try writing a class that allows the user to 1) optionally select a particular browser to use (but have a default browser defined), 2) pass in the URL to load, 3) ensures that the script can run and exit without having to wait until the browser closes. Here's another script; this one calls Yahoo maps, like this: c:\> map 11123 Main Street, Scottsdale, AZ 85251 # map.rb $: << File.dirname( __FILE__ ) require 'browser' $base_url = 'http://maps.yahoo.com/py/maps.py?Pyt=Tmap' def argstr_to_qstring( str ) parts = str.split( ',', 2) street = parts[0].strip.gsub( / /, '+') state = parts[1].strip.gsub( / /, '+') "&addr=#{street}&csz=#{state}" end def args_to_mapurl( args ) a = args.join(' ') "#{$base_url}#{argstr_to_qstring( a )}&Get%A0Map=Get+Map" end if ARGV.size < 1 puts "You need to enter an address." else url = args_to_mapurl( ARGV ) Browser.get( url , Browser::MOZ ) end # end of map.rb This one is a bit gnarlier. But, once someone has written a general way to launch a Web browser, a next step might be to explore manipulating the input to construct more complex URLs. The goal is to make it easy to type at the command line, and let the code do the busy work. James