Skip to content

Instantly share code, notes, and snippets.

require "dropbox_sdk"
class MyDropboxSession < ActiveRecord::Base
# This can obviously live anywhere you store your credentials or settings.
APP_KEY = "*********"
APP_SECRET = "%%%%%%%%%"
scope :authorized, where(authorized: true)
class CreateMyDropboxSessions < ActiveRecord::Migration
def change
create_table :my_dropbox_sessions do |t|
t.string :app_key
t.string :app_secret
t.string :account_email
t.boolean :authorized, default: false
t.text :serialized_session
t.timestamps
# 1. Create a new record (account email is optional)
@db_session = MyDropboxSession.create({app_key: MyDropboxSession::APP_KEY,
app_secret: MyDropboxSession::APP_SECRET,
account_email: "a_dropbox_email@example.com"})
# 2. Get the authorization url and visit it in your browser and then authorize the Application
@db_session.authorization_url
# 3. After authorizing in the browser, complete the authorization
# - This gets the access token, serializes the session and saves it in the database
# A. Puting a file to dropbox
@db_session.client.put_file("/Some Shared Dropbox Folder the User has access to/my_data_file.csv","#{Rails.root}/tmp/my_data_file.csv")
# B. Getting a file from dropbox (from dropbox site)
contents, metadata = @db_session.client.get_file_and_metadata('/magnum-opus.txt')
open('magnum-opus.txt', 'w') {|f| f.puts contents }
# To try and fix the sort order, switching to an integer instead of time.
integer :listed_at do |item|
original_listed_at_time = nil
original_item = item.original_item if item.original_item_id.present? && item.payout_policy == "upfront"
if original_item && original_item.created_at < Time.parse("April 1 2014")
original_listed_at_time = original_item.listed_at
end