box-api 0.1.6
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/.gitignore +1 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +40 -0
- data/LICENSE.txt +202 -0
- data/README.md +23 -0
- data/Rakefile +6 -0
- data/box-api.gemspec +21 -0
- data/examples/app_data.yml +6 -0
- data/examples/files.rb +36 -0
- data/examples/login.rb +57 -0
- data/lib/box-api.rb +16 -0
- data/lib/box/account.rb +108 -0
- data/lib/box/api.rb +140 -0
- data/lib/box/api/exceptions.rb +75 -0
- data/lib/box/file.rb +37 -0
- data/lib/box/folder.rb +140 -0
- data/lib/box/item.rb +127 -0
- data/spec/account_spec.rb +43 -0
- data/spec/api_spec.rb +19 -0
- data/spec/file_spec.rb +105 -0
- data/spec/folder_spec.rb +117 -0
- data/spec/helper/account.rb +17 -0
- data/spec/helper/account.yml +3 -0
- data/spec/helper/fake_tree.rb +101 -0
- data/spec/item_spec.rb +51 -0
- metadata +139 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
pkg/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
box-api (0.1.6)
|
5
|
+
httmultiparty (~> 0.3)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
configuration (1.3.1)
|
11
|
+
crack (0.1.8)
|
12
|
+
diff-lcs (1.1.2)
|
13
|
+
httmultiparty (0.3)
|
14
|
+
httparty (>= 0.7.3)
|
15
|
+
multipart-post
|
16
|
+
httparty (0.7.8)
|
17
|
+
crack (= 0.1.8)
|
18
|
+
launchy (0.4.0)
|
19
|
+
configuration (>= 0.0.5)
|
20
|
+
rake (>= 0.8.1)
|
21
|
+
multipart-post (1.1.2)
|
22
|
+
rake (0.9.2)
|
23
|
+
rspec (2.6.0)
|
24
|
+
rspec-core (~> 2.6.0)
|
25
|
+
rspec-expectations (~> 2.6.0)
|
26
|
+
rspec-mocks (~> 2.6.0)
|
27
|
+
rspec-core (2.6.4)
|
28
|
+
rspec-expectations (2.6.0)
|
29
|
+
diff-lcs (~> 1.1.2)
|
30
|
+
rspec-mocks (2.6.0)
|
31
|
+
|
32
|
+
PLATFORMS
|
33
|
+
java
|
34
|
+
ruby
|
35
|
+
|
36
|
+
DEPENDENCIES
|
37
|
+
box-api!
|
38
|
+
launchy
|
39
|
+
rake
|
40
|
+
rspec
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
|
2
|
+
Apache License
|
3
|
+
Version 2.0, January 2004
|
4
|
+
http://www.apache.org/licenses/
|
5
|
+
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14
|
+
the copyright owner that is granting the License.
|
15
|
+
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
17
|
+
other entities that control, are controlled by, or are under common
|
18
|
+
control with that entity. For the purposes of this definition,
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
20
|
+
direction or management of such entity, whether by contract or
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25
|
+
exercising permissions granted by this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
28
|
+
including but not limited to software source code, documentation
|
29
|
+
source, and configuration files.
|
30
|
+
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
32
|
+
transformation or translation of a Source form, including but
|
33
|
+
not limited to compiled object code, generated documentation,
|
34
|
+
and conversions to other media types.
|
35
|
+
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
37
|
+
Object form, made available under the License, as indicated by a
|
38
|
+
copyright notice that is included in or attached to the work
|
39
|
+
(an example is provided in the Appendix below).
|
40
|
+
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47
|
+
the Work and Derivative Works thereof.
|
48
|
+
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
50
|
+
the original version of the Work and any modifications or additions
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
62
|
+
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
65
|
+
subsequently incorporated within the Work.
|
66
|
+
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
73
|
+
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79
|
+
where such license applies only to those patent claims licensable
|
80
|
+
by such Contributor that are necessarily infringed by their
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
83
|
+
institute patent litigation against any entity (including a
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
86
|
+
or contributory patent infringement, then any patent licenses
|
87
|
+
granted to You under this License for that Work shall terminate
|
88
|
+
as of the date such litigation is filed.
|
89
|
+
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
92
|
+
modifications, and in Source or Object form, provided that You
|
93
|
+
meet the following conditions:
|
94
|
+
|
95
|
+
(a) You must give any other recipients of the Work or
|
96
|
+
Derivative Works a copy of this License; and
|
97
|
+
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
99
|
+
stating that You changed the files; and
|
100
|
+
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
103
|
+
attribution notices from the Source form of the Work,
|
104
|
+
excluding those notices that do not pertain to any part of
|
105
|
+
the Derivative Works; and
|
106
|
+
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
109
|
+
include a readable copy of the attribution notices contained
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
112
|
+
of the following places: within a NOTICE text file distributed
|
113
|
+
as part of the Derivative Works; within the Source form or
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
115
|
+
within a display generated by the Derivative Works, if and
|
116
|
+
wherever such third-party notices normally appear. The contents
|
117
|
+
of the NOTICE file are for informational purposes only and
|
118
|
+
do not modify the License. You may add Your own attribution
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
121
|
+
that such additional attribution notices cannot be construed
|
122
|
+
as modifying the License.
|
123
|
+
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
125
|
+
may provide additional or different license terms and conditions
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
129
|
+
the conditions stated in this License.
|
130
|
+
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
134
|
+
this License, without any additional terms or conditions.
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136
|
+
the terms of any separate license agreement you may have executed
|
137
|
+
with Licensor regarding such Contributions.
|
138
|
+
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
141
|
+
except as required for reasonable and customary use in describing the
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
143
|
+
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
153
|
+
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
159
|
+
incidental, or consequential damages of any character arising as a
|
160
|
+
result of this License or out of the use or inability to use the
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
163
|
+
other commercial damages or losses), even if such Contributor
|
164
|
+
has been advised of the possibility of such damages.
|
165
|
+
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169
|
+
or other liability obligations and/or rights consistent with this
|
170
|
+
License. However, in accepting such obligations, You may act only
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
175
|
+
of your accepting any such warranty or additional liability.
|
176
|
+
|
177
|
+
END OF TERMS AND CONDITIONS
|
178
|
+
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
180
|
+
|
181
|
+
To apply the Apache License to your work, attach the following
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
183
|
+
replaced with your own identifying information. (Don't include
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
185
|
+
comment syntax for the file format. We also recommend that a
|
186
|
+
file or class name and description of purpose be included on the
|
187
|
+
same "printed page" as the copyright notice for easier
|
188
|
+
identification within third-party archives.
|
189
|
+
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
191
|
+
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
193
|
+
you may not use this file except in compliance with the License.
|
194
|
+
You may obtain a copy of the License at
|
195
|
+
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
197
|
+
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
201
|
+
See the License for the specific language governing permissions and
|
202
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
Box Ruby Library
|
2
|
+
----------------
|
3
|
+
|
4
|
+
You can use the Box.net API to create applications and websites that integrate with Box.net and can perform the following functions:
|
5
|
+
|
6
|
+
* Store and retrieve files from Box.net
|
7
|
+
* Organize files into folders
|
8
|
+
* Move, rename and delete files
|
9
|
+
* Share files
|
10
|
+
|
11
|
+
### Usage ###
|
12
|
+
|
13
|
+
You must first obtain an API key by [registering your Box.net application](http://www.box.net/developers/services).
|
14
|
+
|
15
|
+
Once you have an API key, you are free to start using the box API! Check out the 'examples' folder for sample code and instructions.
|
16
|
+
|
17
|
+
### Documentation ###
|
18
|
+
|
19
|
+
You can find API documentation at [http://developers.box.net/](http://developers.box.net/). Full documentation regarding the Ruby library methods is coming soon.
|
20
|
+
|
21
|
+
### Support ###
|
22
|
+
|
23
|
+
If you have any questions or suggestions for improvement, ping us on our [forum](http://forum.developers.box.net/box_developers) or email us directly at developers[at]box[dot]net.
|
data/Rakefile
ADDED
data/box-api.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "box-api"
|
3
|
+
s.version = "0.1.6"
|
4
|
+
s.platform = Gem::Platform::RUBY
|
5
|
+
s.authors = ["Box.net", "Luke Curley"]
|
6
|
+
s.email = ["luke@box.net"]
|
7
|
+
s.homepage = "http://box.net"
|
8
|
+
s.summary = %q{A ruby library that helps navigate the Box API}
|
9
|
+
s.description = %q{Box-api is a collection of classes that implement functions defined in the Box public API. See http://developer.box.net for more information.}
|
10
|
+
|
11
|
+
s.files = `git ls-files`.split("\n")
|
12
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
13
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
14
|
+
s.require_paths = [ "lib", "spec" ]
|
15
|
+
|
16
|
+
s.add_dependency("httmultiparty", "~> 0.3")
|
17
|
+
|
18
|
+
s.add_development_dependency("launchy") # only used in examples
|
19
|
+
s.add_development_dependency("rake")
|
20
|
+
s.add_development_dependency("rspec")
|
21
|
+
end
|
data/examples/files.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# log in using the login example, so we don't have to duplicate code
|
2
|
+
$: << File.dirname(__FILE__) # for 1.9
|
3
|
+
require 'login'
|
4
|
+
|
5
|
+
# get the root of the folder structure
|
6
|
+
root = @account.root
|
7
|
+
|
8
|
+
# list all of the folders in the root directory with their index
|
9
|
+
root.folders.each_with_index do |folder, i|
|
10
|
+
puts "##{ i } -- #{ folder.name }"
|
11
|
+
end
|
12
|
+
|
13
|
+
# let the user pick one to show the contents of
|
14
|
+
puts "Pick a folder number above to show: "
|
15
|
+
index = gets
|
16
|
+
|
17
|
+
begin
|
18
|
+
# grab the folder they selected
|
19
|
+
# to_i or [] will throw an exception if the index is invalid or out of range respectively
|
20
|
+
folder = root.folders[index.to_i]
|
21
|
+
rescue
|
22
|
+
# they picked an invalid folder!
|
23
|
+
puts "You picked an invalid folder, please try again."
|
24
|
+
exit
|
25
|
+
end
|
26
|
+
|
27
|
+
# the folder they picked was valid
|
28
|
+
puts "Excellent choice, here are the contents of that folder"
|
29
|
+
|
30
|
+
# show the selected folder
|
31
|
+
puts "FOLDER: #{ folder.name } (#{ folder.id })"
|
32
|
+
|
33
|
+
# loop through and show each of the sub files and folders
|
34
|
+
(folder.files + folder.folders).each do |item|
|
35
|
+
puts "\t#{ item.type.upcase }: #{ item.name } (#{ item.id })"
|
36
|
+
end
|
data/examples/login.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# add the lib directory to our require path (you do not need this if you install the box-api gem)
|
2
|
+
$: << File.dirname(__FILE__) + "/../lib"
|
3
|
+
|
4
|
+
# we use bundler to keep all of our gems up to date, but it is optional
|
5
|
+
require 'rubygems'
|
6
|
+
require 'bundler/setup'
|
7
|
+
|
8
|
+
# we're only using account specific features in this example, so we don't need to require the entire gem
|
9
|
+
require 'box/account'
|
10
|
+
|
11
|
+
# launchy is a simple gem that opens a browser, but is completely optional
|
12
|
+
require 'launchy'
|
13
|
+
|
14
|
+
# we save all of our app data to a file, so we don't have to hard-code values
|
15
|
+
app_data_file = File.dirname(__FILE__) + '/app_data.yml'
|
16
|
+
app_data = YAML.load_file(app_data_file)
|
17
|
+
|
18
|
+
# create an account object using the API key stored in app_data.yml
|
19
|
+
# you need to get your own API key at http://www.box.net/developers/services
|
20
|
+
account = Box::Account.new(app_data['api_key'])
|
21
|
+
|
22
|
+
# the user has to authorize your app on the box website, at which point you'll get an auth token
|
23
|
+
# this auth token works between sessions, so read it from disk if the user has authed before
|
24
|
+
auth_token = app_data['auth_token']
|
25
|
+
|
26
|
+
# now we have enough information to log into the box api, so we try to authorize using the auth token
|
27
|
+
authed = account.authorize(auth_token) do |auth_url|
|
28
|
+
# this block is called if the auth_token is invalid or missing
|
29
|
+
|
30
|
+
# we use launchy to open a new browser, but you can do it however you want
|
31
|
+
Launchy.open(auth_url)
|
32
|
+
|
33
|
+
# make sure you pause until the user has authorized, or just tell them to restart the program
|
34
|
+
puts "Hit any key once you have logged in on the opened web page."
|
35
|
+
gets
|
36
|
+
end
|
37
|
+
|
38
|
+
unless authed
|
39
|
+
# the user didn't auth like we told them to, so we can't access anything
|
40
|
+
puts "Unable to login, please try again."
|
41
|
+
exit
|
42
|
+
end
|
43
|
+
|
44
|
+
# this auth token will let us instantly authorize next time this app is run, so we want to save it
|
45
|
+
# keep in mind that an auth token only works with the same api key, and will expire if the user logs out
|
46
|
+
app_data['auth_token'] = account.auth_token
|
47
|
+
|
48
|
+
# we write the auth_token to file so it can be accessed on next run
|
49
|
+
File.open(app_data_file, 'w') do |file|
|
50
|
+
YAML.dump(app_data, file)
|
51
|
+
end
|
52
|
+
|
53
|
+
# we managed to log in successfully!
|
54
|
+
puts "Logged in as #{ account.info['login'] }"
|
55
|
+
|
56
|
+
# this is so the other example can access the account variable (bad practice)
|
57
|
+
@account = account
|
data/lib/box-api.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
2
|
+
# you may not use this file except in compliance with the License.
|
3
|
+
# You may obtain a copy of the License at
|
4
|
+
#
|
5
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
6
|
+
#
|
7
|
+
# Unless required by applicable law or agreed to in writing, software
|
8
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
9
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
10
|
+
# See the License for the specific language governing permissions and
|
11
|
+
# limitations under the License.
|
12
|
+
|
13
|
+
# These three are the only classes that should be accessed directly
|
14
|
+
require 'box/account'
|
15
|
+
require 'box/file'
|
16
|
+
require 'box/folder'
|
data/lib/box/account.rb
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
require 'box/api'
|
2
|
+
require 'box/folder'
|
3
|
+
|
4
|
+
module Box
|
5
|
+
class Account
|
6
|
+
attr_reader :api, :ticket, :auth_token
|
7
|
+
|
8
|
+
# setup the api using our api_key
|
9
|
+
def initialize(api)
|
10
|
+
@api = case
|
11
|
+
when api.class == Box::Api; api # use the api object as passed in
|
12
|
+
else; Box::Api.new(api) # allows user to pass in the api_key rather than make a new API object themselves
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
#### AUTHENTICATION SECTION ####
|
17
|
+
# register the user with the given details
|
18
|
+
def register(email, password)
|
19
|
+
# note: this function can throw EmailInvalid and EmailTaken
|
20
|
+
response = @api.register_new_user(email, password)
|
21
|
+
|
22
|
+
cache_info(response['user']) # cache account_info, saves an extra API call
|
23
|
+
authorize_token(response['token'])
|
24
|
+
end
|
25
|
+
|
26
|
+
# logout, rendering any auth_token useless
|
27
|
+
def logout
|
28
|
+
begin
|
29
|
+
@api.logout
|
30
|
+
authorize_token(nil)
|
31
|
+
rescue Api::NotAuthorized
|
32
|
+
# already logged out, or never logged in
|
33
|
+
end
|
34
|
+
|
35
|
+
true
|
36
|
+
end
|
37
|
+
|
38
|
+
# authorize the application either using a saved auth_token or ask for a new token
|
39
|
+
def authorize(auth_token = self.auth_token)
|
40
|
+
return true if auth_token and authorize_token(auth_token) # saved auth_tokens significantly speed up the authentication process
|
41
|
+
|
42
|
+
if block_given? and not authorize_ticket # if we cannot authorize the ticket, the user needs to visit a web page and give our app permission
|
43
|
+
yield authorize_url # the supplied block should instruct the user to visit this url, returning once they have
|
44
|
+
authorize_ticket # try authorizing again, assuming the user has authed now
|
45
|
+
end
|
46
|
+
|
47
|
+
authorized?
|
48
|
+
end
|
49
|
+
|
50
|
+
# tickets are used to associate authentication attempts, so each user needs their own ticket
|
51
|
+
def ticket
|
52
|
+
@ticket ||= @api.get_ticket['ticket']
|
53
|
+
end
|
54
|
+
|
55
|
+
# the user must visit this url to allow the application to access their account
|
56
|
+
def authorize_url(ticket = self.ticket)
|
57
|
+
"#{ api.url }/auth/#{ ticket }"
|
58
|
+
end
|
59
|
+
|
60
|
+
# using the ticket, we get the auth_token providing the user has already allowed our application the privilege
|
61
|
+
def authorize_ticket(ticket = self.ticket) # note: self.ticket will request a ticket if none exists, not the same as @ticket
|
62
|
+
begin
|
63
|
+
response = @api.get_auth_token(ticket)
|
64
|
+
|
65
|
+
cache_info(response['user']) # cache account_info, saves an extra API call
|
66
|
+
authorize_token(response['auth_token'])
|
67
|
+
rescue Api::NotAuthorized
|
68
|
+
false
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
# we have a token, and we have to save it and check if it is valid
|
73
|
+
def authorize_token(auth_token = self.auth_token)
|
74
|
+
@api.set_auth_token(auth_token)
|
75
|
+
@auth_token = auth_token if authorized? # set auth token if successful
|
76
|
+
end
|
77
|
+
|
78
|
+
# we are authorized if we have the user's account info
|
79
|
+
def authorized?
|
80
|
+
info != nil
|
81
|
+
end
|
82
|
+
|
83
|
+
# return the cached account info, or request it
|
84
|
+
def info
|
85
|
+
return @info if @info
|
86
|
+
|
87
|
+
begin
|
88
|
+
info = @api.get_account_info['user']
|
89
|
+
cache_info(info)
|
90
|
+
rescue Api::NotAuthorized, Api::InvalidInput
|
91
|
+
nil
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# return the root of the user's folder structure
|
96
|
+
def root
|
97
|
+
return @root if @root
|
98
|
+
@root = Box::Folder.new(@api, nil, :id => 0)
|
99
|
+
end
|
100
|
+
|
101
|
+
protected
|
102
|
+
|
103
|
+
# cache account info, possibly from get_auth_token, register_new_user, or get_account_info
|
104
|
+
def cache_info(info)
|
105
|
+
@info = info
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|