ruby-box 1.0.0 → 1.0.1

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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ODI5MWRhMWMwNjYwMDFhN2QyYTg3YzM1OTJhMWE2ZDM5NjMzODNjOQ==
4
+ MzQwYmEzNDcwZTkzZDk3MTMzNjMwZDFhODY5NjM2MjllZmVjOWEyNQ==
5
5
  data.tar.gz: !binary |-
6
- YTM1OGYwMmMyOGU0Y2EyZmU0NGFjYWUyMTM0ZjQ2NDM0ZDllMmVhYg==
6
+ YjQ3Yzc4YjgzZmRiOTdhMjE4NWM5NTJiYmY1MDg1YjkyMDlhMDM2Yw==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- NjgxZWMxODc2MzIyODg4YTAyMzIzMzg4NGI4MTM4NzdkYmFjNTBhNGFlYmY2
10
- MjAxOGU0MmU4ZGYyYWU1MjQ4MWYzNWYxMTcyYmFlYWM3ZDgxMWFhMTcyZTQ3
11
- OGMzYTQzMWQzZjAwYTliOGMxMjMwNTcwMWVmNzA0NzQ5MzJmNmQ=
9
+ NmY5ZmE2ZDI0YTk0OWQzMGU4ZDNkOTZiM2I1ZTdlZmZmODNiNGFmYjQxM2Rm
10
+ YjQ3ZTFjYjI4Nzc2MTlkOGQ5NjA2OGExNzBhMmMzYzYxMWZhYjM4YjMzNTJm
11
+ Njg0YWRlYWM5ZTU3MDVkZThkOGJjZThhOTBiZTNmMjcxZGFkYmI=
12
12
  data.tar.gz: !binary |-
13
- YzhkODAxM2Q2YjZlMWVlMWE5YjAyNzFhMTcwOTA5YmE0MmQ5MzE0NmI0ZTMx
14
- YmQxMTM4YTUyZjQ3YzJhM2NhNWJjMTAwZjk0MmNiY2I1NDRhODE4MjA1ZTNi
15
- NmZmOGMyYTcxZmViOGZiZjQ3NjE2YzlkMTk4ODc1OTY2YmM2NDY=
13
+ YWQwNTliOTU4NTQ2MTUzODA2MTA2ODNiOTBiOGM2ZDY0YmY2YTA3OTIwZmRl
14
+ ZWFjOGVjNjU3MzgyMjgwN2Y2NDViYjhmNmY2YTNhMmYxNTVjZDI4MzgzZTU5
15
+ ODFkZTFiMmQ1NzliZmM2YWQ4OGYwOTcyN2YwYmE0MmNjYmM2MGI=
@@ -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.0
1
+ 1.0.1
@@ -1,11 +1,6 @@
1
1
  module RubyBox
2
2
  class Discussion < Item
3
-
4
- def comments
5
- url = "#{RubyBox::API_URL}/#{resource_name}/#{id}/comments"
6
- resp = @session.get( url )
7
- resp['entries'].map {|i| Comment.new(@session, i)}
8
- end
3
+ has_many :comments
9
4
 
10
5
  private
11
6
 
@@ -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)
@@ -1,18 +1,8 @@
1
1
  module RubyBox
2
2
  class Folder < Item
3
- def items(item_limit=100, offset=0)
4
- Enumerator.new do |yielder|
5
- while true
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,
@@ -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
@@ -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, { :redirect_uri => @redirect_uri, :token_method => :post })
30
- @access_token.token
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
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "ruby-box"
8
- s.version = "1.0.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"]
@@ -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 }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-box
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamin Coe