Parsing Excel files on Heroku with roo
When it comes to processing Excel files in Ruby, your options are slim. A quick Google or Github search might reveal roo. roo is an interesting beast. It appears that the RubyGems.org gem file has been taken over and there’s some large inconsistencies between the official gem and the Github codebase. The official gem is at version 1.9.5 as of this writing and the Github repo is still stuck at 1.3.11. Don’t you hate when that happens?
This guide doesn’t involve using the actual roo API, see the Github page or rubyforge page for that.
Getting roo to work on Heroku
Without tweaking, roo doesn’t work on Heroku. It makes the (bad) assumption that the file system is writable. Check out this line of library to see where the temporary directory gets set. This line assigns a directory within the current working directory as the temporary file store.
On Heroku, that won’t be possible. While the roo gem is packed in the dyno, it’s directory is read only.
Directly after the linked line is another line that refers to the environment variable ROO_TMP. In version 1.2.0, ROO_TMP was added to alleviate issues where the current directory is inadequate to store temporary files.
To get roo to work on Heroku, create an initializer and set the environment variable to the Rails tmp directory (the only writable directory on Heroku):
config/initializers/roo.rb
ENV['ROO_TMP'] = "#{RAILS_ROOT}/tmp/"
Now, when your app is run either on Heroku or locally, roo will use the Rails tmp directory to store it’s files.
Happy rooing!
9 comments