From 00b30cb453e428467644de712a3e402d02968d82 Mon Sep 17 00:00:00 2001 From: Xiao Date: Fri, 13 Jun 2014 14:32:19 -0400 Subject: [PATCH 1/5] fix issue with session not handling errors when response.body is empty --- lib/ruby-box/session.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/ruby-box/session.rb b/lib/ruby-box/session.rb index 8cf96e6..cd7aaf9 100644 --- a/lib/ruby-box/session.rb +++ b/lib/ruby-box/session.rb @@ -111,6 +111,7 @@ def handle_errors( response, raw ) body = response.body begin parsed_body = JSON.parse(body) + raise if body.empty? || body.nil? rescue msg = body.nil? || body.empty? ? "no data returned" : body parsed_body = { "message" => msg } From 0b61d2a50169985a40584da7a8c6b3610c044391 Mon Sep 17 00:00:00 2001 From: Tristan Peralta Date: Mon, 30 Jun 2014 22:25:50 +0800 Subject: [PATCH 2/5] Fix typo --- README.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index a432952..8ac0f8e 100644 --- a/README.markdown +++ b/README.markdown @@ -319,7 +319,7 @@ An array of Ruby:Box users in an enterprise (Supports Filtering, Limit and Offse users = client.users ``` -* Remeber the API filters "name" and "login" by the start of the string. ie: to get "sean+awesome@gmail.com" an approriate filter term would be "sean" +* Remember the API filters "name" and "login" by the start of the string. ie: to get "sean+awesome@gmail.com" an approriate filter term would be "sean" ```ruby users = client.users("sean" , 10 , 1) From 79d7565b306fb104afc73562c52e8934af50943b Mon Sep 17 00:00:00 2001 From: Sergey Tyatin <700@2rba.com> Date: Thu, 24 Jul 2014 15:03:22 +0300 Subject: [PATCH 3/5] token refresh fix --- lib/ruby-box/session.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/ruby-box/session.rb b/lib/ruby-box/session.rb index 8cf96e6..cbffc30 100644 --- a/lib/ruby-box/session.rb +++ b/lib/ruby-box/session.rb @@ -64,14 +64,17 @@ def request(uri, request, raw=false, retries=0) #http.set_debug_output($stdout) if @access_token + request.delete('Authorization') request.add_field('Authorization', "Bearer #{@access_token.token}") else + request.delete('Authorization') request.add_field('Authorization', build_auth_header) end - - request.add_field('As-User', "#{@as_user}") if @as_user - + if @as_user + request.delete('As-User') + request.add_field('As-User', "#{@as_user}") + end response = http.request(request) if response.is_a? Net::HTTPNotFound From e6425e3ab32ed1d4e1eee756dfbd7613a8a17edf Mon Sep 17 00:00:00 2001 From: Tristan Peralta Date: Mon, 28 Jul 2014 16:44:57 +0800 Subject: [PATCH 4/5] Remove trailing whitespaces --- lib/ruby-box/client.rb | 4 +-- lib/ruby-box/comment.rb | 4 +-- lib/ruby-box/event_response.rb | 2 +- lib/ruby-box/exceptions.rb | 2 +- lib/ruby-box/file.rb | 2 +- lib/ruby-box/folder.rb | 2 +- lib/ruby-box/item.rb | 4 +-- lib/ruby-box/shared_link.rb | 6 ++-- lib/ruby-box/user.rb | 8 +++--- spec/client_spec.rb | 4 +-- spec/file_spec.rb | 4 +-- spec/folder_spec.rb | 6 ++-- spec/integration_spec.rb | 50 +++++++++++++++++----------------- spec/item_spec.rb | 2 +- 14 files changed, 50 insertions(+), 50 deletions(-) diff --git a/lib/ruby-box/client.rb b/lib/ruby-box/client.rb index 22a66e8..f3ad17f 100644 --- a/lib/ruby-box/client.rb +++ b/lib/ruby-box/client.rb @@ -76,7 +76,7 @@ def create_folder(path) folder = root_folder folder_names = split_path(path) folder_names.each do |folder_name| - new_folder = folder.folders(folder_name).first + new_folder = folder.folders(folder_name).first if !new_folder begin new_folder = folder.create_subfolder(folder_name) @@ -158,6 +158,6 @@ def fmt_events_args(stream_position, stream_type, limit) limit = limit.kind_of?(Fixnum) ? limit : 100 "stream_position=#{stream_position}&stream_type=#{stream_type}&limit=#{limit}" end - + end end diff --git a/lib/ruby-box/comment.rb b/lib/ruby-box/comment.rb index ce6e594..0103bc1 100644 --- a/lib/ruby-box/comment.rb +++ b/lib/ruby-box/comment.rb @@ -1,6 +1,6 @@ module RubyBox class Comment < Item - + private def resource_name @@ -11,4 +11,4 @@ def has_mini_format? true end end -end \ No newline at end of file +end diff --git a/lib/ruby-box/event_response.rb b/lib/ruby-box/event_response.rb index 60e8985..0f5f0c4 100644 --- a/lib/ruby-box/event_response.rb +++ b/lib/ruby-box/event_response.rb @@ -12,6 +12,6 @@ def events def resource_name 'events' end - + end end diff --git a/lib/ruby-box/exceptions.rb b/lib/ruby-box/exceptions.rb index 0c2f68e..e23fc1c 100644 --- a/lib/ruby-box/exceptions.rb +++ b/lib/ruby-box/exceptions.rb @@ -1,4 +1,4 @@ -module RubyBox +module RubyBox class RubyBoxError < StandardError attr_accessor :body, :status diff --git a/lib/ruby-box/file.rb b/lib/ruby-box/file.rb index 9344f96..acc2d6c 100644 --- a/lib/ruby-box/file.rb +++ b/lib/ruby-box/file.rb @@ -78,7 +78,7 @@ def create_comment(message) def file_content_url "#{RubyBox::API_URL}/#{resource_name}/#{id}/content" end - + def resource_name 'files' diff --git a/lib/ruby-box/folder.rb b/lib/ruby-box/folder.rb index 66908e6..28a8ece 100644 --- a/lib/ruby-box/folder.rb +++ b/lib/ruby-box/folder.rb @@ -22,7 +22,7 @@ def upload_file(filename, data, overwrite=true) begin resp = file.upload_content(data) #write a new file. If there is a conflict, update the conflicted file. rescue RubyBox::ItemNameInUse => e - + # if overwrite flag is false, simply raise exception. raise e unless overwrite diff --git a/lib/ruby-box/item.rb b/lib/ruby-box/item.rb index e0b447c..d9cb510 100644 --- a/lib/ruby-box/item.rb +++ b/lib/ruby-box/item.rb @@ -75,12 +75,12 @@ def method_missing(method, *args, &block) # Support has many and paginated has many relationships. return many(key) if @@has_many.include?(key) return paginated(key, args[0] || 100, args[1] || 0, args[2]) if @@has_many_paginated.include?(key) - + # update @raw_item hash if this appears to be a setter. setter = method.to_s.end_with?('=') key = key[0...-1] if setter @raw_item[key] = args[0] if setter and update_fields.include?(key) - + # we may have a mini version of the object loaded, fix this. reload_meta if @raw_item[key].nil? and has_mini_format? diff --git a/lib/ruby-box/shared_link.rb b/lib/ruby-box/shared_link.rb index 72a74f3..692cba2 100644 --- a/lib/ruby-box/shared_link.rb +++ b/lib/ruby-box/shared_link.rb @@ -1,10 +1,10 @@ module RubyBox class SharedLink < Item - + private - + def has_mini_format? false end end -end \ No newline at end of file +end diff --git a/lib/ruby-box/user.rb b/lib/ruby-box/user.rb index 94121aa..b0839f7 100644 --- a/lib/ruby-box/user.rb +++ b/lib/ruby-box/user.rb @@ -1,16 +1,16 @@ module RubyBox class User < Item - + def enterprise resp = @session.get( "#{RubyBox::API_URL}/users/#{id}?fields=enterprise" ) resp["enterprise"] end - + private def resource_name 'users' end - + end -end \ No newline at end of file +end diff --git a/spec/client_spec.rb b/spec/client_spec.rb index c12b0d4..62a9a40 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -37,7 +37,7 @@ client.split_path('foo/bar/').should == ['foo', 'bar'] end end - + describe '#create_folder' do it 'doesnt call folder.create_folder if the folder exists' do client = RubyBox::Client.new(@session) @@ -49,7 +49,7 @@ result = client.create_folder( '/test0') result.should == test_folder end - + it 'calls folder.create_folder if the folder does not exist' do client = RubyBox::Client.new(@session) mock_root_folder = mock( Object ) diff --git a/spec/file_spec.rb b/spec/file_spec.rb index e7f089b..61a0f58 100644 --- a/spec/file_spec.rb +++ b/spec/file_spec.rb @@ -51,7 +51,7 @@ file.name = 'Funky Monkey.jpg' file.description = 'a new description' file.name.should == 'Funky Monkey.jpg' - file.description.should == 'a new description' + file.description.should == 'a new description' end it 'should not update files raw_item hash for keys not in update_fields' do @@ -107,4 +107,4 @@ end end -end \ No newline at end of file +end diff --git a/spec/folder_spec.rb b/spec/folder_spec.rb index 099d7ac..fd1bdca 100644 --- a/spec/folder_spec.rb +++ b/spec/folder_spec.rb @@ -34,7 +34,7 @@ it "compares name in a case insensitive manner" do items = [ JSON.parse('{ "total_count": 4, "entries": [ { "type": "folder", "id": "409047867", "sequence_id": "1", "etag": "1", "name": "Here\'s your folder" }, { "type": "file", "id": "409042867", "sequence_id": "1", "etag": "1", "name": "A choice file" } ], "offset": "0", "limit": "2"}'), - JSON.parse('{ "total_count": 4, "entries": [ { "type": "folder", "id": "409047868", "sequence_id": "1", "etag": "1", "name": "Here\'s another folder" }, { "type": "file", "id": "409042810", "sequence_id": "1", "etag": "1", "name": "A choice file" } ], "offset": "2", "limit": "2"}') + JSON.parse('{ "total_count": 4, "entries": [ { "type": "folder", "id": "409047868", "sequence_id": "1", "etag": "1", "name": "Here\'s another folder" }, { "type": "file", "id": "409042810", "sequence_id": "1", "etag": "1", "name": "A choice file" } ], "offset": "2", "limit": "2"}') ] RubyBox::Session.any_instance.stub(:request) { items.pop } @@ -96,7 +96,7 @@ it "should allow you to filter files by name" do items = [ JSON.parse('{ "total_count": 4, "entries": [ { "type": "folder", "id": "409047867", "sequence_id": "1", "etag": "1", "name": "Here\'s your folder" }, { "type": "file", "id": "409042867", "sequence_id": "1", "etag": "1", "name": "A choice file" } ], "offset": "0", "limit": "2"}'), - JSON.parse('{ "total_count": 4, "entries": [ { "type": "folder", "id": "409047868", "sequence_id": "1", "etag": "1", "name": "Here\'s another folder" }, { "type": "file", "id": "409042810", "sequence_id": "1", "etag": "1", "name": "A choice file" } ], "offset": "2", "limit": "2"}') + JSON.parse('{ "total_count": 4, "entries": [ { "type": "folder", "id": "409047868", "sequence_id": "1", "etag": "1", "name": "Here\'s another folder" }, { "type": "file", "id": "409042810", "sequence_id": "1", "etag": "1", "name": "A choice file" } ], "offset": "2", "limit": "2"}') ] RubyBox::Session.any_instance.stub(:request) { items.pop } @@ -108,7 +108,7 @@ items = [ JSON.parse('{ "total_count": 4, "entries": [ { "type": "folder", "id": "409047867", "sequence_id": "1", "etag": "1", "name": "Here\'s your folder" }, { "type": "file", "id": "409042867", "sequence_id": "1", "etag": "1", "name": "A choice file" } ], "offset": "0", "limit": "2"}'), - JSON.parse('{ "total_count": 4, "entries": [ { "type": "folder", "id": "409047868", "sequence_id": "1", "etag": "1", "name": "Here\'s another folder" }, { "type": "file", "id": "409042810", "sequence_id": "1", "etag": "1", "name": "A choice file" } ], "offset": "2", "limit": "2"}') + JSON.parse('{ "total_count": 4, "entries": [ { "type": "folder", "id": "409047868", "sequence_id": "1", "etag": "1", "name": "Here\'s another folder" }, { "type": "file", "id": "409042810", "sequence_id": "1", "etag": "1", "name": "A choice file" } ], "offset": "2", "limit": "2"}') ] # should return no files. diff --git a/spec/integration_spec.rb b/spec/integration_spec.rb index 8ffc74b..d63d51d 100644 --- a/spec/integration_spec.rb +++ b/spec/integration_spec.rb @@ -9,7 +9,7 @@ before do WebMock.allow_net_connect! - + @session = RubyBox::Session.new({ api_key: ACCOUNT['api_key'], auth_token: ACCOUNT['auth_token'] @@ -39,7 +39,7 @@ lambda {@bad_client.root_folder}.should raise_error( RubyBox::AuthError ) end - + it "raises a RequestError if a badly formed request detected by the server" do stub_request(:get, "https://api.box.com/2.0/folders/0").to_return(:status => 401, :body => '{"type": "error", "status": 401, "message": "baddd req"}', :headers => {}) lambda {@client.root_folder}.should raise_error( RubyBox::AuthError ) @@ -57,7 +57,7 @@ it "raises a ServerError if the server raises a 500 error" do stub_request(:get, "https://api.box.com/2.0/folders/0").to_return(:status => 503, :body => '{"type": "error", "status": 503, "message": "We messed up! - Box.com"}', :headers => {}) lambda {@client.root_folder}.should raise_error( RubyBox::ServerError ) - + # make sure status and body is # set on error object. begin @@ -75,30 +75,30 @@ items = @client.root_folder.items.to_a items.count.should == 6 items.any? do |item| - item.type == "folder" && + item.type == "folder" && item.id == "318810303" && item.name == "ruby-box_gem_testing" end.should == true end - + it "returns list of items in the folder id passed in" do folder = RubyBox::Folder.new(@session, {'id' => '318810303'}) items = folder.items.to_a items.count.should == 2 items.any? do |item| - item.type == "file" && + item.type == "file" && item.id == "2550686921" && item.name == "2513582219_03fb9b67db_b.jpg" end.should == true end end - + describe '#file' do it "finds the id of a file" do folder = RubyBox::Folder.new(@session, {'id' => '318810303'}) file = folder.files( '2513582219_03fb9b67db_b.jpg' ).first file.id.should == "2550686921" - end + end end describe '#files' do @@ -110,7 +110,7 @@ file.size.should == 593978 end end - + describe '#create' do it "creates a folder" do folder = @client.folder('/ruby-box_gem_testing') @@ -152,7 +152,7 @@ fdata = File.open( 'spec/fixtures/' + utf8_file_name, 'rb' ) response = @client.upload_data('/ruby-box_gem_testing/cool stuff/遠志教授.jpg', fdata) fdata = File.open( 'spec/fixtures/' + utf8_file_name, 'rb' ) - + file = @client.upload_data('/ruby-box_gem_testing/cool stuff/遠志教授.jpg', fdata) file.name.should == '遠志教授.jpg' file.delete @@ -162,16 +162,16 @@ fdata = File.open( 'spec/fixtures/遠志教授.jpg', 'rb' ) file = @client.upload_data('/ruby-box_gem_testing/cool stuff/遠志教授.jpg', fdata) fdata = File.open( 'spec/fixtures/遠志教授.jpg', 'rb' ) - + expect{ @client.upload_data('/ruby-box_gem_testing/cool stuff/遠志教授.jpg', fdata, false) }.to raise_error(RubyBox::ItemNameInUse) file.delete end - + it "should upload a new file" do utf8_file_name = '遠志教授.jpg' file = @client.upload_file('spec/fixtures/' + utf8_file_name, '/ruby-box_gem_testing/cool stuff/') file.name.should == '遠志教授.jpg' - file.delete + file.delete end it "should allow a file to be uploaded by a folder id" do @@ -179,10 +179,10 @@ folder = @client.folder('/ruby-box_gem_testing/cool stuff/') file = @client.upload_file_by_folder_id('spec/fixtures/' + utf8_file_name, folder.id) file.name.should == '遠志教授.jpg' - file.delete + file.delete end end - + describe '#create_folder' do it "creates a path that doesnt exist" do folder = @client.create_folder('/ruby-box_gem_testing/cool stuff/екузц/path1/path2') @@ -219,11 +219,11 @@ file_by_id.size.should == 14 end end - + describe '#stream' do it "should download file contents" do stream = @client.stream( '/ruby-box_gem_testing/cool stuff/кузнецкий_105_а_№2.test' ) - stream.read.should eq "Test more data" + stream.read.should eq "Test more data" end it "should execute content_length_proc lambda with filesize" do @@ -233,7 +233,7 @@ filesize.should == 14 } ) - stream.read.should eq "Test more data" + stream.read.should eq "Test more data" end end @@ -242,11 +242,11 @@ data = @client.download( '/ruby-box_gem_testing/cool stuff/кузнецкий_105_а_№2.test' ) data.should eq "Test more data" end - + it "returns nil for a nonexistent file" do data = @client.download( '/ruby-box_gem_testing/doesntexist' ) data.should eq nil - end + end end describe '#collaborations' do @@ -285,12 +285,12 @@ # share link was successfully created. file.shared_link.url.should match /https?:\/\/[\S]+/ - + # share link can be disabled. file.disable_shared_link file.shared_link.should == nil - file.delete + file.delete end end @@ -307,7 +307,7 @@ it "it copies a file to a folder when a folder id is passed in" do file = @client.upload_file('spec/fixtures/遠志教授.jpg', '/ruby-box_gem_testing/') folder = @client.folder('/ruby-box_gem_testing/cool stuff/') - + file.copy_to(folder.id) copied_file = @client.file('/ruby-box_gem_testing/cool stuff/遠志教授.jpg') @@ -322,7 +322,7 @@ it "it copies a file to a folder when a folder is passed in" do file = @client.upload_file('spec/fixtures/遠志教授.jpg', '/ruby-box_gem_testing/') folder = @client.folder('/ruby-box_gem_testing/cool stuff/') - + file.copy_to(folder) copied_file = @client.file('/ruby-box_gem_testing/cool stuff/遠志教授.jpg') @@ -337,7 +337,7 @@ it "allows file to be renamed when copied" do file = @client.upload_file('spec/fixtures/遠志教授.jpg', '/ruby-box_gem_testing/') folder = @client.folder('/ruby-box_gem_testing/cool stuff/') - + file.copy_to(folder, 'banana.jpg') copied_file = @client.file('/ruby-box_gem_testing/cool stuff/banana.jpg') diff --git a/spec/item_spec.rb b/spec/item_spec.rb index d4a0a14..5131b4a 100644 --- a/spec/item_spec.rb +++ b/spec/item_spec.rb @@ -11,7 +11,7 @@ end describe '#factory' do - + it 'creates an object from a web_link hash' do web_link = RubyBox::Item.factory(@session, { 'type' => 'web_link' From 7c709bc86490f9484f1ef2bd4643c8ed801f1f7e Mon Sep 17 00:00:00 2001 From: Tristan Peralta Date: Mon, 28 Jul 2014 17:18:28 +0800 Subject: [PATCH 5/5] Replace deprecated mock with double --- spec/client_spec.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/client_spec.rb b/spec/client_spec.rb index 62a9a40..f73d38c 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -41,8 +41,8 @@ describe '#create_folder' do it 'doesnt call folder.create_folder if the folder exists' do client = RubyBox::Client.new(@session) - mock_root_folder = mock( Object ) - test_folder = mock( Object ) + mock_root_folder = double + test_folder = double mock_root_folder.should_receive(:folders).and_return([test_folder]) mock_root_folder.should_not_receive(:create_subfolder) client.should_receive(:root_folder).and_return(mock_root_folder) @@ -52,8 +52,8 @@ it 'calls folder.create_folder if the folder does not exist' do client = RubyBox::Client.new(@session) - mock_root_folder = mock( Object ) - test_folder = mock( Object ) + mock_root_folder = double + test_folder = double mock_root_folder.should_receive(:folders).and_return([]) mock_root_folder.should_receive(:create_subfolder).and_return(test_folder) client.should_receive(:root_folder).and_return(mock_root_folder)