Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 111 lines (85 sloc) 4.047 kb
3db9072 @RISCfuture Docs
authored
1 = Ruby Dropbox Gem
36bef4d Initial commit to dropbox.
Tim Morgan authored
2
3db9072 @RISCfuture Docs
authored
3 An easy-to-use third-party interface to the RESTful Dropbox API.
48d84c7 @RISCfuture Configuration
authored
4
dbc9e78 Doc fixes
Tim Morgan authored
5 == Installation
6
6c7e8b5 Change name of gem from rdropbox to dropbox
Tim Morgan authored
7 gem install dropbox
dbc9e78 Doc fixes
Tim Morgan authored
8
48d84c7 @RISCfuture Configuration
authored
9 == Tutorial by Example
10
3db9072 @RISCfuture Docs
authored
11 First things first: Be sure you've gotten a consumer key and secret from
12 http://developers.dropbox.com
13
14 # STEP 1: Authorize the user
15 session = Dropbox::Session.new('your_consumer_key', 'your_consumer_secret')
fe3dc45 @RISCfuture Updated README and added example
authored
16 session.mode = :sandbox # might need to set this to :dropbox; consult your API account page
3db9072 @RISCfuture Docs
authored
17 puts "Visit #{session.authorize_url} to log in to Dropbox. Hit enter when you have done this."
18 gets
19 session.authorize
20
21 # STEP 2: Play!
9fc2ab3 @RISCfuture Example fix
authored
22 session.upload('testfile.txt', '/')
23 uploaded_file = session.file('testfile.txt')
3db9072 @RISCfuture Docs
authored
24 puts uploaded_file.metadata.size
25
26 uploaded_file.move 'new_name.txt'
27 uploaded_file.delete
28
59a87c6 @jonallured added to the basic example to illustrate how serializing the session wor...
jonallured authored
29 # STEP 3: Save session for later
30 File.open('serialized_session.txt', 'w') do |f|
31 f.puts session.serialize
32 end
33
34 # STEP 4: Play with saved session!
35 new_session = Dropbox::Session.deserialize(File.read('serialized_session.txt'))
36 account = new_session.account
37 puts account.display_name
38
3db9072 @RISCfuture Docs
authored
39 == Tutorial by Example, Rails Edition
40
41 A simple Rails controller that allows a user to first authorize their Dropbox
42 account, and then upload a file to their Dropbox.
43
44 class DropboxController < ApplicationController
45 def authorize
46 if params[:oauth_token] then
47 dropbox_session = Dropbox::Session.deserialize(session[:dropbox_session])
48 dropbox_session.authorize(params)
49 session[:dropbox_session] = dropbox_session.serialize # re-serialize the authenticated session
50
51 redirect_to :action => 'upload'
52 else
53 dropbox_session = Dropbox::Session.new('your_consumer_key', 'your_consumer_secret')
54 session[:dropbox_session] = dropbox_session.serialize
55 redirect_to dropbox_session.authorize_url(:oauth_callback => url_for(:action => 'authorize'))
56 end
57 end
58
59 def upload
60 return redirect_to(:action => 'authorize') unless session[:dropbox_session]
61 dropbox_session = Dropbox::Session.deserialize(session[:dropbox_session])
62 return redirect_to(:action => 'authorize') unless dropbox_session.authorized?
63
64 if request.method == :post then
dbc9e78 Doc fixes
Tim Morgan authored
65 dropbox_session.upload params[:file], 'My Uploads'
3db9072 @RISCfuture Docs
authored
66 render :text => 'Uploaded OK'
67 else
68 # display a multipart file field form
69 end
70 end
71 end
72
73 == Features and Where to Find Them
74
75 * Start with the Dropbox::Session class. The first thing you should do is
76 authenticate your users and that class is how to do it.
77 * The Dropbox::API module (attached to the Dropbox::Session class) is the meat
78 and potatoes. Use it to modify a user's Dropbox.
79 * The Dropbox::Entry class is a more object-oriented way of manipulating files.
80 It's totally optional; check it out if you like OOP.
81 * The Dropbox::Memoization module has some handy utility methods for memoizing
82 server responses to reduce network calls. It's plug-in compatible with any
83 caching strategy you might already have (memcache, etc.).
dbc9e78 Doc fixes
Tim Morgan authored
84 * If you're using pingbacks, check out Dropbox::Event and Dropbox::Revision.
85 Those classes parse pingbacks from Dropbox into Ruby objects.
36bef4d Initial commit to dropbox.
Tim Morgan authored
86
e3c57ea @RISCfuture Note on fguillen's mock
authored
87 == Testing Your Code
88
d6c3a4e @RISCfuture Bugfixes and improvements to tboyd47's additions:
authored
89 The gem is fully specced. Run specs with +rake spec+. Before doing so, you will
90 need to create a file called +keys.json+ in the project root containing your
91 Dropbox API key and secret, as well as the email and password for a Dropbox
92 account. See the +keys.json.example+ file to get started.
93
e3c57ea @RISCfuture Note on fguillen's mock
authored
94 fguillen has implemented a mock of the Dropbox API server:
95 http://github.com/fguillen/DummyDropbox
96
36bef4d Initial commit to dropbox.
Tim Morgan authored
97 == Note on Patches/Pull Requests
98
99 * Fork the project.
100 * Make your feature addition or bug fix.
101 * Add tests for it. This is important so I don't break it in a
102 future version unintentionally.
103 * Commit, do not mess with rakefile, version, or history.
104 (if you want to have your own version, that is fine but
3db9072 @RISCfuture Docs
authored
105 bump version in a commit by itself I can ignore when I pull)
36bef4d Initial commit to dropbox.
Tim Morgan authored
106 * Send me a pull request. Bonus points for topic branches.
107
108 == Copyright
109
110 Copyright (c) 2009 Tim Morgan. See LICENSE for details.
Something went wrong with that request. Please try again.