Code Monkey home page Code Monkey logo

ruby-box's Introduction

ruby-box

Mainted by: Attachments.me

RubyBox provides a simple, chainable, feature-rich client for Box's 2.0 API.

Authorization

RubyBox uses Box's OAuth2 Implementaton, Here are the steps involved in authorizing a client:

1) Get the authorization url.

require 'ruby-box'

session = RubyBox::Session.new({
  client_id: 'your-client-id',
  client_secret: 'your-client-secret'
})

authorize_url = session.authorize_url('https://redirect-url-in-app-settings')

2) After redirecting to the authorize_url, exchange the code given for an access_token

@token = session.get_access_token('code-returned-to-redirect_url')
p '@token.token' # the access token.
p '@token.refresh_token' # token that can be exchanged for a new access_token once the access_token expires.

# refreshing token.
@token = session.refresh_token('refresh-token-string')

3) Create a client using a session initialized with the access_token.

require 'ruby-box'

session = RubyBox::Session.new({
  client_id: 'your-client-id',
  client_secret: 'your-client-secret',
  access_token: 'access-token'
})

client = RubyBox::Client.new(session)

Usage

Once you've created a client, you can start interacting with the Box API. What follows are some basic examples of RubyBox's usage:

Folders

  • Listing items in a folder:
files = client.folder('/image_folder').files # all files in a folder.
folders = client.root_folder.folders # all folders in the root directory.
files_and_folders = client.folder('files').items # all files and folders in /files
  • Creating a folder:
client.folder('image_folder').create_subfolder('subfolder')
  • Setting the description on a folder:
folder = client.folder('image_folder')
folder.description = 'Description on Folder'
folder.update
  • Listing the comments in a discussion surrounding a folder.
folder = client.folder('image_folder')
discussion = folder.discussions.first
discussion.comments.each {|comment| p comment.message}

Files

  • Fetching a file's meta information.
file = client.file('/image_folder/an-image.jpg')
p file.name
p file.created_at
  • Uploading a file to a folder.
file = client.upload_file('./LICENSE.txt', '/license_folder')
  • Downloading a file.
f = open('./LOCAL.txt', 'w+')
f.write( client.file('/license_folder/LICENSE.txt').download )
f.close()
  • Deleting a file.
client.file('/license_folder/LICENSE.txt').delete
  • Displaying comments on a file.
comments = client.file('/image_folder/an-image.jpg').comments
comments.each do |comment|
    p comment.message
end

Search

You can use RubyBox's search method to return files and folders that match a given query.

items = client.search('image')
items.each do |item|
    p "type=#{item.type} name=#{item.name}"
end

Events

You can use RubyBox's event_response method to return an EventResponse that can be used to process any incoming events.

eresp = client.event_response
eresp.chunk_size
eresp.next_stream_position
eresp.events.each do |ev|
  p "type=#{ev.event_id} type=#{ev.event_type} user=#{ev.created_by.name}"
end

Contributors

  • Benjamin Coe
  • Larry Kang
  • Dan Reed
  • Jesse Miller

Contributing to ruby-box

RubyBox does not yet support all of Box's API Version 2.0 functionality, be liberal with your contributions.

  • Rename account.example to account.yml and fill in your Box credentials
  • Type bundle install
  • Type rake.. tests should pass
  • Add a failing test
  • Make it pass
  • Submit a pull request

Copyright

Copyright (c) 2012 Attachments.me. See LICENSE.txt for further details.

ruby-box's People

Contributors

jessemiller avatar tmhedberg avatar zenchild avatar

Watchers

 avatar  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.