Code works in IRB but not when saved and run (NET/TOC)

I’m using the net-toc gem to connect to AIM with Ruby 1.8.7 since 1.9.2
doesn’t seem to support net-toc. The code below works fine if I input it
line by line into IRB, but it won’t create a connection and send an IM
if I run the code from a file($ ruby aim.rb). I’ve tried adding sleep(2)
after each connection to emulate the time it took me to write each line
in IRB, but that didn’t work either. Any ideas?

#Loads net/toc, aim gem
require ‘rubygems’
require ‘net/toc’

#Get username, password, buddy’s screenname, and message
print "username: "
username = gets
print "password: "
password = gets
print "buddy’s screenname: "
screenname = gets
print "message: "
message = gets

#Define TOC Connection
client = Net::TOC.new(username, password)

#Connect
client.connect

#Grab object associated with buddy’s screenname
friend = client.buddy_list.buddy_named(screenname)

#Sends the IM
friend.send_im(message)

Irb is keeping the session alive. Maybe it’s not working because your
script is exiting before it can?

Do you get any errors?

Do you have both 1.8.7 and 1.9.2 installed? If so, maybe you’re using
two
different versions–add “puts RUBY_VERSION” variable in irb and your rb
file.

My other guess would be that you might need something like EventMachine
to
handle asynchronous events, but I would look into the simpler solutions
first.

Wouldn’t a sleep(5) at the end of the script test for this?

No errors. The rb file completes-- but the account never signs on and no
message is sent. RUBY_VERSION yields 1.8.7 for both the rb file and irb.

Using eventmachine, would it look something like this?

EventMachine::run do

#Define TOC Connection
client = Net::TOC.new(username, password)

#Connect
client.connect

#Grab object associated with buddy’s screenname
friend = client.buddy_list.buddy_named(screenname)

#Sends the IM
friend.send_im(message)

end

So I ran your code. I notice that client.connect returns a separate
thread,
so creating your own event/thread with EM shouldn’t be needed.

take a look at the documentation:
http://net-toc.rubyforge.org/doc/classes/Net/TOC/Client.html#M000012

wait(limit=nil)
http://net-toc.rubyforge.org/doc/classes/Net/TOC/Client.src/M000012.html

Waits for the event-handling thread for limit seconds, or indefinitely
if no
argument is given. Use this to prevent your program from exiting
prematurely…

I haven’t been able to get a ruby script to connect, and doing it in IRB
works for me, too. I think using client.wait (instead of sleep) is
going in
the right direction. I did a “puts client.buddy_list” in the client,
which
reported back my buddy list. However, doing this in the script printed
out
an empty line, so it looks like the connection isn’t completing. You
may
want to try playing with this some more–hope this helps

Thanks very much for the help! I’ve gotten the script working, but am
still working on more elegant solutions.

One big issue with my original code was not stripping the gets-- the
username and password were tailed by \n’s.

“sleep 1” after the client connects and after the message is sent allow
the script to work.

I’m still trying to see what I can do with the .wait method.

I was able to get rid of the sleeps by looping the program, giving the
user a chance to send another message after the process completes.

#Loads net/toc, aim gem
require ‘rubygems’
require ‘net/toc’

#Get username, password
puts “Screenname:”
username = gets.strip
puts "Password: "
password = gets.strip

#Define TOC Connection
client = Net::TOC.new(username, password)

#Connect
client.connect

#Create screenname variable
screenname = nil

#Loop until it receives the exit command
while screenname != “*”

#Get buddy’s screenname
puts “Buddy’s Screenname(or ‘*’ to exit):”
screenname = gets.strip

#Check for exit command
if screenname == “*”
client.disconnect
else

#Get message
puts "Message: "
message = gets.strip

#Grab object associated with buddy’s screenname
friend = client.buddy_list.buddy_named(screenname)

#Send the IM
friend.send_im(message)

end
end