box-api 0.1.7 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile.lock +1 -1
- data/box-api.gemspec +1 -1
- data/doc/Box.html +108 -0
- data/doc/Box/Account.html +1099 -0
- data/doc/Box/Api.html +3428 -0
- data/doc/Box/Api/AccountExceeded.html +130 -0
- data/doc/Box/Api/EmailInvalid.html +141 -0
- data/doc/Box/Api/EmailTaken.html +130 -0
- data/doc/Box/Api/ErrorStatus.html +130 -0
- data/doc/Box/Api/Exception.html +120 -0
- data/doc/Box/Api/Generic.html +130 -0
- data/doc/Box/Api/InvalidFolder.html +141 -0
- data/doc/Box/Api/InvalidInput.html +130 -0
- data/doc/Box/Api/InvalidName.html +130 -0
- data/doc/Box/Api/NameTaken.html +130 -0
- data/doc/Box/Api/NoAccess.html +130 -0
- data/doc/Box/Api/NoParent.html +130 -0
- data/doc/Box/Api/NotAuthorized.html +130 -0
- data/doc/Box/Api/Restricted.html +141 -0
- data/doc/Box/Api/SizeExceeded.html +130 -0
- data/doc/Box/Api/Unknown.html +130 -0
- data/doc/Box/Api/UnknownResponse.html +141 -0
- data/doc/Box/Api/UploadFailed.html +141 -0
- data/doc/Box/File.html +580 -0
- data/doc/Box/Folder.html +1078 -0
- data/doc/Box/Item.html +1884 -0
- data/doc/_index.html +342 -0
- data/doc/class_list.html +47 -0
- data/doc/css/common.css +1 -0
- data/doc/css/full_list.css +53 -0
- data/doc/css/style.css +320 -0
- data/doc/file.README.html +98 -0
- data/doc/file_list.html +49 -0
- data/doc/frames.html +13 -0
- data/doc/index.html +98 -0
- data/doc/js/app.js +205 -0
- data/doc/js/full_list.js +150 -0
- data/doc/js/jquery.js +16 -0
- data/doc/method_list.html +574 -0
- data/doc/top-level-namespace.html +103 -0
- data/examples/login.rb +4 -4
- data/lib/box/account.rb +168 -51
- data/lib/box/api.rb +174 -16
- data/lib/box/api/exceptions.rb +4 -0
- data/lib/box/file.rb +17 -4
- data/lib/box/folder.rb +85 -30
- data/lib/box/item.rb +89 -20
- data/spec/account_spec.rb +0 -4
- data/spec/folder_spec.rb +3 -9
- metadata +42 -4
@@ -0,0 +1,103 @@
|
|
1
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
2
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
3
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
4
|
+
<head>
|
5
|
+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
6
|
+
<title>
|
7
|
+
Top Level Namespace
|
8
|
+
|
9
|
+
— Documentation by YARD 0.7.2
|
10
|
+
|
11
|
+
</title>
|
12
|
+
|
13
|
+
<link rel="stylesheet" href="css/style.css" type="text/css" media="screen" charset="utf-8" />
|
14
|
+
|
15
|
+
<link rel="stylesheet" href="css/common.css" type="text/css" media="screen" charset="utf-8" />
|
16
|
+
|
17
|
+
<script type="text/javascript" charset="utf-8">
|
18
|
+
relpath = '';
|
19
|
+
if (relpath != '') relpath += '/';
|
20
|
+
</script>
|
21
|
+
|
22
|
+
<script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
|
23
|
+
|
24
|
+
<script type="text/javascript" charset="utf-8" src="js/app.js"></script>
|
25
|
+
|
26
|
+
|
27
|
+
</head>
|
28
|
+
<body>
|
29
|
+
<script type="text/javascript" charset="utf-8">
|
30
|
+
if (window.top.frames.main) document.body.className = 'frames';
|
31
|
+
</script>
|
32
|
+
|
33
|
+
<div id="header">
|
34
|
+
<div id="menu">
|
35
|
+
|
36
|
+
<a href="_index.html">Index</a> »
|
37
|
+
|
38
|
+
|
39
|
+
<span class="title">Top Level Namespace</span>
|
40
|
+
|
41
|
+
|
42
|
+
<div class="noframes"><span class="title">(</span><a href="." target="_top">no frames</a><span class="title">)</span></div>
|
43
|
+
</div>
|
44
|
+
|
45
|
+
<div id="search">
|
46
|
+
|
47
|
+
<a id="class_list_link" href="#">Class List</a>
|
48
|
+
|
49
|
+
<a id="method_list_link" href="#">Method List</a>
|
50
|
+
|
51
|
+
<a id="file_list_link" href="#">File List</a>
|
52
|
+
|
53
|
+
</div>
|
54
|
+
<div class="clear"></div>
|
55
|
+
</div>
|
56
|
+
|
57
|
+
<iframe id="search_frame"></iframe>
|
58
|
+
|
59
|
+
<div id="content"><h1>Top Level Namespace
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
</h1>
|
64
|
+
|
65
|
+
<dl class="box">
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
</dl>
|
75
|
+
<div class="clear"></div>
|
76
|
+
|
77
|
+
<h2>Defined Under Namespace</h2>
|
78
|
+
<p class="children">
|
79
|
+
|
80
|
+
|
81
|
+
<strong class="modules">Modules:</strong> <span class='object_link'><a href="Box.html" title="Box (module)">Box</a></span>
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
|
86
|
+
</p>
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
|
94
|
+
</div>
|
95
|
+
|
96
|
+
<div id="footer">
|
97
|
+
Generated on Mon Aug 8 13:05:22 2011 by
|
98
|
+
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
99
|
+
0.7.2 (ruby-1.9.2).
|
100
|
+
</div>
|
101
|
+
|
102
|
+
</body>
|
103
|
+
</html>
|
data/examples/login.rb
CHANGED
@@ -24,19 +24,19 @@ account = Box::Account.new(app_data['api_key'])
|
|
24
24
|
auth_token = app_data['auth_token']
|
25
25
|
|
26
26
|
# now we have enough information to log into the box api, so we try to authorize using the auth token
|
27
|
-
|
27
|
+
account.authorize(auth_token) do |auth_url|
|
28
28
|
# this block is called if the auth_token is invalid or missing
|
29
29
|
|
30
30
|
# we use launchy to open a new browser, but you can do it however you want
|
31
31
|
Launchy.open(auth_url)
|
32
32
|
|
33
33
|
# make sure you pause until the user has authorized, or just tell them to restart the program
|
34
|
-
puts "
|
34
|
+
puts "Please press the enter key once you have authorized this application to use your account"
|
35
35
|
gets
|
36
36
|
end
|
37
37
|
|
38
|
-
unless
|
39
|
-
# the user didn't
|
38
|
+
unless account.authorized?
|
39
|
+
# the user didn't authorize like we told them to, so we can't access anything
|
40
40
|
puts "Unable to login, please try again."
|
41
41
|
exit
|
42
42
|
end
|
data/lib/box/account.rb
CHANGED
@@ -2,32 +2,111 @@ require 'box/api'
|
|
2
2
|
require 'box/folder'
|
3
3
|
|
4
4
|
module Box
|
5
|
-
|
6
|
-
|
5
|
+
# Represents an account on Box. In order to use the Box api, the user
|
6
|
+
# must first grant the application permission to use their account. This
|
7
|
+
# is done in the {#authorize} function. Once an account has been
|
8
|
+
# authorized, it can access all of the details and information stored
|
9
|
+
# on that account.
|
7
10
|
|
8
|
-
|
11
|
+
class Account
|
12
|
+
# @return [String] The auth token if authorization was successful.
|
13
|
+
attr_reader :auth_token
|
14
|
+
|
15
|
+
# Creates an account object using the given Box api key.
|
16
|
+
# You can then {#register} a new account or {#authorize} an
|
17
|
+
# existing account.
|
18
|
+
#
|
19
|
+
# @param [String, Api] api the api key to use for the Box api.
|
9
20
|
def initialize(api)
|
10
21
|
@api = case
|
11
22
|
when api.class == Box::Api; api # use the api object as passed in
|
12
|
-
else; Box::Api.new(api) # allows user to pass in
|
23
|
+
else; Box::Api.new(api) # allows user to pass in a string
|
13
24
|
end
|
14
25
|
end
|
15
26
|
|
16
|
-
|
17
|
-
#
|
27
|
+
# Register a new account on the Box website with the given details.
|
28
|
+
#
|
29
|
+
# @param [String] email The email address to create the account with
|
30
|
+
# @param [String] password The password to create the account with
|
31
|
+
# @return [Boolean] Whether registration was successful.
|
32
|
+
#
|
33
|
+
# @raise [Api::EmailInvalid] The email address was invalid
|
34
|
+
# @raise [Api::EmailTaken] The email address was taken
|
35
|
+
#
|
18
36
|
def register(email, password)
|
19
|
-
# note: this function can throw EmailInvalid and EmailTaken
|
20
37
|
response = @api.register_new_user(email, password)
|
21
38
|
|
22
|
-
cache_info(response['user']) # cache account_info,
|
39
|
+
cache_info(response['user']) # cache account_info, saving an extra API call
|
23
40
|
authorize_token(response['token'])
|
41
|
+
|
42
|
+
true
|
43
|
+
end
|
44
|
+
|
45
|
+
# Authorize the account using the given auth token, or request
|
46
|
+
# permission from the user to let this application use their account.
|
47
|
+
#
|
48
|
+
# An auth token can be reused from previous authorizations provided the
|
49
|
+
# user doesn't log out, and significantly speeds up the process. If the
|
50
|
+
# auth token if invalid or not provided, the account tries to log in
|
51
|
+
# normally and requires the user to log in and provide access for their
|
52
|
+
# account.
|
53
|
+
#
|
54
|
+
# @param [Optional, String] auth_token Uses an existing token or requests
|
55
|
+
# a new one if nil.
|
56
|
+
# @yield [authorize_url] This block called when the user has not yet
|
57
|
+
# granted this application permission to use their account. You
|
58
|
+
# must have the user navigate to the passed url and authorize
|
59
|
+
# this app before continuing.
|
60
|
+
# @return [Boolean] Whether the user is authorized.
|
61
|
+
#
|
62
|
+
# @example Authorize an account without a saved auth token.
|
63
|
+
# account.authorize do |auth_url|
|
64
|
+
# puts "Please visit #{ auth_url } and enter your account infomation"
|
65
|
+
# puts "Press the enter key once you have done this."
|
66
|
+
# gets # wait for the enter key to be pressed
|
67
|
+
# end
|
68
|
+
#
|
69
|
+
# @example Authorize an account using an existing auth token.
|
70
|
+
# auth_token = "saved auth token" # load from file ideally
|
71
|
+
# account.authorize(auth_token)
|
72
|
+
#
|
73
|
+
# @example Combining the above two for the best functionality.
|
74
|
+
# auth_token = "saved auth token" # load from file if possible
|
75
|
+
# account.authorize(auth_token) do |auth_url|
|
76
|
+
# # auth token was invalid or nil, have the user visit auth_url
|
77
|
+
# end
|
78
|
+
#
|
79
|
+
def authorize(auth_token = nil)
|
80
|
+
# use a saved auth token if it is given
|
81
|
+
if auth_token
|
82
|
+
return true if authorize_token(auth_token)
|
83
|
+
end
|
84
|
+
|
85
|
+
# the auth token either failed or was not provided
|
86
|
+
# we must try to authorize a ticket, and call the block if that fails
|
87
|
+
if not authorize_ticket and block_given?
|
88
|
+
# the supplied block should instruct the user to visit this url
|
89
|
+
yield authorize_url
|
90
|
+
|
91
|
+
# try authorizing once more
|
92
|
+
authorize_ticket
|
93
|
+
end
|
94
|
+
|
95
|
+
# return our authorized status
|
96
|
+
authorized?
|
24
97
|
end
|
25
98
|
|
26
|
-
#
|
99
|
+
# Log out of the account and invalidate the auth token.
|
100
|
+
#
|
101
|
+
# @note The user will have to re-authorize if they wish to use this
|
102
|
+
# application, and the auth token will no longer work.
|
103
|
+
#
|
104
|
+
# @return [Boolean] Whether logout was successful.
|
105
|
+
#
|
27
106
|
def logout
|
28
107
|
begin
|
29
108
|
@api.logout
|
30
|
-
|
109
|
+
cache_token(nil)
|
31
110
|
rescue Api::NotAuthorized
|
32
111
|
# already logged out, or never logged in
|
33
112
|
end
|
@@ -35,72 +114,110 @@ module Box
|
|
35
114
|
true
|
36
115
|
end
|
37
116
|
|
38
|
-
#
|
39
|
-
|
40
|
-
|
117
|
+
# Return the account details. A cached copy will be used if avaliable,
|
118
|
+
# and requested if it is not.
|
119
|
+
#
|
120
|
+
# @param [Boolean] refresh Will not use the cached version if true.
|
121
|
+
# @return [Hash] A hash containing all of the user's account
|
122
|
+
# details, or nil if they are not authorized. Please see the
|
123
|
+
# Box api documentation for information about each field.
|
124
|
+
#
|
125
|
+
# TODO: Add url to Box api documentation, and provide the current fields.
|
126
|
+
#
|
127
|
+
def info(refresh = false)
|
128
|
+
return @info if @info and not refresh
|
41
129
|
|
42
|
-
|
43
|
-
|
44
|
-
|
130
|
+
begin
|
131
|
+
cache_info(nil) # reset existing info
|
132
|
+
info = @api.get_account_info['user']
|
133
|
+
cache_info(info)
|
134
|
+
rescue Api::NotAuthorized, Api::InvalidInput
|
135
|
+
nil
|
45
136
|
end
|
137
|
+
end
|
46
138
|
|
47
|
-
|
139
|
+
# Get the root folder of the account. You can use this {Folder} object
|
140
|
+
# to access all sub items within the account. This folder is lazy loaded,
|
141
|
+
# and a network request will be made if/when the data is requested.
|
142
|
+
#
|
143
|
+
# @return [Folder] A folder object representing the root folder.
|
144
|
+
#
|
145
|
+
def root
|
146
|
+
return @root if @root
|
147
|
+
@root = Box::Folder.new(@api, nil, :id => 0)
|
148
|
+
end
|
149
|
+
|
150
|
+
# @return [Boolean] Is the account authorized?
|
151
|
+
def authorized?
|
152
|
+
@info != nil
|
48
153
|
end
|
49
154
|
|
50
|
-
|
155
|
+
protected
|
156
|
+
|
157
|
+
# @return [Api] The api currently in use.
|
158
|
+
attr_reader :api
|
159
|
+
|
160
|
+
# Get the cached ticket or request a new one from the Box api.
|
161
|
+
# @return [String] The authorization ticket.
|
51
162
|
def ticket
|
52
163
|
@ticket ||= @api.get_ticket['ticket']
|
53
164
|
end
|
54
165
|
|
55
|
-
# the user
|
166
|
+
# The url the user needs to visit in order to grant this application
|
167
|
+
# permission to use their account. This requires a ticket, which
|
168
|
+
# is either pulled from the cache or requested.
|
169
|
+
#
|
170
|
+
# @param [String] ticket Use the ticket for the url.
|
171
|
+
# If no ticket is provided, one will be requested and cached.
|
172
|
+
# @return [String] the url used for authorizing this account.
|
173
|
+
#
|
56
174
|
def authorize_url(ticket = self.ticket)
|
57
175
|
"#{ api.base_url }/auth/#{ ticket }"
|
58
176
|
end
|
59
177
|
|
60
|
-
#
|
61
|
-
|
178
|
+
# Attempt to authorize this account using the given ticket. This will
|
179
|
+
# only succeed if the user has granted this ticket permission, done
|
180
|
+
# by visiting and logging into the {#authorize_url}.
|
181
|
+
#
|
182
|
+
# @param [String] ticket The ticket used for authorization.
|
183
|
+
# If no ticket is provided, one will be requested and cached.
|
184
|
+
# @return [String, nil] The auth token if successful otherwise, nil.
|
185
|
+
#
|
186
|
+
def authorize_ticket(ticket = self.ticket)
|
62
187
|
begin
|
63
188
|
response = @api.get_auth_token(ticket)
|
64
189
|
|
65
|
-
cache_info(response['user']) #
|
66
|
-
|
190
|
+
cache_info(response['user']) # saves an extra API call
|
191
|
+
cache_token(response['auth_token'])
|
67
192
|
rescue Api::NotAuthorized
|
68
|
-
|
193
|
+
nil
|
69
194
|
end
|
70
195
|
end
|
71
196
|
|
72
|
-
#
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
#
|
79
|
-
def
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
# return the cached account info, or request it
|
84
|
-
def info
|
85
|
-
return @info if @info
|
197
|
+
# Attempt to authorize this account using the given auth token. This
|
198
|
+
# will only succeed if the auth token has been used before, and
|
199
|
+
# be done to make login easier.
|
200
|
+
#
|
201
|
+
# @param [String] auth_token The auth token to attempt to use
|
202
|
+
# @return [Boolean] If the attempt was successful.
|
203
|
+
#
|
204
|
+
def authorize_token(auth_token)
|
205
|
+
cache_token(auth_token)
|
206
|
+
info(true) # force a refresh
|
86
207
|
|
87
|
-
|
88
|
-
info = @api.get_account_info['user']
|
89
|
-
cache_info(info)
|
90
|
-
rescue Api::NotAuthorized, Api::InvalidInput
|
91
|
-
nil
|
92
|
-
end
|
208
|
+
authorized?
|
93
209
|
end
|
94
210
|
|
95
|
-
#
|
96
|
-
|
97
|
-
|
98
|
-
|
211
|
+
# Use and cache the given auth token.
|
212
|
+
# @param [String] auth_token The auth token to cache.
|
213
|
+
# @return [String] The auth token.
|
214
|
+
def cache_token(auth_token)
|
215
|
+
@api.set_auth_token(auth_token)
|
216
|
+
@auth_token = auth_token
|
99
217
|
end
|
100
218
|
|
101
|
-
|
102
|
-
|
103
|
-
# cache account info, possibly from get_auth_token, register_new_user, or get_account_info
|
219
|
+
# Cache the account info.
|
220
|
+
# @param [Hash] info The account info to cache.
|
104
221
|
def cache_info(info)
|
105
222
|
@info = info
|
106
223
|
end
|
data/lib/box/api.rb
CHANGED
@@ -3,11 +3,36 @@ require 'box/api/exceptions'
|
|
3
3
|
require 'httmultiparty'
|
4
4
|
|
5
5
|
module Box
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
# A wrapper and interface to the Box api. Please visit the Box developers
|
7
|
+
# site for a full explaination of what each of the Box api methods
|
8
|
+
# expect and perform.
|
9
|
+
# TODO: Link to the site.
|
10
10
|
|
11
|
+
class Api
|
12
|
+
# an extension of HTTParty, adding multi-part upload support
|
13
|
+
include HTTMultiParty
|
14
|
+
|
15
|
+
# @return [String] The base url of the box api.
|
16
|
+
attr_accessor :base_url
|
17
|
+
|
18
|
+
# @return [String] The upload url of the box api.
|
19
|
+
attr_accessor :upload_url
|
20
|
+
|
21
|
+
# Create a new API object using the given parameters.
|
22
|
+
#
|
23
|
+
# @note Chances are that if the Box api is updated or moves location,
|
24
|
+
# this class will no longer work. However, the option to change
|
25
|
+
# the defaults still remains.
|
26
|
+
#
|
27
|
+
# @param [String, Api] api_key The api key for your application. You can
|
28
|
+
# request one on the Box developer website at
|
29
|
+
# {http://www.box.net/developers/services}. If an {Api} instance
|
30
|
+
# is passed instead, its key is used.
|
31
|
+
#
|
32
|
+
# @param [String] url the url of the Box api.
|
33
|
+
# @param [String] upload_url the url of the upload host for the Box api.
|
34
|
+
# @param [String] version the version of the Box api in use.
|
35
|
+
#
|
11
36
|
def initialize(key, url = 'https://box.net', upload_url = 'https://upload.box.net', version = '1.0')
|
12
37
|
@default_params = { :api_key => key } # add the api_key to every query
|
13
38
|
|
@@ -15,20 +40,61 @@ module Box
|
|
15
40
|
@upload_url = "#{ upload_url }/api/#{ version }" # uploads use a different url than everything else
|
16
41
|
end
|
17
42
|
|
43
|
+
# Make a normal REST request.
|
44
|
+
#
|
45
|
+
# @param [String] expected the normal status expected to be returned.
|
46
|
+
# If the actual status does not match, an exception is thrown.
|
47
|
+
# @param [Hash] options The parameters that wish to be passed in the
|
48
|
+
# request. These should coorespond to the api specifications,
|
49
|
+
# and will be passed along with the api key and auth token.
|
50
|
+
#
|
51
|
+
# @return [Hash] A parsed version of the XML response.
|
52
|
+
#
|
18
53
|
def query_rest(expected, options = {})
|
19
54
|
query_raw('get', "#{ @base_url }/rest", expected, options)['response']
|
20
55
|
end
|
21
56
|
|
22
|
-
|
23
|
-
|
24
|
-
|
57
|
+
# Make a download request.
|
58
|
+
#
|
59
|
+
# @param [String] query the operation to be performed ("download").
|
60
|
+
# @param [Array] args an array of arguments to put in the url.
|
61
|
+
# @param [Hash] options (see #query_rest)
|
62
|
+
#
|
63
|
+
# @return The raw binary data of the file.
|
64
|
+
#
|
65
|
+
def query_download(args, options = {})
|
66
|
+
# produces: /download/<auth_token>/<arg1>/<arg2>/<etc>
|
67
|
+
url = [ "#{ @base_url }/download", @auth_token, args ].flatten.compact.join('/')
|
68
|
+
query_raw('get', url, nil, options)
|
25
69
|
end
|
26
70
|
|
71
|
+
# Make an upload request.
|
72
|
+
#
|
73
|
+
# @param [String] query The operation to be performed.
|
74
|
+
# @param [Array] args (see #query_download)
|
75
|
+
# @param [String] expected (see #query_rest)
|
76
|
+
# @param [Hash] options (see #query_rest)
|
77
|
+
#
|
78
|
+
# @return (see #query_rest)
|
79
|
+
#
|
27
80
|
def query_upload(query, args, expected, options = {})
|
28
|
-
|
81
|
+
# produces: /upload/<auth_token>/<arg1>/<arg2>/<etc>
|
82
|
+
url = [ "#{ @upload_url }/#{ query }", @auth_token, args ].flatten.compact.join('/')
|
29
83
|
query_raw('post', url, expected, options)['response']
|
30
84
|
end
|
31
85
|
|
86
|
+
# Make a raw request.
|
87
|
+
#
|
88
|
+
# @note: HTTParty will automatically parse the response from its native
|
89
|
+
# XML to a nested hash/array structure.
|
90
|
+
#
|
91
|
+
# @param ['get', 'post'] method The HTTP method to use.
|
92
|
+
# @param [String] url The url to make the request.
|
93
|
+
# @param [String] expected (see #query_rest)
|
94
|
+
# @param [Hash] options (see #query_rest)
|
95
|
+
#
|
96
|
+
# @return (see #query_rest)
|
97
|
+
#
|
32
98
|
def query_raw(method, url, expected, options = {})
|
33
99
|
response = case method
|
34
100
|
when 'get'
|
@@ -40,6 +106,15 @@ module Box
|
|
40
106
|
handle_response(response, expected)
|
41
107
|
end
|
42
108
|
|
109
|
+
# Handle the response of the request.
|
110
|
+
#
|
111
|
+
# @param [Hash] response The parsed representation of the XML response.
|
112
|
+
# @param expected (see #query_rest)
|
113
|
+
#
|
114
|
+
# @return [Hash] The response if no errors were found.
|
115
|
+
# @raise [Exception, UnknownResponse] Raises an exception if the
|
116
|
+
# response status does not match the expected. This exception
|
117
|
+
# is determined by {.get_exception}
|
43
118
|
def handle_response(response, expected = nil)
|
44
119
|
if expected
|
45
120
|
begin
|
@@ -58,83 +133,166 @@ module Box
|
|
58
133
|
response
|
59
134
|
end
|
60
135
|
|
136
|
+
# TODO: Add link to API documentation for all functions below.
|
137
|
+
# TODO: Document exceptions that could be thrown.
|
138
|
+
|
139
|
+
# Request a ticket for authorization
|
61
140
|
def get_ticket
|
62
141
|
query_rest('get_ticket_ok', :action => :get_ticket)
|
63
142
|
end
|
64
143
|
|
144
|
+
# Request an auth token given a ticket.
|
145
|
+
#
|
146
|
+
# @param [String] ticket the ticket to use.
|
65
147
|
def get_auth_token(ticket)
|
66
148
|
query_rest('get_auth_token_ok', :action => :get_auth_token, :ticket => ticket)
|
67
149
|
end
|
68
150
|
|
69
|
-
#
|
151
|
+
# Add the auth token to every request.
|
152
|
+
#
|
153
|
+
# @param [String] auth_token The auth token to add to every request.
|
70
154
|
def set_auth_token(auth_token)
|
71
155
|
@auth_token = auth_token
|
72
156
|
@default_params[:auth_token] = auth_token
|
73
157
|
end
|
74
158
|
|
159
|
+
# Request the user be logged out.
|
75
160
|
def logout
|
76
161
|
query_rest('logout_ok', :action => :logout)
|
77
162
|
end
|
78
163
|
|
79
|
-
|
80
|
-
|
164
|
+
# Register a new user.
|
165
|
+
#
|
166
|
+
# @param [String] email The email address to use.
|
167
|
+
# @param [String] password The password to use.
|
168
|
+
def register_new_user(email, password)
|
169
|
+
query_rest('successful_register', :action => :register_new_user, :login => email, :password => password)
|
81
170
|
end
|
82
171
|
|
83
|
-
|
84
|
-
|
172
|
+
# Verify a registration email.
|
173
|
+
#
|
174
|
+
# @param [String] email The email address to check.
|
175
|
+
def verify_registration_email(email)
|
176
|
+
query_rest('email_ok', :action => :verify_registration_email, :login => email)
|
85
177
|
end
|
86
178
|
|
179
|
+
# Get the user's account info.
|
87
180
|
def get_account_info
|
88
181
|
query_rest('get_account_info_ok', :action => :get_account_info)
|
89
182
|
end
|
90
183
|
|
91
|
-
#
|
92
|
-
|
184
|
+
# Get the entire tree of a given folder.
|
185
|
+
#
|
186
|
+
# @param [String] folder_id The id of the folder to use.
|
187
|
+
# @param [Array] args The arguments to pass along to get_account_tree.
|
188
|
+
#
|
189
|
+
# @note This function can take a long time for large folders.
|
190
|
+
# @todo Use zip compression to save bandwidth.
|
191
|
+
#
|
192
|
+
# TODO: document the possible arguments.
|
193
|
+
def get_account_tree(folder_id, *args)
|
93
194
|
query_rest('listing_ok', :action => :get_account_tree, :folder_id => folder_id, :params => [ 'nozip' ] + args)
|
94
195
|
end
|
95
196
|
|
197
|
+
# Create a new folder.
|
198
|
+
#
|
199
|
+
# @param [String] parent_id The id of the parent folder to use.
|
200
|
+
# @param [String] name The name of the newly created folder.
|
201
|
+
# @param [Integer] shared The shared state of the new folder.
|
96
202
|
def create_folder(parent_id, name, share = 0)
|
97
203
|
query_rest('create_ok', :action => :create_folder, :parent_id => parent_id, :name => name, :share => share)
|
98
204
|
end
|
99
205
|
|
206
|
+
# Move the item to a new destination.
|
207
|
+
#
|
208
|
+
# @param ["file", "folder"] target The type of item.
|
209
|
+
# @param [String] target_id The id of the item to move.
|
210
|
+
# @param [String] destination_id The id of the parent to move to.
|
100
211
|
def move(target, target_id, destination_id)
|
101
212
|
query_rest('s_move_node', :action => :move, :target => target, :target_id => target_id, :destination_id => destination_id)
|
102
213
|
end
|
103
214
|
|
215
|
+
# Copy the the item to a new destination.
|
216
|
+
#
|
217
|
+
# @note The api currently only supports copying files.
|
218
|
+
#
|
219
|
+
# @param ["file"] target The type of item.
|
220
|
+
# @param [String] target_id The id of the item to copy.
|
221
|
+
# @param [String] destination_id The id of the parent to copy to.
|
104
222
|
def copy(target, target_id, destination_id)
|
105
223
|
query_rest('s_copy_node', :action => :copy, :target => target, :target_id => target_id, :destination_id => destination_id)
|
106
224
|
end
|
107
225
|
|
226
|
+
# Rename the item.
|
227
|
+
#
|
228
|
+
# @param ["file", "folder"] target The type of item.
|
229
|
+
# @param [String] target_id The id of the item to rename.
|
230
|
+
# @param [String] new_name The new name to be used.
|
108
231
|
def rename(target, target_id, new_name)
|
109
232
|
query_rest('s_rename_node', :action => :rename, :target => target, :target_id => target_id, :new_name => new_name)
|
110
233
|
end
|
111
234
|
|
235
|
+
# Delete the item.
|
236
|
+
#
|
237
|
+
# @param ["file", "folder"] target The type of item.
|
238
|
+
# @param [String] target_id The id of the item to delete.
|
112
239
|
def delete(target, target_id)
|
113
240
|
query_rest('s_delete_node', :action => :delete, :target => target, :target_id => target_id)
|
114
241
|
end
|
115
242
|
|
243
|
+
# Get the file info.
|
244
|
+
#
|
245
|
+
# @param [String] file_id The file id to get info for.
|
116
246
|
def get_file_info(file_id)
|
117
247
|
query_rest('s_get_file_info', :action => :get_file_info, :file_id => file_id)
|
118
248
|
end
|
119
249
|
|
250
|
+
# Set the item description.
|
251
|
+
#
|
252
|
+
# @param ["file", "folder"] target The type of item.
|
253
|
+
# @param [String] target_id The id of the item to describe.
|
254
|
+
# @param [String] description The description to use.
|
120
255
|
def set_description(target, target_id, description)
|
121
256
|
query_rest('s_set_description', :action => :set_description, :target => target, :target_id => target_id, :description => description)
|
122
257
|
end
|
123
258
|
|
259
|
+
# Download the file to the given path.
|
260
|
+
#
|
261
|
+
# @note You cannot download folders.
|
262
|
+
#
|
263
|
+
# @param [String] path The path to write the file to.
|
264
|
+
# @param [String] file_id The file id to download.
|
265
|
+
# @param [Optional, String] version The version of the file to download.
|
124
266
|
def download(path, file_id, version = nil)
|
125
267
|
::File.open(path, 'w') do |file|
|
126
|
-
file << query_download(
|
268
|
+
file << query_download([ file_id, version ]) # write the response directly to file
|
127
269
|
end
|
128
270
|
end
|
129
271
|
|
272
|
+
# Upload the file to the specified folder.
|
273
|
+
#
|
274
|
+
# @param [String] path Upload the file at the given path.
|
275
|
+
# @param [String] folder_id The folder id of the parent folder to use.
|
276
|
+
# @param [Optional, Boolean] new_copy Upload a new copy instead of overwriting.
|
130
277
|
def upload(path, folder_id, new_copy = false)
|
131
278
|
query_upload('upload', folder_id, 'upload_ok', :file => ::File.new(path), :new_copy => new_copy)
|
132
279
|
end
|
133
280
|
|
281
|
+
# Overwrite the given file with a new one.
|
282
|
+
#
|
283
|
+
# @param [String] path (see #upload)
|
284
|
+
# @param [String] file_id Replace the file with this id.
|
285
|
+
# @param [Optional, String] name Use a new name as well.
|
134
286
|
def overwrite(path, file_id, name = nil)
|
135
287
|
query_upload('overwrite', file_id, 'upload_ok', :file => ::File.new(path), :file_name => name)
|
136
288
|
end
|
137
289
|
|
290
|
+
# Upload a new copy of the given file.
|
291
|
+
#
|
292
|
+
# @param [String] path (see #upload)
|
293
|
+
# @param [String] file_id The id of the file to copy.
|
294
|
+
# @param [Optional, String] name Use a new name as well.
|
295
|
+
# TODO: Verfiy this does what I think it does
|
138
296
|
def new_copy(path, file_id, name = nil)
|
139
297
|
query_upload('new_copy', file_id, 'upload_ok', :file => ::File.new(path), :new_file_name => name)
|
140
298
|
end
|