Git Product home page Git Product logo

google-drive-ruby's Introduction

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

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

Incompatibility introduced in ver. 1.0.0

Ver. 1.0.0 is not 100% backward compatible with 0.3.x. Some methods have been removed. Especially, GoogleDrive.login has been removed, and you must use GoogleDrive.login_with_oauth instead, as in the example code below.

I recommend to switch to the new API. But if you somehow want to continue using the old API, you can keep using it by requiring “google_drive_v0” instead of “google_drive” and use GoogleDriveV0 class instead of GoogleDrive class. It should continue working at least until April 20, 2015 when Google stops support of Documents List API.

How to install

$ sudo gem install google_drive

How to use

First, follow “Create a client ID and client secret” in this page to get a client ID and client secret for OAuth.

Example to read/write files in Google Drive:

require "rubygems"
require "google/api_client"
require "google_drive"

# Authorizes with OAuth and gets an access token.
client = Google::APIClient.new
auth = client.authorization
auth.client_id = "YOUR CLIENT ID"
auth.client_secret = "YOUR CLIENT SECRET"
auth.scope =
    "https://www.googleapis.com/auth/drive " +
    "https://spreadsheets.google.com/feeds/"
auth.redirect_uri = "urn:ietf:wg:oauth:2.0:oob"
print("1. Open this page:\n%s\n\n" % auth.authorization_uri)
print("2. Enter the authorization code shown in the page: ")
auth.code = $stdin.gets.chomp
auth.fetch_access_token!
access_token = auth.access_token

# Creates a session.
session = GoogleDrive.login_with_oauth(access_token)

# Gets list of remote files.
for file in session.files
  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 "rubygems"
require "google/api_client"
require "google_drive"

# Same as the code above to get access_token...

# Creates a session.
session = GoogleDrive.login_with_oauth(access_token)

# First worksheet of
# https://docs.google.com/spreadsheet/ccc?key=pz7XtlQC-PYx-jrVMJErTcg
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.
for row in 1..ws.num_rows
  for col in 1..ws.num_cols
    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()

API document: gimite.net/doc/google-drive-ruby/

Source code

github.com/gimite/google-drive-ruby

The license of this source is “New BSD Licence”

Supported environments

Ruby 1.9.x or later. Checked with Ruby 1.9.3 and 2.1.3.

Author

Hiroshi Ichikawa - gimite.net/en/index.php?Contact

google-drive-ruby's People

Contributors

gimite avatar nalanj avatar swhitt avatar phuongnd08 avatar stympy avatar evgenyneu avatar wkang avatar bpot avatar robermorales avatar erebor avatar sirupsen avatar hotchpotch avatar christophermanning avatar eldavido avatar er1c avatar iamruinous avatar norgg avatar ocouronne avatar wesgibbs avatar wiktorschmidt avatar yemartin avatar deepblue avatar

Watchers

James Cloos avatar Christo De Lange 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.