Skip to content

Commit

Permalink
Switch from google-api-client 0.8.x to 0.9.x.
Browse files Browse the repository at this point in the history
Switch from Google Drive API V2 to V3.
  • Loading branch information
gimite committed May 4, 2016
1 parent 45aab02 commit 1fc9ad2
Show file tree
Hide file tree
Showing 14 changed files with 394 additions and 391 deletions.
29 changes: 29 additions & 0 deletions MIGRATING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Migrating from version 1.x.x to 2.x.x

google-drive-ruby 1.x.x depends on google-api-client 0.8.x and Google Drive API V2.

google-drive-ruby 2.x.x depends on google-api-client 0.9.x and Google Drive API V3.

Each of them involves incompatible API changes. The users of google-drive-ruby may be affected by these changes.

Here are some changes likely affecting google-drive-ruby users:

If you pass an instance of Google::APIClient to GoogleDrive.login_with_oauth, it will no longer work, because Google::APIClient.new was removed. You need to pass credentials generated by [googleauth](https://github.com/google/google-auth-library-ruby) library instead.

The field "title" in search queries was renamed to "name". e.g.,

```ruby
session.files(q: "title = 'hoge'")
```

must be rewritten to:

```ruby
session.files(q: "name = 'hoge'")
```

# Migrating from version 0.x.x to 1.x.x

Ver. 0.x.x no longer works, because the API used was deprecated and shut down. You need to migrate to ver. 1.x.x or later.

Ver. 1.x.x is not 100% backward compatible with 0.x.x. Some methods have been removed. Especially, GoogleDrive.login has been removed, and you must use GoogleDrive.saved_session or GoogleDrive.login_with_oauth instead, as in the example code in README.rdoc.
6 changes: 2 additions & 4 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ 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 0.3.x or before to ver. 1.0.x
= Migration from ver. 0.x.x / 1.x.x to to ver. 2.x.x

Ver. 0.3.x and the versions before no longer works, because the API used was deprecated and shut down. You need to migrate to ver. 1.0.x.

Ver. 1.0.x 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.saved_session or GoogleDrive.login_with_oauth instead, as in the example code below.
There are some incompatible API changes. See [MIGRATING.md](https://github.com/gimite/google-drive-ruby/blob/master/MIGRATING.md).


= How to install
Expand Down
18 changes: 9 additions & 9 deletions google_drive.gemspec
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
Gem::Specification.new do |s|
s.name = 'google_drive'
s.version = '1.0.6'
s.version = '2.0.0.pre1'
s.authors = ['Hiroshi Ichikawa']
s.email = ['gimite+github@gmail.com']
s.summary = 'A library to read/write files/spreadsheets in Google Drive/Docs.'
s.description = 'A library to read/write files/spreadsheets in Google Drive/Docs.'
s.homepage = 'https://github.com/gimite/google-drive-ruby'
s.rubygems_version = '1.2.0'
s.license = 'New BSD'
s.required_ruby_version = '>= 2.0.0'

s.files = ['README.rdoc'] + Dir['lib/**/*']
s.require_paths = ['lib']
s.has_rdoc = true
s.extra_rdoc_files = ['README.rdoc'] + Dir['doc_src/**/*']
s.rdoc_options = ['--main', 'README.rdoc']

s.add_dependency('nokogiri', ['>= 1.4.4', '!= 1.5.1', '!= 1.5.2'])
s.add_dependency('oauth', ['>= 0.3.6'])
s.add_dependency('oauth2', ['>= 0.5.0'])
# TODO: Switch to 0.9 API once it is released:
# https://github.com/google/google-api-ruby-client/blob/master/MIGRATING.md
s.add_dependency('google-api-client', ['>= 0.7.0', '< 0.9'])
s.add_development_dependency('test-unit', ['>= 3.0.0'])
s.add_dependency('nokogiri', ['>= 1.5.3', '< 2.0.0'])
s.add_dependency('oauth', ['>= 0.3.6', '< 1.0.0'])
s.add_dependency('oauth2', ['>= 0.5.0', '< 2.0.0'])
s.add_dependency('google-api-client', ['>= 0.9.0', '< 1.0.0'])
s.add_dependency('googleauth', ['>= 0.5.0', '< 1.0.0'])
s.add_development_dependency('test-unit', ['>= 3.0.0', '< 4.0.0'])
s.add_development_dependency('rake', ['>= 0.8.0'])
s.add_development_dependency('rspec-mocks', ['>= 3.4.0'])
s.add_development_dependency('rspec-mocks', ['>= 3.4.0', '< 4.0.0'])
end
110 changes: 40 additions & 70 deletions lib/google_drive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,77 +2,52 @@
# The license of this source is "New BSD Licence"

require 'json'
require 'google/api_client'
require 'googleauth'

require 'google_drive/session'

module GoogleDrive
# Authenticates with given OAuth2 token.
#
# +access_token+ can be either OAuth2 access_token string, or OAuth2::AccessToken.
# +access_token+ can be either OAuth2 access_token string, OAuth2::AccessToken
# or credentials generated by googleauth library.
#
# OAuth2 code example for Web apps:
#
# require "rubygems"
# require "google/api_client"
# client = Google::APIClient.new
# auth = client.authorization
# # Follow Step 1 and 2 of “Authorizing requests with OAuth 2.0” in
# # https://developers.google.com/drive/v3/web/about-auth to get a client ID and client secret.
# 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 = "http://example.com/redirect"
# auth_url = auth.authorization_uri
# require "googleauth"
#
# credentials = Google::Auth::UserRefreshCredentials.new(
# client_id: "YOUR CLIENT ID",
# client_secret: "YOUR CLIENT SECRET",
# scope: [
# "https://www.googleapis.com/auth/drive",
# "https://spreadsheets.google.com/feeds/",
# ],
# redirect_uri: "http://example.com/redirect")
# auth_url = credentials.authorization_uri
# # Redirect the user to auth_url and get authorization code from redirect URL.
# auth.code = authorization_code
# auth.fetch_access_token!
# session = GoogleDrive.login_with_oauth(auth.access_token)
# credentials.code = authorization_code
# credentials.fetch_access_token!
# session = GoogleDrive.login_with_oauth(credentials)
#
# auth.access_token expires in 1 hour. If you want to restore a session afterwards, you can store
# auth.refresh_token somewhere after auth.fetch_access_token! above, and use this code:
#
# require "rubygems"
# require "google/api_client"
# client = Google::APIClient.new
# auth = client.authorization
# # Follow "Create a client ID and client secret" in
# # https://developers.google.com/drive/web/auth/web-server] to get a client ID and client secret.
# 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 = "http://example.com/redirect"
# require "googleauth"
#
# credentials = Google::Auth::UserRefreshCredentials.new(
# client_id: "YOUR CLIENT ID",
# client_secret: "YOUR CLIENT SECRET",
# scope: [
# "https://www.googleapis.com/auth/drive",
# "https://spreadsheets.google.com/feeds/",
# ],
# redirect_uri: "http://example.com/redirect")
# auth.refresh_token = refresh_token
# auth.fetch_access_token!
# session = GoogleDrive.login_with_oauth(auth.access_token)
#
# OAuth2 code example for command-line apps:
#
# require "rubygems"
# require "google/api_client"
# client = Google::APIClient.new
# auth = client.authorization
# # Follow "Create a client ID and client secret" in
# # https://developers.google.com/drive/web/auth/web-server] to get a client ID and client secret.
# 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!
# session = GoogleDrive.login_with_oauth(auth.access_token)
#
# See this document for details:
#
# - https://developers.google.com/drive/web/about-auth
# For command-line apps, it would be easier to use saved_session method instead.
def self.login_with_oauth(client_or_access_token, proxy = nil)
Session.new(client_or_access_token, proxy)
end
Expand Down Expand Up @@ -149,30 +124,25 @@ def self.saved_session(

refresh_token = config.refresh_token

client = Google::APIClient.new(
application_name: 'google_drive Ruby library',
application_version: '0.4.0'
)

auth = client.authorization
auth.client_id = config.client_id
auth.client_secret = config.client_secret
auth.scope = config.scope
auth.redirect_uri = 'urn:ietf:wg:oauth:2.0:oob'
credentials = Google::Auth::UserRefreshCredentials.new(
client_id: config.client_id,
client_secret: config.client_secret,
scope: config.scope,
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob')

if config.refresh_token
auth.refresh_token = config.refresh_token
auth.fetch_access_token!
credentials.refresh_token = config.refresh_token
credentials.fetch_access_token!
else
$stderr.print("\n1. Open this page:\n%s\n\n" % auth.authorization_uri)
$stderr.print("\n1. Open this page:\n%s\n\n" % credentials.authorization_uri)
$stderr.print('2. Enter the authorization code shown in the page: ')
auth.code = $stdin.gets.chomp
auth.fetch_access_token!
config.refresh_token = auth.refresh_token
credentials.code = $stdin.gets.chomp
credentials.fetch_access_token!
config.refresh_token = credentials.refresh_token
end

config.save

GoogleDrive.login_with_oauth(client)
GoogleDrive.login_with_oauth(credentials)
end
end
52 changes: 20 additions & 32 deletions lib/google_drive/acl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@ class Acl
def initialize(session, file) #:nodoc:
@session = session
@file = file
api_result = @session.execute!(
api_method: @session.drive.permissions.list,
parameters: { fileId: @file.id })
@entries = api_result.data.items.map { |i| AclEntry.new(i, self) }
api_permissions = @session.drive.list_permissions(@file.id, fields: '*')
@entries = api_permissions.permissions.map { |perm| AclEntry.new(perm, self) }
end

def_delegators(:@entries, :size, :[], :each)

# Adds a new entry. +entry+ is either a GoogleDrive::AclEntry or a Hash with keys
# :scope_type, :scope and :role. See GoogleDrive::AclEntry#scope_type and
# +:type+, +:email_address+, +:domain+, +:role+ and +:allow_file_discovery+.
# See GoogleDrive::AclEntry#type and
# GoogleDrive::AclEntry#role for the document of the fields.
#
# Also you can pass the second hash argument +options+, which specifies
Expand All @@ -43,30 +42,29 @@ def initialize(session, file) #:nodoc:
# e.g.
# # A specific user can read or write.
# spreadsheet.acl.push(
# {:type => "user", :value => "example2@gmail.com", :role => "reader"})
# {type: "user", email_address: "example2@gmail.com", role: "reader"})
# spreadsheet.acl.push(
# {:type => "user", :value => "example3@gmail.com", :role => "writer"})
# {type: "user", email_address: "example3@gmail.com", role: "writer"})
# # Share with a Google Apps domain.
# spreadsheet.acl.push(
# {type: "domain", domain: "gimite.net", role: "reader"})
# # Publish on the Web.
# spreadsheet.acl.push(
# {:type => "anyone", :role => "reader"})
# {type: "anyone", role: "reader"})
# # Anyone who knows the link can read.
# spreadsheet.acl.push(
# {:type => "anyone", :withLink => true, :role => "reader"})
# {type: "anyone", allow_file_discovery: false, role: "reader"})
# # Set ACL without sending notification emails
# spreadsheet.acl.push(
# {:type => "user", :value => "example2@gmail.com", :role => "reader"},
# {:sendNotificationEmails => false})
# {type: "user", email_address: "example2@gmail.com", role: "reader"},
# {sendNotificationEmails: false})
#
# See here for parameter detais:
# https://developers.google.com/drive/v2/reference/permissions/insert
# https://developers.google.com/drive/v3/reference/permissions/create
def push(params_or_entry, options = {})
entry = params_or_entry.is_a?(AclEntry) ? params_or_entry : AclEntry.new(params_or_entry)
new_permission = @session.drive.permissions.insert.request_schema.new(entry.params)
api_result = @session.execute!(
api_method: @session.drive.permissions.insert,
body_object: new_permission,
parameters: options.merge(fileId: @file.id))
new_entry = AclEntry.new(api_result.data, self)
api_permission = @session.drive.create_permission(@file.id, entry.params, fields: '*')
new_entry = AclEntry.new(api_permission, self)
@entries.push(new_entry)
new_entry
end
Expand All @@ -76,24 +74,14 @@ def push(params_or_entry, options = {})
# e.g.
# spreadsheet.acl.delete(spreadsheet.acl[1])
def delete(entry)
@session.execute!(
api_method: @session.drive.permissions.delete,
parameters: {
fileId: @file.id,
permissionId: entry.id
})
@session.drive.delete_permission(@file.id, entry.id)
@entries.delete(entry)
end

def update_role(entry) #:nodoc:
api_result = @session.execute!(
api_method: @session.drive.permissions.update,
body_object: entry.api_permission,
parameters: {
fileId: @file.id,
permissionId: entry.id
})
entry.api_permission = api_result.data
api_permission = @session.drive.update_permission(
@file.id, entry.id, {role: entry.role}, fields: '*')
entry.api_permission = api_permission
entry
end

Expand Down
Loading

0 comments on commit 1fc9ad2

Please sign in to comment.