Integrating Scribd with your Rails application

I recently integrated the Scribd API with my resume management application, Catch the Best, so I thought I'd share a few details here about how I did that in case you are interested in using their great API.

To get started, first you need to install the rscribd gem and set up an account at Scribd to get API access.

Once you do that, it's easy to send a document to Scribd:

require 'rscribd'

Scribd::API.instance.key = 'yourkey' Scribd::API.instance.secret = 'yoursecret' Scribd::User.login 'login', 'pass'

doc = Scribd::Document.upload(:file => '/path/to/file')

One thing to consider when creating documents with Scribd is that you don't want to be uploading documents to them during the request/response cycle. It's not that Scribd is particularly slow, but you don't want unnecessary delays slowing down your users. To accomplish that, I move the call for Scribd::Document to a method in my Attachment class (which is using attachment_fu), and then queue the creation of the Scribd document with the excellent Bj plugin. So, my model ends up looking something like this:

class Attachment < ActiveRecord::Base has_attachment ... after_create :send_to_scribd def scribdify doc = Scribd::Document.upload(:file => self.full_filename, :access => 'private') ... end def send_to_scribd Bj.submit("./script/runner ./script/scribd.rb #{self.id}") end end

The scribd.rb script is itself pretty simple -- it just does the setup code for Scribd and then calls scribdify on the attachments it finds from the arguments passed to Bj.

And that's it! Easy Scribd API integration for your Rails app. You can see the finished product here: Catch the Best integrates with iPaper

Update: See info on integrating Scribd in your Rails app -- client-side edition.

Comments