GithubHelp home page GithubHelp logo

sergeyradov / dropbox Goto Github PK

View Code? Open in Web Editor NEW

This project forked from riscfuture/dropbox

0.0 1.0 0.0 305 KB

An easy-to-use client library for the official Dropbox API.

License: MIT License

Ruby 100.00%

dropbox's Introduction

Ruby Dropbox Gem

An easy-to-use third-party interface to the RESTful Dropbox API.

Installation

gem install dropbox

Tutorial by Example

First things first: Be sure you’ve gotten a consumer key and secret from developers.dropbox.com

# STEP 1: Authorize the user
session = Dropbox::Session.new('your_consumer_key', 'your_consumer_secret')
session.mode = :sandbox # might need to set this to :dropbox; consult your API account page
puts "Visit #{session.authorize_url} to log in to Dropbox. Hit enter when you have done this."
gets
session.authorize

# STEP 2: Play!
session.upload('testfile.txt', '/')
uploaded_file = session.file('testfile.txt')
puts uploaded_file.metadata.size

uploaded_file.move 'new_name.txt'
uploaded_file.delete

# STEP 3: Save session for later
File.open('serialized_session.txt', 'w') do |f|
  f.puts session.serialize
end

# STEP 4: Play with saved session!
new_session = Dropbox::Session.deserialize(File.read('serialized_session.txt'))
account = new_session.account
puts account.display_name

Tutorial by Example, Rails Edition

A simple Rails controller that allows a user to first authorize their Dropbox account, and then upload a file to their Dropbox.

class DropboxController < ApplicationController
  def authorize
    if params[:oauth_token] then
      dropbox_session = Dropbox::Session.deserialize(session[:dropbox_session])
      dropbox_session.authorize(params)
      session[:dropbox_session] = dropbox_session.serialize # re-serialize the authenticated session

      redirect_to :action => 'upload'
    else
      dropbox_session = Dropbox::Session.new('your_consumer_key', 'your_consumer_secret')
      session[:dropbox_session] = dropbox_session.serialize
      redirect_to dropbox_session.authorize_url(:oauth_callback => url_for(:action => 'authorize'))
    end
  end

  def upload
    return redirect_to(:action => 'authorize') unless session[:dropbox_session]
    dropbox_session = Dropbox::Session.deserialize(session[:dropbox_session])
    return redirect_to(:action => 'authorize') unless dropbox_session.authorized?

    if request.method == :post then
      dropbox_session.upload params[:file], 'My Uploads'
      render :text => 'Uploaded OK'
    else
      # display a multipart file field form
    end
  end
end

Features and Where to Find Them

  • Start with the Dropbox::Session class. The first thing you should do is authenticate your users and that class is how to do it.

  • The Dropbox::API module (attached to the Dropbox::Session class) is the meat and potatoes. Use it to modify a user’s Dropbox.

  • The Dropbox::Entry class is a more object-oriented way of manipulating files. It’s totally optional; check it out if you like OOP.

  • The Dropbox::Memoization module has some handy utility methods for memoizing server responses to reduce network calls. It’s plug-in compatible with any caching strategy you might already have (memcache, etc.).

  • If you’re using pingbacks, check out Dropbox::Event and Dropbox::Revision. Those classes parse pingbacks from Dropbox into Ruby objects.

Testing Your Code

The gem is fully specced. Run specs with +rake spec+. Before doing so, you will need to create a file called keys.json in the project root containing your Dropbox API key and secret, as well as the email and password for a Dropbox account. See the keys.json.example file to get started.

fguillen has implemented a mock of the Dropbox API server: github.com/fguillen/DummyDropbox

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.

Copyright © 2009 Tim Morgan. See LICENSE for details.

dropbox's People

Contributors

riscfuture avatar tb0yd avatar lmartinking avatar crystalneth avatar igorw avatar nanek avatar mathieuravaux avatar nickstamas avatar bcoe avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.