ruby-box 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/README.markdown +6 -1
- data/VERSION +1 -1
- data/lib/ruby-box/discussion.rb +1 -6
- data/lib/ruby-box/file.rb +2 -6
- data/lib/ruby-box/folder.rb +3 -19
- data/lib/ruby-box/item.rb +35 -0
- data/lib/ruby-box/session.rb +7 -4
- data/ruby-box.gemspec +1 -1
- data/spec/folder_spec.rb +10 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MzQwYmEzNDcwZTkzZDk3MTMzNjMwZDFhODY5NjM2MjllZmVjOWEyNQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YjQ3Yzc4YjgzZmRiOTdhMjE4NWM5NTJiYmY1MDg1YjkyMDlhMDM2Yw==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NmY5ZmE2ZDI0YTk0OWQzMGU4ZDNkOTZiM2I1ZTdlZmZmODNiNGFmYjQxM2Rm
|
10
|
+
YjQ3ZTFjYjI4Nzc2MTlkOGQ5NjA2OGExNzBhMmMzYzYxMWZhYjM4YjMzNTJm
|
11
|
+
Njg0YWRlYWM5ZTU3MDVkZThkOGJjZThhOTBiZTNmMjcxZGFkYmI=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
YWQwNTliOTU4NTQ2MTUzODA2MTA2ODNiOTBiOGM2ZDY0YmY2YTA3OTIwZmRl
|
14
|
+
ZWFjOGVjNjU3MzgyMjgwN2Y2NDViYjhmNmY2YTNhMmYxNTVjZDI4MzgzZTU5
|
15
|
+
ODFkZTFiMmQ1NzliZmM2YWQ4OGYwOTcyN2YwYmE0MmNjYmM2MGI=
|
data/README.markdown
CHANGED
@@ -26,7 +26,12 @@ authorize_url = session.authorize_url('https://redirect-url-in-app-settings')
|
|
26
26
|
__2)__ After redirecting to the authorize_url, exchange the _code_ given for an _access\_token_
|
27
27
|
|
28
28
|
```ruby
|
29
|
-
session.get_access_token('code-returned-to-redirect_url')
|
29
|
+
@token = session.get_access_token('code-returned-to-redirect_url')
|
30
|
+
p '@token.token' # the access token.
|
31
|
+
p '@token.refresh_token' # token that can be exchanged for a new access_token once the access_token expires.
|
32
|
+
|
33
|
+
# refreshing token.
|
34
|
+
@token = session.refresh_token('refresh-token-string')
|
30
35
|
```
|
31
36
|
|
32
37
|
__3)__ Create a client using a session initialized with the _access\_token_.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.
|
1
|
+
1.0.1
|
data/lib/ruby-box/discussion.rb
CHANGED
data/lib/ruby-box/file.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
module RubyBox
|
2
2
|
class File < Item
|
3
3
|
|
4
|
+
has_many :comments
|
5
|
+
|
4
6
|
def download
|
5
7
|
resp = stream.read
|
6
8
|
end
|
@@ -10,12 +12,6 @@ module RubyBox
|
|
10
12
|
@session.do_stream( url, opts )
|
11
13
|
end
|
12
14
|
|
13
|
-
def comments
|
14
|
-
url = "#{RubyBox::API_URL}/#{resource_name}/#{id}/comments"
|
15
|
-
resp = @session.get( url )
|
16
|
-
resp['entries'].map {|i| Comment.new(@session, i)}
|
17
|
-
end
|
18
|
-
|
19
15
|
def upload_content( data )
|
20
16
|
url = "#{RubyBox::UPLOAD_URL}/#{resource_name}/content"
|
21
17
|
uri = URI.parse(url)
|
data/lib/ruby-box/folder.rb
CHANGED
@@ -1,18 +1,8 @@
|
|
1
1
|
module RubyBox
|
2
2
|
class Folder < Item
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
url = "#{RubyBox::API_URL}/#{resource_name}/#{id}/items?limit=#{item_limit}&offset=#{offset}"
|
7
|
-
resp = @session.get( url )
|
8
|
-
resp['entries'].each do |entry|
|
9
|
-
yielder.yield(RubyBox::Item.factory(@session, entry))
|
10
|
-
end
|
11
|
-
offset += resp['entries'].count
|
12
|
-
break if resp['offset'].to_i + resp['limit'].to_i >= resp['total_count'].to_i
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
3
|
+
|
4
|
+
has_many :discussions
|
5
|
+
has_many_paginated :items
|
16
6
|
|
17
7
|
def files(name=nil, item_limit=100, offset=0)
|
18
8
|
items(item_limit, offset).select do |item|
|
@@ -26,12 +16,6 @@ module RubyBox
|
|
26
16
|
end
|
27
17
|
end
|
28
18
|
|
29
|
-
def discussions
|
30
|
-
url = "#{RubyBox::API_URL}/#{resource_name}/#{id}/discussions"
|
31
|
-
resp = @session.get( url )
|
32
|
-
resp['entries'].map {|i| Discussion.new(@session, i)}
|
33
|
-
end
|
34
|
-
|
35
19
|
def upload_file(filename, data)
|
36
20
|
file = RubyBox::File.new(@session, {
|
37
21
|
'name' => filename,
|
data/lib/ruby-box/item.rb
CHANGED
@@ -3,11 +3,22 @@ require 'time'
|
|
3
3
|
module RubyBox
|
4
4
|
class Item
|
5
5
|
|
6
|
+
@@has_many = []
|
7
|
+
@@has_many_paginated = []
|
8
|
+
|
6
9
|
def initialize( session, raw_item )
|
7
10
|
@session = session
|
8
11
|
@raw_item = raw_item
|
9
12
|
end
|
10
13
|
|
14
|
+
def self.has_many(*keys)
|
15
|
+
keys.each {|key| @@has_many << key.to_s}
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.has_many_paginated(*keys)
|
19
|
+
keys.each {|key| @@has_many_paginated << key.to_s}
|
20
|
+
end
|
21
|
+
|
11
22
|
def update
|
12
23
|
reload_meta unless etag
|
13
24
|
|
@@ -37,6 +48,10 @@ module RubyBox
|
|
37
48
|
|
38
49
|
def method_missing(method, *args, &block)
|
39
50
|
key = method.to_s
|
51
|
+
|
52
|
+
# Support has many and paginated has many relationships.
|
53
|
+
return many(key) if @@has_many.include?(key)
|
54
|
+
return paginated(key, args[0] || 100, args[1] || 0) if @@has_many_paginated.include?(key)
|
40
55
|
|
41
56
|
# update @raw_item hash if this appears to be a setter.
|
42
57
|
setter = method.to_s.end_with?('=')
|
@@ -75,6 +90,26 @@ module RubyBox
|
|
75
90
|
|
76
91
|
private
|
77
92
|
|
93
|
+
def many(key)
|
94
|
+
url = "#{RubyBox::API_URL}/#{resource_name}/#{id}/#{key}"
|
95
|
+
resp = @session.get( url )
|
96
|
+
resp['entries'].map {|i| RubyBox::Item.factory(@session, i)}
|
97
|
+
end
|
98
|
+
|
99
|
+
def paginated(key, item_limit=100, offset=0)
|
100
|
+
Enumerator.new do |yielder|
|
101
|
+
while true
|
102
|
+
url = "#{RubyBox::API_URL}/#{resource_name}/#{id}/#{key}?limit=#{item_limit}&offset=#{offset}"
|
103
|
+
resp = @session.get( url )
|
104
|
+
resp['entries'].each do |entry|
|
105
|
+
yielder.yield(RubyBox::Item.factory(@session, entry))
|
106
|
+
end
|
107
|
+
offset += resp['entries'].count
|
108
|
+
break if resp['offset'].to_i + resp['limit'].to_i >= resp['total_count'].to_i
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
78
113
|
def serialize
|
79
114
|
update_fields.inject({}) {|hash, field| hash[field] = @raw_item[field]; hash}
|
80
115
|
end
|
data/lib/ruby-box/session.rb
CHANGED
@@ -2,7 +2,6 @@ require 'oauth2'
|
|
2
2
|
|
3
3
|
module RubyBox
|
4
4
|
class Session
|
5
|
-
attr_accessor :api_key, :auth_token, :oauth2_client, :access_token
|
6
5
|
|
7
6
|
OAUTH2_URLS = {
|
8
7
|
:site => 'https://www.box.com',
|
@@ -13,7 +12,7 @@ module RubyBox
|
|
13
12
|
def initialize(opts={})
|
14
13
|
if opts[:client_id]
|
15
14
|
@oauth2_client = OAuth2::Client.new(opts[:client_id], opts[:client_secret], OAUTH2_URLS)
|
16
|
-
@access_token = OAuth2::AccessToken.new(oauth2_client, opts[:access_token]) if opts[:access_token]
|
15
|
+
@access_token = OAuth2::AccessToken.new(@oauth2_client, opts[:access_token]) if opts[:access_token]
|
17
16
|
else # Support legacy API for historical reasons.
|
18
17
|
@api_key = opts[:api_key]
|
19
18
|
@auth_token = opts[:auth_token]
|
@@ -26,8 +25,12 @@ module RubyBox
|
|
26
25
|
end
|
27
26
|
|
28
27
|
def get_access_token(code)
|
29
|
-
@access_token = @oauth2_client.auth_code.get_token(code
|
30
|
-
|
28
|
+
@access_token = @oauth2_client.auth_code.get_token(code)
|
29
|
+
end
|
30
|
+
|
31
|
+
def refresh_token(refresh_token)
|
32
|
+
refresh_access_token_obj = OAuth2::AccessToken.new(@oauth2_client, @access_token.token, {'refresh_token' => refresh_token})
|
33
|
+
@access_token = refresh_access_token_obj.refresh!
|
31
34
|
end
|
32
35
|
|
33
36
|
def build_auth_header
|
data/ruby-box.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "ruby-box"
|
8
|
-
s.version = "1.0.
|
8
|
+
s.version = "1.0.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Benjamin Coe", "Jesse Miller", "Larry Kang"]
|
data/spec/folder_spec.rb
CHANGED
@@ -89,6 +89,16 @@ describe RubyBox::Folder do
|
|
89
89
|
|
90
90
|
end
|
91
91
|
|
92
|
+
describe '#discussions' do
|
93
|
+
it "should return all the discussions surrounding a folder" do
|
94
|
+
item = JSON.parse('{ "id": "0000001", "total_count": 1, "entries": [ { "type": "discussion", "id": "409042867", "sequence_id": "1", "etag": "1", "name": "A choice file" } ], "offset": "0", "limit": "1"}')
|
95
|
+
RubyBox::Session.any_instance.stub(:request).and_return(item)
|
96
|
+
session = RubyBox::Session.new
|
97
|
+
item = RubyBox::Client.new(session).root_folder.discussions.first
|
98
|
+
item.kind_of?(RubyBox::Discussion).should == true
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
92
102
|
describe '#folders' do
|
93
103
|
it "should only return items of type folder" do
|
94
104
|
RubyBox::Session.any_instance.stub(:request) { @items.pop }
|