Integrating Scribd with your Rails application

12 Mar 2008

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



Javascript database

10 Mar 2008

This is cool… TaffyDB is an in-browser javascript database. This could be really useful for doing changes to a dataset without moving page to page, and then simply sending JSON back to a Rails or Merb app to save the changes.



Do or do not…

1 Mar 2008

Chris came up with this excellent little snippet, try, as seen on Ruby Inside.

That inspired me to a create a similarly sugary method for an idiom I use time and again:

class Object
  ##
  #   @person.name rescue nil
  # vs
  #   @person.do_or_do_not(:name)
  def do_or_do_not(method)
    send method rescue nil
  end
end


Who’s the number 2 most influential entrepreneur-blogger in Seattle?

27 Feb 2008

I am! According to Marcelo, who founded Sampa and blogs about the Seattle startup scene:

Most Influential Entrepreneur-Bloggers of Seattle

Thanks, Marcelo! My mom will be so proud. :)



Sample Rails consulting contract

20 Feb 2008

I’ve had requests from time to time from other developers for the contract that I use with clients. Here’s the Terms section that I typically use. In addition to this section, I have sections that describe the work to be done, the payment schedule, and other info specific to each client.

Enjoy!

Sample Rails consulting contract

Ob. disclaimer: I am not a lawyer, and this is not legal advice—I’m just some guy on the internet.