# CGI and Methods?

**URL:** <https://rubytalk.org/t/cgi-and-methods/45246>\
**Category:** ruby-talk\
**Created:** [17 March 2008 23:26 UTC](https://rubytalk.org/t/cgi-and-methods/45246 "2008-03-17T23:26:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tony\_De1](https://avatars.discourse-cdn.com/v4/letter/t/f14d63/32.png) [@Tony\_De1](https://rubytalk.org/u/Tony_De1)\
**Post date:** [17 March 2008 23:26 UTC](https://rubytalk.org/t/cgi-and-methods/45246/1 "2008-03-17T23:26:14Z")

</div>

Hi all,

I'm trying my hand at scripting cgi in ruby. At the same time I'm  
looking at Rails. So far I love ruby. Still reserving comments on  
Rails. I have a question though. When coding a cgi script, is it  
possible to have methods as I would say in a n my\_app.rb or rails app?  
Below is a script I'm working on to provide a web interface to our  
syslog servers logs for our tech support dept. I would like to DRY this  
up a bit. I'm repeating entirely too much code. I would welcome not  
only a response to my question, but any other suggestions/criticism to  
aid me in my learning process. Thank you very much.

tonyd

#!/usr/local/bin/ruby -w

> **···**
>
> #  
> # Filename: syssearch.cgi  
> #  
> # Description:  
> # Open log(s) file and do our searching before we begin to send  
> # data back to the browser. This way we don't cause the browser to time  
> out  
> # (in theory...)  
> #  
> # Inputs: none
> 
> require 'cgi'
> 
> cgi = CGI.new
> 
> # Our search params passed  
> search\_string = cgi['searchit']  
> slogtype = cgi['logtype']  
> smethod = cgi['method']
> 
> # Do we have something to search on?  
> if search\_string.empty?  
> &nbsp;&nbsp;puts "Content-type: text/plain\r\n\r\n\<HTML\>\n\<HEAD\>\n\<TITLE\>CommSpeed  
> Log Search\</TITLE\>\n\</HEAD\>\n\<BODY\>\n"  
> &nbsp;&nbsp;puts "\<h1\>Please enter a search criteria\</h1\>"  
> &nbsp;&nbsp;puts "\</BODY\>\n\</HTML\>\n"  
> &nbsp;&nbsp;exit  
> end
> 
> # A hash of possible logs  
> logs = {'smtp' =\> 'smtp', 'mail' =\> 'maillog', 'mx' =\> 'mx', 'spam' =\>  
> 'spam', 'Dial Up' =\> 'radius', 'DHCP' =\> 'dhcp' }
> 
> if smethod == "CAT"  
> &nbsp;&nbsp;# Array to hold our search result matches  
> &nbsp;&nbsp;results = Array.new
> 
> &nbsp;&nbsp;# Open file and look for our search string  
> &nbsp;&nbsp;File.open("/var/log/"+logs[slogtype],  
> 'r').grep(/#{search\_string}/).each do |line|  
> &nbsp;&nbsp;results.push(line) # append it to our results array  
> end # file is automatically closed
> 
> &nbsp;&nbsp;if results.empty?  
> &nbsp;&nbsp;&nbsp;&nbsp;# No matches found  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "Content-type:  
> text/plain\r\n\r\n\<HTML\>\n\<HEAD\>\n\<TITLE\>CommSpeed Log  
> Search\</TITLE\>\n\</HEAD\>\n\<BODY\>\n"  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "\<h1\>No DHCP Log Results Found!\</h1\>"  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "\</BODY\>\n\</HTML\>\n"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit  
> &nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;# Now send results back to browser  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "Content-type:  
> text/plain\r\n\r\n\<HTML\>\n\<HEAD\>\n\<TITLE\>CommSpeed Log  
> Search\</TITLE\>\n\</HEAD\>\n\<BODY\>\n"  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "\<h1\>DHCP Log Results...\</h1\>"
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Print the results  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;results.each do |line|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts line+"\<br\>"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;&nbsp;# Close HTML  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "\</BODY\>\n\</HTML\>\n"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit  
> &nbsp;&nbsp;end  
> end
> 
> if smethod == "TAIL"  
> &nbsp;&nbsp;#TODO  
> &nbsp;&nbsp;#Need to figure out how to tail a file with out search scope in place  
> and feed it to the browser window.  
> end  
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
