Friday, 19 July 2013

Integrate Dropbox with your Rails App by using Dropbox API gem with Paperclip

We are going to steal all the Dropbox documents by using Dropbox API with the help of Paperclip. Hey got scared !! .. Oh pls ..  just kidding. But seriously ,We can do that with the help of dropbox-api gem & paperclip .

Step 1

Create an App in Dropbox  Developer  to get the key-Secret pair. You have to set the permission type to "Full Dropbox" mode to get the full Read/Write Access to any file inside the user's Dropbox.

Step 2

Setup your Rails App , include these 2 gems in your Gemfile.

gem 'dropbox-api'
gem "paperclip"

Step 3

Create a file in your config/initializers directory and name it dropbox.rb and put the following line of code inside.


Dropbox::API::Config.app_key    = 'APP KEY'
Dropbox::API::Config.app_secret = 'SECRET KEY'
Dropbox::API::Config.mode       = "dropbox"

Step 4

we have to create Paperclip model for downloading the Files from Dropbox and create the same in your app in Rails way.


class DropboxFiles < ActiveRecord::Base

   attr_accessible :url , :created_at ,:updated_at, :document

   has_attached_file :document ,

    :url => "/system/:class/:attachment/:id/:style/:basename.:extension",
    :path => "#{Rails.root}/public/system/:class/:attachment/:id/:style/:basename.:extension",
    :storage => ((Rails.env.production? || Rails.env.staging?) ? :s3 : :filesystem)

end

Step 5

Controller and Callback Actions for authenticating the user first to Dropbox and redirecting the user back to the callback action to perform the Downloading Activities.


routes.rb


match '/dropbox/authorize'   => 'dropbox#authorize' , :method => :get , :as => :dropbox_auth
match '/dropbox/callback' => 'dropbox#callback' , :method => :get , :as =>  :dropbox_callback

dropbox_controller.rb


I will brief you the functions written inside Dropbox controller.

 authorize Action will authorize the user with App and will redirect the user back to the Callback action. Callback Action will give you the secret token pair as a response . We can create a Dropbox client object with access token & secret token pair .

Now we are ready to download the entire documents ( One after another ) belongs to that particular user.

We will list all the documents and will iterate inside a loop . this time we will check whether the document is a File or a Directory . it will call  the function recursively until it reaches a File object .

If it is a File object then will call download_and_create(obj) function and will pass the same File obj as an argument. Inside download_and_create(obj) function we will again call one more function named download_file_from_url and will pass the same argument .The File object that we passed as an argument has one instance method named "redirect_url" , we can get the full Dropbox path of that particular document by calling "redirect_url.url" method.

download_file_from_url action will download the file first to Server and will return the absolute path of the downloaded file. So now the File is there in our Rails App. we can create a Paperclip file Object by using DropboxFiles model , DropboxFiles.create(:document => dp_file). here we are passing the absolute file path as an argument.It will create same File in Our Rails application. Don't forget to remove the file from your Rails root path after creating the Paperclip Object.

So that's it , hope you guys will try this . There are lot of options that we can try out with Dropbox-api gem. I have not optimized the code , hope you guys will do it for me. so have fun coding ...

4 comments:

  1. Replies
    1. Thank u Willi , Hope you have found this post useful..

      Delete
  2. Thanks for sharing such a nice information with us. There are many Ruby on Rails companies in India but I found Cryptex Technologies is one of the best company. they offer best price in the industry and complete value of money to clients.

    ReplyDelete