views:

379

answers:

5

I would like to upload documents to GoogleDocs every time the OS hears that a file was added/dragged/saved in a designated folder, just the way DropBox uploads a file when you save it in the DropBox folder.

What would this take in Ruby, what are the parts?

  • How do you listen for when a File is Saved?
  • How do you listen for when a File is added to a Folder?

I understand how to use the GoogleDocs API and upload things once I get these events, but I'm not sure how this would work.

A: 

I don't know the correct way of doing this, but a simple hack would be to have a script running in the background which checks the contents of a bunch of folders every n minutes and uses the associated timestamps to determine if the file was modified in that span of time

adi92
A: 

You would definitely need some native OS code here, to write the monitoring service/client. I'd select C++ if you want it to be cross platform. If you decide to go with .Net, for example, you can use the FileSystemWatcher class to achieve what you need (documentation and here's a related article).

Traveling Tech Guy
+1  A: 

Ruby doesn't include a built-in way to "listen" for updates to files. If you want to stick to pure Ruby, your best bet would be to perform the upload on a fixed schedule (say every 5 minutes) regardless of when the file is saved.

If this isn't an acceptable alternative, you could try writing the app (or at least certain parts of it) in Java, which does support this type of thing. Take a look at JRuby for integrating the Ruby and Java portions of your app.

igul222
+1  A: 

If I were faced with this, I would use something like git or bzr to handle the version checking and just call add then commit from your script and monitor which files have changed (and therefore need to be uploaded).

This adds the benefit of full version control over files and it's mostly cross platform (if you include binaries for each platform).

Note this doesn't handle your listening problem, just what you do when you know something has changed. You could schedule the task (via various routes) but I still like the idea of a proper VCS under the hood.

I just found this: http://www.codeforpeople.com/lib/ruby/dirwatch/

You'd need to read over it as I can't vouch for its efficiency or reliability. It appears to use SQLite, so it might be better just to manually check once every 10 seconds (or something along those lines).

Oli
A: 

Here is a pure ruby gem:

http://github.com/TwP/directory_watcher

Mike