2. 6/13/2013Scripting with Ruby
Why scripting?
Perform regular tasks automatically
Create daily reports, publish information, back up files, clear history
Perform background tasks
Send emails, share information with other systems
Easily use operating system programs/utilities
FTP, scp, mail, file storage
4. 6/13/2013Scripting with Ruby
Sample shell script
#!/bin/tcsh
#
# Expects one parameter in mm/dd/yyyy format
#
setenv TOP /opt/sp
setenv SBIN ${TOP}/sbin
setenv LOG ${TOP}/log
set logDir=${LOG}/tradetotals
#
# Convert mm/dd/yyyy to yyyymmdd
#
set yyyy=`echo $1 | cut c710`
set mm=`echo $1 | cut c12`
set dd=`echo $1 | cut c45`
set tradeDate=${yyyy}${mm}${dd}
set saveDir=${logDir}/${yyyy}
#
# Run the totals
#
fisql Uxxx Pxxx Ssss w140 > /tmp/total.out <<EOF
exec getTotals '$1'
go
quit
EOF
#
# See if there was any output
#
if ( z /tmp/total.out ) then
rm f /tmp/total.out
exit 0
endif
#
# Nice formatting
#
echo "" > /tmp/total.fax
echo "Totals for $1" >> /tmp/total.fax
echo "" >> /tmp/total.fax
#
# Get rid of isql junk
#
cat /tmp/total.out | grep v line1 | grep v '' | grep v
"return stat" | egrep v e '^$' > /tmp/total.out1
#
# Get rid of tabs
#
sed e "s///" /tmp/total.out1 >> /tmp/total.fax
rm f /tmp/total.out /tmp/total.out1
#
# Send the file to the mail server for emailing
#
${SBIN}/emailtofinop.csh "Trade Totals for $1" /tmp/total.fax
#
# Save the file for prosperity
#
if ( ! d ${saveDir} ) then
mkdir ${saveDir}
endif
mv f /tmp/total.fax ${saveDir}/total.${tradeDate}
5. 6/13/2013Scripting with Ruby
What can Ruby do?
Ruby Standard Library
✔ Numbers and Math
✔ Strings
✔ Regular expressions
✔ Collections
✔ Dates and Times
✔ Files and Directories
✔ Networking
✔ Threads
6. 6/13/2013Scripting with Ruby
My First Exposure to Ruby
require 'rubygems'
require 'roo'
require 'fileutils'
ss = Excel.new( "business_plan_cmbs.xls" )
ss.default_sheet = "Recommended"
puts ss.cell('B', 8)
'C'.upto('BJ') do |column|
date = ss.cell(2, column)
monthnum = ss.cell(3, column)
yearnum = ss.cell(4, column)
interest = ss.cell(8, column)
amort = ss.cell(9, column)
other = ss.cell(10, column)
sum = interest + amort + other
puts "#{date} (#{monthnum}/#{yearnum})t#{sum}"
end
7. 6/13/2013Scripting with Ruby
Sending email
require 'net/smtp'
# Send email
Net::SMTP.start('mail.silveradosw.com', 25, 'www.silveradosw.com') do |smtp|
smtp.open_message_stream('stuart@silveradosw.com', 'rubyjax@meetup.com') do |f|
f.puts 'From: Stuart Palmer <stuart@silveradosw.com>'
f.puts 'To: Ruby Jax <rubyjax@meetup.com>'
f.puts "Date: #{DateTime.now.rfc2822}"
f.puts 'Subject: Your Report for today'
f.puts 'MessageID: <ruby1234@silveradosw.com>'
lines.each do |line|
f.puts line
end
end
end
8. 6/13/2013Scripting with Ruby
File functions and FTP
require 'file'
require 'fileutils'
# Write to a file
report_file = File.new('/tmp/reportnew', 'w')
lines.each do |line|
report_file.puts line
end
report_file.close
FileUtils.mv('/tmp/reportnew', '/opt/xxx/todaysreport')
# FTP the file
ftp = Net::FTP.new('ftp.silveradosw.com')
ftp.login('stuart@silveradosw.com', 'xxx')
ftp.passive = true
ftp.chdir('Reports')
ftp.puttextfile('/tmp/reportnew', 'todays_report')
ftp.close
9. 6/13/2013Scripting with Ruby
File functions and FTP
require 'file'
require 'fileutils'
require 'net/ftp'
# Write to a file
report_file = File.new('/tmp/reportnew', 'w')
lines.each do |line|
report_file.puts line
end
report_file.close
FileUtils.mv('/tmp/reportnew', '/opt/xxx/todaysreport')
# FTP the file
ftp = Net::FTP.new('ftp.silveradosw.com')
ftp.login('stuart@silveradosw.com', 'xxx')
ftp.passive = true
ftp.chdir('Reports')
ftp.puttextfile('/tmp/reportnew', 'todays_report')
ftp.close
10. 6/13/2013Scripting with Ruby
Client / Server
require 'socket'
socket = TCPSocket.open('localhost', 3333)
socket.puts('hello')
socket.puts('what?')
socket.puts('done')
socket = TCPSocket.open('localhost', 3333)
socket.puts('quit')
require 'socket'
socket = TCPSocket.new('localhost', 3333)
done_listening = false
while !done_listening
done_reading = false
while !done_reading
message = socket.read
if message =~ /^quit$|^stop$/i
puts "Received shutdown message."
done_listening = true
done_reading = true
elsif message =~ /^done$/i
puts "Received client end message."
done_reading = true
else
puts "Read message: [#{message}]"
socket.puts "OKn"
end
end
socket.close
end
15. 6/13/2013Scripting with Ruby
Test First
● The usual test frameworks can be used
➢ rspec, test::unit, minitest
C:UsersStuartClientswhbbiwhbbireports> rspec c fs tradetotalsemailspec.rb
TradeTotalsEmail
initialize
should call BaseEmail::new
should call BaseEmail#from with name and email when set in config
set_sender
should call BaseEmail#from with name and email
set_recipients
should call add_recipients with passedin name/email array
should call add_recipients with config name/email
set_default_recipients
should call add_to with config name and email
add_recipients
should call add_to with each passedin name and email
add_to
should call BaseEmail:to when nonnull name and email passed in
Finished in 0.018 seconds
8 examples, 0 failures