Project

rb1drv

0.02
No release in over 3 years
Low commit activity in last 3 years
Ruby OneDrive Library
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.16
~> 10.0

Runtime

~> 0.62
~> 1.4
 Project Readme

Rb1drv

Build Status

Rb1drv is a Ruby SDK for Microsoft OneDrive service, providing a simple interface to access files inside OneDrive.

Rb1drv allows you to list directories, upload or download files, etc.

To use the command line application, install rb1drv-tools gem instead.

Further functionalities can be added to the library upon requests.

Rb1drv uses the latest Microsoft OAuth2 + Graph API at the time it is written, while there are not many other libraries available for reference (e.g. official OneDrive SDK still uses old OAuth2 API instead of new OAuth2 v2.0 API). Feel free to take this as an unoffical reference implementation and write your own SDK.

Installation

Add this line to your application's Gemfile:

gem 'rb1drv'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rb1drv

Usage

You will have to register a new application on Microsoft Application Registration Portal before using the API. Read more at OneDrive API Docs.

od = OneDrive.new('a2b3c4d5-your-app-id', 'C8V3{your-app-secret(', 'https://your-callback/url')

To start using OneDrive library, instanciate the class with your application information.


od.root
od.get('/Folder1')
od.get('/File1.avi')

Use OneDrive#root or OneDrive#get to get a drive item.


od.root.children
od.root.children.grep(OneDriveDir)

Use OneDriveDir#children to get contents of a directory.


od.get('/File1.avi').save_as('/tmp/test.avi', overwrite: true) do |event, stat|
  puts "Downloaded #{stat[:progress]} of #{stat[:total]}" if event == :progress
end

Use OneDriveFile#save_as to download a file.


od.get('/Folder1').upload('/tmp/test.avi', overwrite: true, target_name: 'party.avi') do |event, stat|
  puts "Uploading #{stat[:progress]} of #{stat[:total]} for segment #{stat[:from]}-#{stat[:to]}" if event == :progress
  puts "Uploaded segment #{stat[:from]}-#{stat[:to]}" if event == :finish_segment
end

Use OneDriveDir#upload to upload a file to target directory.

By default OneDriveDir cache #get and #children requests, but you can disable it globally

od
folder = od.folder('/my_folder') # will be OneDriveDir instance
folder.get('some_file.mp3') # will make API request
folder.get('some_file.mp3') # will make API request again

or locally for particular OneDriveDir instance

folder = od.folder('/my_folder') # will be OneDriveDir instance
folder.skip_cache = true
folder.get('some_file.mp3') # will make API request
folder.get('some_file.mp3') # will make API request again

Also you can clear cache for particular OneDriveDir instance using #clear_cache! method

folder = od.folder('/my_folder') # will be OneDriveDir instance
folder.get('some_file.mp3') # will make API request
folder.get('some_file.mp3') # will return cached object
folder.clear_cache!
folder.get('some_file.mp3') # will make API request again

by default OneDrive returns hash with error when request failed (except file not found - in that case it returns instance of OneDrive404)

but you can force library to raise exception instead

Rb1drv.raise_on_failed_request = true
begin
  od.get('/invalid:folder2')
rescue Rb1drv::Errors::ApiError => e
  puts e.message # "Resource not found for the segment 'rootfolder2'."
  puts e.code # "BadRequest"
end

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/msg7086/rb1drv.

License

The gem is available as open source under the terms of the MIT License.