Code Monkey home page Code Monkey logo

rb1drv's Introduction

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.

rb1drv's People

Contributors

dependabot[bot] avatar gigorok avatar msg7086 avatar senid231 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.