ruby-box 1.3.1 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/Gemfile +1 -0
- data/Gemfile.lock +1 -0
- data/VERSION +1 -1
- data/lib/ruby-box/folder.rb +13 -4
- data/lib/ruby-box/item.rb +13 -1
- data/lib/ruby-box/session.rb +15 -2
- data/ruby-box.gemspec +5 -2
- data/spec/folder_spec.rb +0 -9
- data/spec/integration_spec.rb +24 -3
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ODI2ZDdkYWJmMDQ3ZGZiMDQ1ZGI3NDljOTM0ZGY2ZDhiMGM1YWE2Mw==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ODNjY2YyZTA5ODcwMjU3ZjNmODljYjY1MzM5YTcxODYwZjgzNTcyMA==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MDYxYmQ5ZjAyZjYyNzc4MzczNjEwZTdjMWNhOGI4ZWFkODI0OWJjMzVjNGJk
|
10
|
+
ODYyMTQ2ZmVlMmUwZDNkMDE5NGY2NTZjOTgyYWQ5MGMzOGFhYTEzMjhhN2Qy
|
11
|
+
ZDVlY2JkM2E0N2U2NTEzYzA2ZDJkYTZjYjJkODUwYTRjZjI5YmE=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
OTliM2MyMjk3ZmJmNzIxY2I3MjBmYWJjM2IzNWEwNThmN2I0Nzc2NjcxMmQ1
|
14
|
+
MDNhNzFmNjk1YjdlMDIzZjkyYTA0ZGNlYTY1MWFiOThmZDAzYzlkM2IxZWQw
|
15
|
+
OTg5NmRmMTRkNzBkNzMxNjhiMWU4MzEwMzE4ZTMzMGQ5OGU1YTc=
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.4.0
|
data/lib/ruby-box/folder.rb
CHANGED
@@ -2,6 +2,7 @@ module RubyBox
|
|
2
2
|
class Folder < Item
|
3
3
|
|
4
4
|
has_many :discussions
|
5
|
+
has_many :collaborations
|
5
6
|
has_many_paginated :items
|
6
7
|
|
7
8
|
def files(name=nil, item_limit=100, offset=0, fields=nil)
|
@@ -12,10 +13,6 @@ module RubyBox
|
|
12
13
|
items_by_type(RubyBox::Folder, name, item_limit, offset, fields)
|
13
14
|
end
|
14
15
|
|
15
|
-
def collaborations(item_limit=100, offset=0, fields=nil)
|
16
|
-
items_by_type(RubyBox::Collaboration, nil, item_limit, offset, fields)
|
17
|
-
end
|
18
|
-
|
19
16
|
def upload_file(filename, data)
|
20
17
|
file = RubyBox::File.new(@session, {
|
21
18
|
'name' => filename,
|
@@ -42,6 +39,18 @@ module RubyBox
|
|
42
39
|
RubyBox::Folder.new(@session, resp)
|
43
40
|
end
|
44
41
|
|
42
|
+
# see http://developers.box.com/docs/#collaborations-collaboration-object
|
43
|
+
# for a list of valid roles.
|
44
|
+
def create_collaboration(email, role=:viewer)
|
45
|
+
collaboration = RubyBox::Collaboration.new(@session, {
|
46
|
+
'item' => {'id' => id, 'type' => type},
|
47
|
+
'accessible_by' => {'login' => email},
|
48
|
+
'role' => role.to_s
|
49
|
+
})
|
50
|
+
|
51
|
+
collaboration.create
|
52
|
+
end
|
53
|
+
|
45
54
|
private
|
46
55
|
|
47
56
|
def resource_name
|
data/lib/ruby-box/item.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'time'
|
2
|
+
require 'addressable/uri'
|
2
3
|
|
3
4
|
module RubyBox
|
4
5
|
class Item
|
@@ -35,8 +36,19 @@ module RubyBox
|
|
35
36
|
self
|
36
37
|
end
|
37
38
|
|
38
|
-
def
|
39
|
+
def create
|
40
|
+
url = "#{RubyBox::API_URL}/#{resource_name}"
|
41
|
+
uri = URI.parse(url)
|
42
|
+
request = Net::HTTP::Post.new( uri.request_uri )
|
43
|
+
request.body = JSON.dump(@raw_item)
|
44
|
+
resp = @session.request(uri, request)
|
45
|
+
@raw_item = resp
|
46
|
+
self
|
47
|
+
end
|
48
|
+
|
49
|
+
def delete(opts={})
|
39
50
|
url = "#{RubyBox::API_URL}/#{resource_name}/#{id}"
|
51
|
+
url = "#{url}#{Addressable::URI.new(:query_values => opts).to_s}" unless opts.keys.empty?
|
40
52
|
resp = @session.delete( url )
|
41
53
|
end
|
42
54
|
|
data/lib/ruby-box/session.rb
CHANGED
@@ -13,6 +13,7 @@ module RubyBox
|
|
13
13
|
if opts[:client_id]
|
14
14
|
@oauth2_client = OAuth2::Client.new(opts[:client_id], opts[:client_secret], OAUTH2_URLS.dup)
|
15
15
|
@access_token = OAuth2::AccessToken.new(@oauth2_client, opts[:access_token]) if opts[:access_token]
|
16
|
+
@refresh_token = opts[:refresh_token]
|
16
17
|
else # Support legacy API for historical reasons.
|
17
18
|
@api_key = opts[:api_key]
|
18
19
|
@auth_token = opts[:auth_token]
|
@@ -49,7 +50,7 @@ module RubyBox
|
|
49
50
|
resp = request( uri, request, raw )
|
50
51
|
end
|
51
52
|
|
52
|
-
def request(uri, request, raw=false)
|
53
|
+
def request(uri, request, raw=false, retries=0)
|
53
54
|
|
54
55
|
http = Net::HTTP.new(uri.host, uri.port)
|
55
56
|
http.use_ssl = true
|
@@ -66,6 +67,13 @@ module RubyBox
|
|
66
67
|
if response.is_a? Net::HTTPNotFound
|
67
68
|
raise RubyBox::ObjectNotFound
|
68
69
|
end
|
70
|
+
|
71
|
+
# Got unauthorized (401) status, try to refresh the token
|
72
|
+
if response.code.to_i == 401 and @refresh_token and retries == 0
|
73
|
+
refresh_token(@refresh_token)
|
74
|
+
request(uri, request, raw, retries + 1)
|
75
|
+
end
|
76
|
+
|
69
77
|
handle_errors( response.code.to_i, response.body, raw )
|
70
78
|
end
|
71
79
|
|
@@ -91,10 +99,15 @@ module RubyBox
|
|
91
99
|
msg = body.nil? || body.empty? ? "no data returned" : body
|
92
100
|
parsed_body = { "message" => msg }
|
93
101
|
end
|
102
|
+
|
103
|
+
# status is used to determine whether
|
104
|
+
# we need to refresh the access token.
|
105
|
+
parsed_body["status"] = status
|
106
|
+
|
94
107
|
case status / 100
|
95
108
|
when 4
|
96
109
|
raise(RubyBox::ItemNameInUse.new(parsed_body), parsed_body["message"]) if parsed_body["code"] == "item_name_in_use"
|
97
|
-
raise(RubyBox::AuthError.new(parsed_body), parsed_body["message"]) if parsed_body["code"] == "unauthorized" || status == 401
|
110
|
+
raise(RubyBox::AuthError.new(parsed_body), parsed_body["message"]) if parsed_body["code"] == "unauthorized" || status == 401
|
98
111
|
raise(RubyBox::RequestError.new(parsed_body), parsed_body["message"])
|
99
112
|
when 5
|
100
113
|
raise RubyBox::ServerError, parsed_body["message"]
|
data/ruby-box.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "ruby-box"
|
8
|
-
s.version = "1.
|
8
|
+
s.version = "1.4.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Attachments.me"]
|
12
|
-
s.date = "2013-06-
|
12
|
+
s.date = "2013-06-21"
|
13
13
|
s.description = "ruby gem for box.com 2.0 api"
|
14
14
|
s.email = "ben@attachments.me"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -64,6 +64,7 @@ Gem::Specification.new do |s|
|
|
64
64
|
s.add_runtime_dependency(%q<multipart-post>, [">= 0"])
|
65
65
|
s.add_runtime_dependency(%q<oauth2>, [">= 0"])
|
66
66
|
s.add_runtime_dependency(%q<json>, [">= 0"])
|
67
|
+
s.add_runtime_dependency(%q<addressable>, [">= 0"])
|
67
68
|
s.add_development_dependency(%q<rspec>, [">= 0"])
|
68
69
|
s.add_development_dependency(%q<bundler>, [">= 0"])
|
69
70
|
s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
|
@@ -72,6 +73,7 @@ Gem::Specification.new do |s|
|
|
72
73
|
s.add_dependency(%q<multipart-post>, [">= 0"])
|
73
74
|
s.add_dependency(%q<oauth2>, [">= 0"])
|
74
75
|
s.add_dependency(%q<json>, [">= 0"])
|
76
|
+
s.add_dependency(%q<addressable>, [">= 0"])
|
75
77
|
s.add_dependency(%q<rspec>, [">= 0"])
|
76
78
|
s.add_dependency(%q<bundler>, [">= 0"])
|
77
79
|
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
@@ -81,6 +83,7 @@ Gem::Specification.new do |s|
|
|
81
83
|
s.add_dependency(%q<multipart-post>, [">= 0"])
|
82
84
|
s.add_dependency(%q<oauth2>, [">= 0"])
|
83
85
|
s.add_dependency(%q<json>, [">= 0"])
|
86
|
+
s.add_dependency(%q<addressable>, [">= 0"])
|
84
87
|
s.add_dependency(%q<rspec>, [">= 0"])
|
85
88
|
s.add_dependency(%q<bundler>, [">= 0"])
|
86
89
|
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
data/spec/folder_spec.rb
CHANGED
@@ -122,13 +122,4 @@ describe RubyBox::Folder do
|
|
122
122
|
end
|
123
123
|
end
|
124
124
|
|
125
|
-
describe '#collaborations' do
|
126
|
-
it "should only return items of type Collaboration" do
|
127
|
-
RubyBox::Session.any_instance.stub(:request) { @items.pop }
|
128
|
-
session = RubyBox::Session.new
|
129
|
-
collaborations = RubyBox::Folder.new(session, {'id' => 1}).collaborations
|
130
|
-
collaborations.count.should == 1
|
131
|
-
collaborations.first.kind_of?(RubyBox::Collaboration).should == true
|
132
|
-
end
|
133
|
-
end
|
134
125
|
end
|
data/spec/integration_spec.rb
CHANGED
@@ -27,7 +27,7 @@ describe RubyBox, :skip => true do
|
|
27
27
|
after do
|
28
28
|
File.delete('./spec/fixtures/遠志教授.jpg')
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
it "raises an AuthError if not client auth fails" do
|
32
32
|
session = RubyBox::Session.new({
|
33
33
|
api_key: 'bad-key',
|
@@ -112,7 +112,6 @@ describe RubyBox, :skip => true do
|
|
112
112
|
end
|
113
113
|
|
114
114
|
describe RubyBox::Client do
|
115
|
-
|
116
115
|
describe '#put_data' do
|
117
116
|
it "should update an existing file" do
|
118
117
|
utf8_file_name = '遠志教授.jpg'
|
@@ -141,6 +140,14 @@ describe RubyBox, :skip => true do
|
|
141
140
|
end
|
142
141
|
end
|
143
142
|
|
143
|
+
describe '#delete folder' do
|
144
|
+
it "should be able to recursively delete the contents of a folder" do
|
145
|
+
folder = @client.create_folder('/ruby-box_gem_testing/delete_test')
|
146
|
+
@client.create_folder('/ruby-box_gem_testing/delete_test/subfolder')
|
147
|
+
folder.delete(recursive: 'true')
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
144
151
|
describe '#get_file_info' do
|
145
152
|
it "returns meta information for a file" do
|
146
153
|
file = @client.file( '/ruby-box_gem_testing/cool stuff/кузнецкий_105_а_№2.test' )
|
@@ -176,6 +183,20 @@ describe RubyBox, :skip => true do
|
|
176
183
|
data.should eq nil
|
177
184
|
end
|
178
185
|
end
|
186
|
+
|
187
|
+
describe '#collaborations' do
|
188
|
+
it 'should be able to create and list collaborations on a folder' do
|
189
|
+
folder = @client.create_folder('/ruby-box_gem_testing/collaboration_folder')
|
190
|
+
folder.create_collaboration('bencoe@gmail.com')
|
191
|
+
folder.create_collaboration('ben@attachments.me', 'editor')
|
192
|
+
collaborations = folder.collaborations.to_a
|
193
|
+
|
194
|
+
collaborations[0].role.should == 'viewer'
|
195
|
+
collaborations[1].role.should == 'editor'
|
196
|
+
collaborations.count.should == 2
|
197
|
+
|
198
|
+
folder.delete if folder
|
199
|
+
end
|
200
|
+
end
|
179
201
|
end
|
180
202
|
end
|
181
|
-
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-box
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Attachments.me
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-06-
|
11
|
+
date: 2013-06-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: multipart-post
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - ! '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: addressable
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: rspec
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|