google_drive2 3.0.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4abb83f41fbf856832cb27e8fbdb51f11ef889111f1270486b71f26a0621fb05
4
+ data.tar.gz: 6143e95fd466821eae41bb017b8e119cd901fe7992de05470fab4ddd871f3ab9
5
+ SHA512:
6
+ metadata.gz: 2dcc26743a2a00658af5b8d38c9172e09af0b63c1ddbc8c32171f52f5a0f85438dc08961234365f3529d2ca92ec0a3cbbbd7b763e0e331cd8b787d51db85a64a
7
+ data.tar.gz: 1f7e58c9bde943005f1c4b4c9a7da9589dadeeeab533f85f25253ea041a65e799ee2350c35290e0750ce9d738e9163a664865d589ea3625daddbf4c5b4fb476e
data/README.md ADDED
@@ -0,0 +1,136 @@
1
+ # google-drive-ruby [![Build Status](https://travis-ci.org/gimite/google-drive-ruby.svg?branch=master)](https://travis-ci.org/gimite/google-drive-ruby)
2
+
3
+ This is a Ruby library to read/write files/spreadsheets in Google Drive/Docs.
4
+
5
+ NOTE: This is NOT a library to create Google Drive App.
6
+
7
+
8
+ * [Migration from ver. 2.x.x or before](#migration)
9
+ * [How to install](#install)
10
+ * [How to use](#use)
11
+ * [API documentation](http://www.rubydoc.info/gems/google_drive)
12
+ * [Authorization](https://github.com/gimite/google-drive-ruby/blob/master/doc/authorization.md)
13
+ * [Github](http://github.com/gimite/google-drive-ruby)
14
+ * [License](#license)
15
+ * [Supported environments](#environments)
16
+ * [Author](#author)
17
+
18
+
19
+ ## <a name="migration">Migration from ver. 2.x.x or before</a>
20
+
21
+ There are some incompatible API changes. See
22
+ [MIGRATING.md](https://github.com/gimite/google-drive-ruby/blob/master/MIGRATING.md).
23
+
24
+
25
+ ## <a name="install">How to install</a>
26
+
27
+ Add this line to your application's Gemfile:
28
+
29
+ ```ruby
30
+ gem 'google_drive'
31
+ ```
32
+
33
+ And then execute:
34
+
35
+ ```
36
+ $ bundle
37
+ ```
38
+
39
+ Or install it yourself as:
40
+
41
+ ```
42
+ $ gem install google_drive
43
+ ```
44
+
45
+ If you need system wide installation, execute below:
46
+
47
+ ```
48
+ $ sudo gem install google_drive
49
+ ```
50
+
51
+ ## <a name="use">How to use</a>
52
+
53
+ ### Authorization
54
+
55
+ Follow one of the options in [Authorization](https://github.com/gimite/google-drive-ruby/blob/master/doc/authorization.md) to construct a session object. The example code below assumes "On behalf of you" option.
56
+
57
+ ### Example to read/write files in Google Drive
58
+
59
+ ```ruby
60
+ require "google_drive"
61
+
62
+ # Creates a session. This will prompt the credential via command line for the
63
+ # first time and save it to config.json file for later usages.
64
+ # See this document to learn how to create config.json:
65
+ # https://github.com/gimite/google-drive-ruby/blob/master/doc/authorization.md
66
+ session = GoogleDrive::Session.from_config("config.json")
67
+
68
+ # Gets list of remote files.
69
+ session.files.each do |file|
70
+ p file.title
71
+ end
72
+
73
+ # Uploads a local file.
74
+ session.upload_from_file("/path/to/hello.txt", "hello.txt", convert: false)
75
+
76
+ # Downloads to a local file.
77
+ file = session.file_by_title("hello.txt")
78
+ file.download_to_file("/path/to/hello.txt")
79
+
80
+ # Updates content of the remote file.
81
+ file.update_from_file("/path/to/hello.txt")
82
+ ```
83
+
84
+ ### Example to read/write spreadsheets
85
+
86
+ ```ruby
87
+ require "google_drive"
88
+
89
+ # Creates a session. This will prompt the credential via command line for the
90
+ # first time and save it to config.json file for later usages.
91
+ # See this document to learn how to create config.json:
92
+ # https://github.com/gimite/google-drive-ruby/blob/master/doc/authorization.md
93
+ session = GoogleDrive::Session.from_config("config.json")
94
+
95
+ # First worksheet of
96
+ # https://docs.google.com/spreadsheet/ccc?key=pz7XtlQC-PYx-jrVMJErTcg
97
+ # Or https://docs.google.com/a/someone.com/spreadsheets/d/pz7XtlQC-PYx-jrVMJErTcg/edit?usp=drive_web
98
+ ws = session.spreadsheet_by_key("pz7XtlQC-PYx-jrVMJErTcg").worksheets[0]
99
+
100
+ # Gets content of A2 cell.
101
+ p ws[2, 1] #==> "hoge"
102
+
103
+ # Changes content of cells.
104
+ # Changes are not sent to the server until you call ws.save().
105
+ ws[2, 1] = "foo"
106
+ ws[2, 2] = "bar"
107
+ ws.save
108
+
109
+ # Dumps all cells.
110
+ (1..ws.num_rows).each do |row|
111
+ (1..ws.num_cols).each do |col|
112
+ p ws[row, col]
113
+ end
114
+ end
115
+
116
+ # Yet another way to do so.
117
+ p ws.rows #==> [["fuga", ""], ["foo", "bar]]
118
+
119
+ # Reloads the worksheet to get changes by other clients.
120
+ ws.reload
121
+ ```
122
+
123
+
124
+ ## <a name="license">License</a>
125
+
126
+ New BSD Licence.
127
+
128
+
129
+ ## <a name="environments">Supported environments</a>
130
+
131
+ Ruby 2.0.0 or later. Checked with Ruby 2.4.1.
132
+
133
+
134
+ ## <a name="author">Author</a>
135
+
136
+ [Hiroshi Ichikawa](http://gimite.net/en/index.php?Contact)
@@ -0,0 +1,19 @@
1
+ module GoogleDrive
2
+ # A simple credentials class using an existing OAuth2 access_token.
3
+ #
4
+ # Based on:
5
+ # https://github.com/google/google-api-ruby-client/issues/296
6
+ #
7
+ # @api private
8
+ class AccessTokenCredentials
9
+ attr_reader :access_token
10
+
11
+ def initialize(access_token)
12
+ @access_token = access_token
13
+ end
14
+
15
+ def apply!(headers)
16
+ headers['Authorization'] = "Bearer #{@access_token}"
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,111 @@
1
+ # Author: Guy Boertje <https://github.com/guyboertje>
2
+ # Author: David R. Albrecht <https://github.com/eldavido>
3
+ # Author: Hiroshi Ichikawa <http://gimite.net/>
4
+ # The license of this source is "New BSD Licence"
5
+
6
+ require 'google_drive/acl_entry'
7
+
8
+ module GoogleDrive
9
+ # ACL (access control list) of a spreadsheet.
10
+ #
11
+ # Use GoogleDrive::Spreadsheet#acl to get GoogleDrive::Acl object.
12
+ # See GoogleDrive::Spreadsheet#acl for usage example.
13
+ #
14
+ # This code is based on https://github.com/guyboertje/gdata-spreadsheet-ruby .
15
+ class Acl
16
+ include(Util)
17
+ include(Enumerable)
18
+ extend(Forwardable)
19
+
20
+ # @api private
21
+ def initialize(session, file)
22
+ @session = session
23
+ @file = file
24
+ api_permissions = @session.drive_service.list_permissions(
25
+ @file.id, fields: '*', supports_all_drives: true
26
+ )
27
+ @entries =
28
+ api_permissions.permissions.map { |perm| AclEntry.new(perm, self) }
29
+ end
30
+
31
+ def_delegators(:@entries, :size, :[], :each)
32
+
33
+ # Adds a new entry. +entry+ is either a GoogleDrive::AclEntry or a Hash with
34
+ # keys +:type+, +:email_address+, +:domain+, +:role+ and
35
+ # +:allow_file_discovery+. See GoogleDrive::AclEntry#type and
36
+ # GoogleDrive::AclEntry#role for the document of the fields.
37
+ #
38
+ # Also you can pass the second hash argument +options+, which specifies
39
+ # optional query parameters for the API.
40
+ # Possible keys of +options+ are,
41
+ # * :email_message -- A custom message to include in notification emails
42
+ # * :send_notification_email -- Whether to send notification emails
43
+ # when sharing to users or groups. (Default: true)
44
+ # * :transfer_ownership -- Whether to transfer ownership to the specified
45
+ # user and downgrade the current owner to a writer. This parameter is
46
+ # required as an acknowledgement of the side effect. (Default: false)
47
+ #
48
+ # e.g.
49
+ # # A specific user can read or write.
50
+ # spreadsheet.acl.push(
51
+ # {type: "user", email_address: "example2@gmail.com", role: "reader"})
52
+ # spreadsheet.acl.push(
53
+ # {type: "user", email_address: "example3@gmail.com", role: "writer"})
54
+ # # Share with a Google Apps domain.
55
+ # spreadsheet.acl.push(
56
+ # {type: "domain", domain: "gimite.net", role: "reader"})
57
+ # # Publish on the Web.
58
+ # spreadsheet.acl.push(
59
+ # {type: "anyone", role: "reader"})
60
+ # # Anyone who knows the link can read.
61
+ # spreadsheet.acl.push(
62
+ # {type: "anyone", allow_file_discovery: false, role: "reader"})
63
+ # # Set ACL without sending notification emails
64
+ # spreadsheet.acl.push(
65
+ # {type: "user", email_address: "example2@gmail.com", role: "reader"},
66
+ # {send_notification_email: false})
67
+ #
68
+ # See here for parameter detais:
69
+ # https://developers.google.com/drive/v3/reference/permissions/create
70
+ def push(params_or_entry, options = {})
71
+ entry = params_or_entry.is_a?(AclEntry) ?
72
+ params_or_entry : AclEntry.new(params_or_entry)
73
+ api_permission = @session.drive_service.create_permission(
74
+ @file.id,
75
+ entry.params,
76
+ **{ fields: '*', supports_all_drives: true }.merge(options)
77
+ )
78
+ new_entry = AclEntry.new(api_permission, self)
79
+ @entries.push(new_entry)
80
+ new_entry
81
+ end
82
+
83
+ # Deletes an ACL entry.
84
+ #
85
+ # e.g.
86
+ # spreadsheet.acl.delete(spreadsheet.acl[1])
87
+ def delete(entry)
88
+ @session.drive_service.delete_permission(
89
+ @file.id, entry.id, supports_all_drives: true
90
+ )
91
+ @entries.delete(entry)
92
+ end
93
+
94
+ # @api private
95
+ def update_role(entry)
96
+ api_permission = @session.drive_service.update_permission(
97
+ @file.id,
98
+ entry.id,
99
+ { role: entry.role },
100
+ fields: '*',
101
+ supports_all_drives: true
102
+ )
103
+ entry.api_permission = api_permission
104
+ entry
105
+ end
106
+
107
+ def inspect
108
+ format("\#<%p %p>", self.class, @entries)
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,181 @@
1
+ # Author: Guy Boertje <https://github.com/guyboertje>
2
+ # Author: David R. Albrecht <https://github.com/eldavido>
3
+ # Author: Hiroshi Ichikawa <http://gimite.net/>
4
+ # Author: Phuogn Nguyen <https://github.com/phuongnd08>
5
+ # The license of this source is "New BSD Licence"
6
+
7
+ module GoogleDrive
8
+ # An entry of an ACL (access control list) of a spreadsheet.
9
+ #
10
+ # Use GoogleDrive::Acl#[] to get GoogleDrive::AclEntry object.
11
+ #
12
+ # This code is based on https://github.com/guyboertje/gdata-spreadsheet-ruby .
13
+ class AclEntry
14
+ include(Util)
15
+
16
+ # +params_or_api_permission+ is a Hash object with keys
17
+ # +:type+, +:email_address+, +:domain+, +:role+ and +:allow_file_discovery+.
18
+ # See GoogleDrive::Acl#push for description of the parameters.
19
+ def initialize(params_or_api_permission, acl = nil)
20
+ @acl = acl
21
+ if acl
22
+ @api_permission = params_or_api_permission
23
+ @params = nil
24
+ delegate_api_methods(self, @api_permission)
25
+ else
26
+ @api_permission = nil
27
+ @params = convert_params(params_or_api_permission)
28
+ end
29
+ end
30
+
31
+ attr_reader(:acl)
32
+
33
+ # @api private
34
+ attr_reader(:params)
35
+
36
+ # @api private
37
+ attr_accessor(:api_permission)
38
+
39
+ # The role given to the scope. One of:
40
+ # - "owner": The owner.
41
+ # - "writer": With read/write access.
42
+ # - "reader": With read-only access.
43
+ def role
44
+ @params ? @params[:role] : @api_permission.role
45
+ end
46
+
47
+ # Type of the scope. One of:
48
+ #
49
+ # - "user": a Google account specified by the email_address field.
50
+ # - "group": a Google Group specified by the email_address field.
51
+ # - "domain": a Google Apps domain specified by the domain field.
52
+ # - "anyone": Publicly shared with all users.
53
+ def type
54
+ @params ? @params[:type] : @api_permission.type
55
+ end
56
+
57
+ alias scope_type type
58
+
59
+ def additional_roles
60
+ @params ? @params[:additionalRoles] : @api_permission.additional_roles
61
+ end
62
+
63
+ def id
64
+ @params ? @params[:id] : @api_permission.id
65
+ end
66
+
67
+ # Email address of the user or the group.
68
+ def email_address
69
+ @params ? @params[:email_address] : @api_permission.email_address
70
+ end
71
+
72
+ # The Google Apps domain name.
73
+ def domain
74
+ @params ? @params[:domain] : @api_permission.domain
75
+ end
76
+
77
+ def value
78
+ if @params
79
+ case @params[:type]
80
+ when 'user', 'group'
81
+ @params[:email_address]
82
+ when 'domain'
83
+ @params[:domain]
84
+ end
85
+ else
86
+ case @api_permission.type
87
+ when 'user', 'group'
88
+ @api_permission.email_address
89
+ when 'domain'
90
+ @api_permission.domain
91
+ end
92
+ end
93
+ end
94
+
95
+ alias scope value
96
+
97
+ # If +false+, the file is shared only with people who know the link.
98
+ # Only used for type "anyone".
99
+ def allow_file_discovery
100
+ @params ?
101
+ @params[:allow_file_discovery] : @api_permission.allow_file_discovery
102
+ end
103
+
104
+ # If +true+, the file is shared only with people who know the link.
105
+ # Only used for type "anyone".
106
+ def with_link
107
+ allow_file_discovery == false
108
+ end
109
+
110
+ alias with_key with_link
111
+
112
+ # Changes the role of the scope.
113
+ #
114
+ # e.g.
115
+ # spreadsheet.acl[1].role = "writer"
116
+ def role=(role)
117
+ if @params
118
+ @params[:role] = role
119
+ else
120
+ @api_permission.role = role
121
+ @acl.update_role(self)
122
+ end
123
+ end
124
+
125
+ def inspect
126
+ case type
127
+ when 'user', 'group'
128
+ format(
129
+ "\#<%p type=%p, email_address=%p, role=%p>",
130
+ self.class, type, email_address, role
131
+ )
132
+ when 'domain'
133
+ format(
134
+ "\#<%p type=%p, domain=%p, role=%p>",
135
+ self.class, type, domain, role
136
+ )
137
+ when 'anyone'
138
+ format(
139
+ "\#<%p type=%p, role=%p, allow_file_discovery=%p>",
140
+ self.class, type, role, allow_file_discovery
141
+ )
142
+ else
143
+ format("\#<%p type=%p, role=%p>", self.class, type, role)
144
+ end
145
+ end
146
+
147
+ private
148
+
149
+ # Normalizes the key to Symbol, and converts parameters in the old version.
150
+ def convert_params(orig_params)
151
+ new_params = {}
152
+ value = nil
153
+ orig_params.each do |k, v|
154
+ k = k.to_s
155
+ case k
156
+ when 'scope_type'
157
+ new_params[:type] = (v == 'default' ? 'anyone' : v)
158
+ when 'scope'
159
+ new_params[:value] = v
160
+ when 'with_key', 'withLink'
161
+ new_params[:allow_file_discovery] = !v
162
+ when 'value'
163
+ value = v
164
+ else
165
+ new_params[k.intern] = v
166
+ end
167
+ end
168
+
169
+ if value
170
+ case new_params[:type]
171
+ when 'user', 'group'
172
+ new_params[:email_address] = value
173
+ when 'domain'
174
+ new_params[:domain] = value
175
+ end
176
+ end
177
+
178
+ new_params
179
+ end
180
+ end
181
+ end
@@ -0,0 +1,59 @@
1
+ # Author: Hiroshi Ichikawa <http://gimite.net/>
2
+ # The license of this source is "New BSD Licence"
3
+
4
+ require 'net/https'
5
+ require 'uri'
6
+ require 'google/apis/drive_v3'
7
+ require 'google/apis/sheets_v4'
8
+ Net::HTTP.version_1_2
9
+
10
+ module GoogleDrive
11
+ class ApiClientFetcher
12
+ class Response
13
+ def initialize(code, body)
14
+ @code = code
15
+ @body = body
16
+ end
17
+
18
+ attr_reader(:code, :body)
19
+ end
20
+
21
+ def initialize(authorization, client_options, request_options)
22
+ @drive = Google::Apis::DriveV3::DriveService.new
23
+ @sheets = Google::Apis::SheetsV4::SheetsService.new
24
+
25
+ [@drive, @sheets].each do |service|
26
+ service.authorization = authorization
27
+
28
+ # Make the timeout virtually infinite because some of the operations
29
+ # (e.g., uploading a large file) can take very long.
30
+ # This value is the maximal allowed timeout in seconds on JRuby.
31
+ t = (2**31 - 1) / 1000
32
+ service.client_options.open_timeout_sec = t
33
+ service.client_options.read_timeout_sec = t
34
+ service.client_options.send_timeout_sec = t
35
+
36
+ if client_options
37
+ service.client_options.members.each do |name|
38
+ if !client_options[name].nil?
39
+ service.client_options[name] = client_options[name]
40
+ end
41
+ end
42
+ end
43
+
44
+ if request_options
45
+ service.request_options = service.request_options.merge(request_options)
46
+ end
47
+ end
48
+ end
49
+
50
+ attr_reader(:drive)
51
+ attr_reader(:sheets)
52
+
53
+ def request_raw(method, url, data, extra_header, _auth)
54
+ options = @drive.request_options.merge(header: extra_header)
55
+ body = @drive.http(method, url, body: data, options: options)
56
+ Response.new('200', body)
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,10 @@
1
+ # Author: Hiroshi Ichikawa <http://gimite.net/>
2
+ # The license of this source is "New BSD Licence"
3
+
4
+ require 'google_drive/response_code_error'
5
+
6
+ module GoogleDrive
7
+ # Raised when GoogleDrive.login has failed.
8
+ class AuthenticationError < GoogleDrive::ResponseCodeError
9
+ end
10
+ end