Coder Social home page Coder Social logo

google-drive-ruby's Introduction

This is a Ruby library to read/write files/spreadsheets in Google Drive/Docs.

NOTE: This is NOT a library to create Google Drive App.

Migration from ver. 0.x.x / 1.x.x to to ver. 2.x.x

There are some incompatible API changes. See MIGRATING.md.

How to install

Add this line to your application's Gemfile:

gem 'google_drive'

And then execute:

$ bundle

Or install it yourself as:

$ gem install google_drive

If you need system wide installation, execute below:

$ sudo gem install google_drive

How to use

First, follow Step 1 and 2 of "Authorizing requests with OAuth 2.0" in this page to get a client ID and client secret for OAuth. Set "Application type" to "Other" in the form to create a client ID if you use GoogleDrive.saved_session method as in the example below.

Next, create a file config.json which contains the client ID and crient secret you got above, which looks like:

{
  "client_id": "xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
  "client_secret": "xxxxxxxxxxxxxxxxxxxxxxxx"
}

Note that when using the OAuth flow, the client will be able to access all files belonging to the the user who generated the token. To set permissions more specifically, you may want to consider using a service account. See full docs in the login_with_oauth method.

Example to read/write files in Google Drive:

require "google_drive"

# Creates a session. This will prompt the credential via command line for the
# first time and save it to config.json file for later usages.
session = GoogleDrive.saved_session("config.json")

# Gets list of remote files.
session.files.each do |file|
  p file.title
end

# Uploads a local file.
session.upload_from_file("/path/to/hello.txt", "hello.txt", convert: false)

# Downloads to a local file.
file = session.file_by_title("hello.txt")
file.download_to_file("/path/to/hello.txt")

# Updates content of the remote file.
file.update_from_file("/path/to/hello.txt")

Example to read/write spreadsheets:

require "google_drive"

# Creates a session. This will prompt the credential via command line for the
# first time and save it to config.json file for later usages.
session = GoogleDrive.saved_session("config.json")

# First worksheet of
# https://docs.google.com/spreadsheet/ccc?key=pz7XtlQC-PYx-jrVMJErTcg
# Or https://docs.google.com/a/someone.com/spreadsheets/d/pz7XtlQC-PYx-jrVMJErTcg/edit?usp=drive_web
ws = session.spreadsheet_by_key("pz7XtlQC-PYx-jrVMJErTcg").worksheets[0]

# Gets content of A2 cell.
p ws[2, 1]  #==> "hoge"

# Changes content of cells.
# Changes are not sent to the server until you call ws.save().
ws[2, 1] = "foo"
ws[2, 2] = "bar"
ws.save

# Dumps all cells.
(1..ws.num_rows).each do |row|
  (1..ws.num_cols).each do |col|
    p ws[row, col]
  end
end

# Yet another way to do so.
p ws.rows  #==> [["fuga", ""], ["foo", "bar]]

# Reloads the worksheet to get changes by other clients.
ws.reload

Full API documentation

API documentation in RubyDoc.info

Source code

Github

The license of this source is "New BSD Licence"

Supported environments

Ruby 2.0.0 or later. Checked with Ruby 2.3.0.

Author

Hiroshi Ichikawa

google-drive-ruby's People

Contributors

bkoski avatar bpot avatar cesare avatar deepblue avatar er1c avatar erebor avatar evgenyneu avatar gimite avatar grosser avatar hotchpotch avatar iamruinous avatar matiasfernandez avatar nalanj avatar naofumi-fujii avatar norgg avatar ocouronne avatar phuongnd08 avatar qen avatar robermorales avatar sirupsen avatar solenko avatar stympy avatar swhitt avatar tei1988 avatar timdiggins avatar tricknotes avatar wesgibbs avatar wiktorschmidt avatar wkang avatar yemartin 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.