Ruby for romance? How to update a script from myself

My wife enjoys this when I use my geek abilities to be “romantic,” so I had the idea of ​​a ruby ​​script to install on her Mac, which would send her quotes and small notes from me throughout the day. I already realized that I would use GeekTool to run the script in the background, and I would use growlnotify to display messages. Now I need a way to update the script from the Internet if it changes. I don’t know anything about ruby, except how to interact with the console.

I have 2 goals: 1) Have an XML message file. This should be available for updating from the Internet, but be local if it does not have a network connection. 2) Change the script itself from the Internet if I find errors or want to improve it.

Now I know that I could just manually update the script on my Mac if I wanted, but I decided that this would be a good way to find out an even more advanced ruby.

Priority 1 will be an XML file, it will be able to download a new version if it changes. What ruby ​​things can I use to upload a file and save it in a specific place locally?

I used hpricot before, would that be a good way to go?

Note. My mac wife is a Macbook running Leopard (but maybe 10.4, not 100%).

EDIT

If anyone is interested in the first version of the script, you can find it here

ps → the first version of the script does not actually update itself, it just updates the messages.xml file and that’s it.

+4
source share
3 answers
require 'net/http' Net::HTTP.start("your.webhost.com") { |http| resp = http.get("/messages/messages.xml") open("messages.xml", "wb") { |file| file.write(resp.body) } } 

Something like this should work. You can load the Ruby script and then call.

 exec("ruby script2.rb") #no further lines in script1 will execute 
+1
source

Won't your wife see you coming if you have a file with xml messages that happen by chance during the day? Wouldn't you rather send a message and receive it instantly?

You can use the XMPP protocol for this .
Your wife’s application connects to the XMPP server; you too. And you can send her a message whenever you want.
You can even make messages send at some distance in time between each of them.
Therefore, when she is going to a nomad without any Internet connection, and you know this, you send her 3 or 4 messages in advance. And she will receive them later.

For updating the script, this is a similar problem than you have on any website server.
So I would open the SSH port on my machine and use capistrano to deploy the latest version of the application.

+1
source

How many posts? XML can be a bit crowded if they are short. Why not just use a text file with one message per line?

 #!/usr/bin/env ruby require 'open-uri' def growl_msg(msg) %x{growlnotify -m "#{msg}"} end def read_msg begin f = open("http://www.yoursite.com/messages.txt").read File.open("messages.txt", 'w') {|f| f.write(f) } #write local copy rescue f = File.open("messages.txt").read end growl_msg(f.split("\n").sort_by{rand}[0]) #growl a random line end while true do read_msg sleep(15*60) # 15 minutes between messages end 

Using GeekTool to run a script is probably not necessary either. You can simply run the script with nohup myscript.rb and it will continue to work until you kill the process.

0
source

All Articles