google_drive 1.0.6 → 2.0.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,127 +5,171 @@
5
5
  # The license of this source is "New BSD Licence"
6
6
 
7
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
8
30
 
9
- # An entry of an ACL (access control list) of a spreadsheet.
10
- #
11
- # Use GoogleDrive::Acl#[] to get GoogleDrive::AclEntry object.
31
+ attr_reader(:acl)
32
+ attr_reader(:params) #:nodoc:
33
+ attr_accessor(:api_permission) #:nodoc:
34
+
35
+ # The role given to the scope. One of:
36
+ # - "owner": The owner.
37
+ # - "writer": With read/write access.
38
+ # - "reader": With read-only access.
39
+ def role
40
+ @params ? @params[:role] : @api_permission.role
41
+ end
42
+
43
+ # Type of the scope. One of:
12
44
  #
13
- # This code is based on https://github.com/guyboertje/gdata-spreadsheet-ruby .
14
- class AclEntry
15
-
16
- include(Util)
17
-
18
- # +params_or_api_permission+ is a Hash object with keys +:type+, +:value+, +:role+ and +:withLink+.
19
- # See GoogleDrive::Acl#push for description of the parameters.
20
- def initialize(params_or_api_permission, acl = nil)
21
- @acl = acl
22
- if acl
23
- @api_permission = params_or_api_permission
24
- @params = nil
25
- delegate_api_methods(self, @api_permission, ["value"])
26
- else
27
- @api_permission = nil
28
- @params = convert_params(params_or_api_permission)
29
- end
30
- end
45
+ # - "user": a Google account specified by the email_address field.
46
+ # - "group": a Google Group specified by the email_address field.
47
+ # - "domain": a Google Apps domain specified by the domain field.
48
+ # - "anyone": Publicly shared with all users.
49
+ def type
50
+ @params ? @params[:type] : @api_permission.type
51
+ end
31
52
 
32
- attr_reader(:acl)
33
- attr_reader(:params) #:nodoc:
34
- attr_accessor(:api_permission) #:nodoc:
53
+ alias_method :scope_type, :type
35
54
 
36
- # The role given to the scope. One of:
37
- # - "owner": The owner.
38
- # - "writer": With read/write access.
39
- # - "reader": With read-only access.
40
- def role
41
- return @params ? @params["role"] : @api_permission.role
42
- end
55
+ def additional_roles
56
+ @params ? @params[:additionalRoles] : @api_permission.additional_roles
57
+ end
43
58
 
44
- # Type of the scope. One of:
45
- #
46
- # - "user": value is a user's email address.
47
- # - "group": value is a Google Group email address.
48
- # - "domain": value is a Google Apps domain.
49
- # - "anyone": Publicly shared with all users. value is +nil+.
50
- def type
51
- return @params ? @params["type"] : @api_permission.type
52
- end
59
+ def id
60
+ @params ? @params[:id] : @api_permission.id
61
+ end
53
62
 
54
- alias scope_type type
63
+ # Email address of the user or the group.
64
+ def email_address
65
+ @params ? @params[:email_address] : @api_permission.email_address
66
+ end
55
67
 
56
- def additional_roles
57
- return @params ? @params["additionalRoles"] : @api_permission.additional_roles
58
- end
68
+ # The Google Apps domain name.
69
+ def domain
70
+ @params ? @params[:domain] : @api_permission.domain
71
+ end
59
72
 
60
- def id
61
- return @params ? @params["id"] : @api_permission.id
73
+ def value
74
+ if @params
75
+ case @params[:type]
76
+ when 'user', 'group'
77
+ @params[:email_address]
78
+ when 'domain'
79
+ @params[:domain]
80
+ else
81
+ nil
62
82
  end
63
-
64
- # The value of the scope. See type.
65
- def value
66
- if @params
67
- return @params["value"]
68
- else
69
- case @api_permission.type
70
- when "user", "group"
71
- return @api_permission.email_address
72
- when "domain"
73
- return @api_permission.domain
74
- else
75
- return nil
76
- end
77
- end
83
+ else
84
+ case @api_permission.type
85
+ when 'user', 'group'
86
+ @api_permission.email_address
87
+ when 'domain'
88
+ @api_permission.domain
89
+ else
90
+ nil
78
91
  end
92
+ end
93
+ end
79
94
 
80
- alias scope value
95
+ alias_method :scope, :value
81
96
 
82
- # If +true+, the file is shared only with people who know the link.
83
- def with_link
84
- return @params ? @params["withLink"] : @api_permission.with_link
85
- end
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 ? @params[:allow_file_discovery] : @api_permission.allow_file_discovery
101
+ end
86
102
 
87
- alias with_key with_link
88
-
89
- # Changes the role of the scope.
90
- #
91
- # e.g.
92
- # spreadsheet.acl[1].role = "writer"
93
- def role=(role)
94
- if @params
95
- @params["role"] = role
96
- else
97
- @api_permission.role = role
98
- @acl.update_role(self)
99
- end
100
- end
103
+ # If +true+, the file is shared only with people who know the link.
104
+ # Only used for type "anyone".
105
+ def with_link
106
+ allow_file_discovery == false
107
+ end
101
108
 
102
- def inspect
103
- return "\#<%p type=%p, name=%p, role=%p>" %
104
- [self.class, self.type, self.name, self.role]
105
- end
109
+ alias_method :with_key, :with_link
106
110
 
107
- private
108
-
109
- # Normalizes the key to String, and converts parameters in the old version.
110
- def convert_params(orig_params)
111
- new_params = {}
112
- for k, v in orig_params
113
- k = k.to_s()
114
- case k
115
- when "scope_type"
116
- new_params["type"] = (v == "default" ? "anyone" : v)
117
- when "scope"
118
- new_params["value"] = v
119
- when "with_key"
120
- new_params["withLink"] = v
121
- else
122
- new_params[k] = v
123
- end
124
- end
125
- return new_params
126
- end
111
+ # Changes the role of the scope.
112
+ #
113
+ # e.g.
114
+ # spreadsheet.acl[1].role = "writer"
115
+ def role=(role)
116
+ if @params
117
+ @params[:role] = role
118
+ else
119
+ @api_permission.role = role
120
+ @acl.update_role(self)
121
+ end
122
+ end
127
123
 
124
+ def inspect
125
+ case type
126
+ when 'user', 'group'
127
+ "\#<%p type=%p, email_address=%p, role=%p>" %
128
+ [self.class, type, email_address, role]
129
+ when 'domain'
130
+ "\#<%p type=%p, domain=%p, role=%p>" %
131
+ [self.class, type, domain, role]
132
+ when 'anyone'
133
+ "\#<%p type=%p, role=%p, allow_file_discovery=%p>" %
134
+ [self.class, type, role, allow_file_discovery]
135
+ else
136
+ "\#<%p type=%p, role=%p>" %
137
+ [self.class, type, role]
138
+ end
128
139
  end
129
140
 
130
- end
141
+ private
142
+
143
+ # Normalizes the key to Symbol, and converts parameters in the old version.
144
+ def convert_params(orig_params)
145
+ new_params = {}
146
+ value = nil
147
+ orig_params.each do |k, v|
148
+ k = k.to_s
149
+ case k
150
+ when 'scope_type'
151
+ new_params[:type] = (v == 'default' ? 'anyone' : v)
152
+ when 'scope'
153
+ new_params[:value] = v
154
+ when 'with_key', 'withLink'
155
+ new_params[:allow_file_discovery] = !v
156
+ when 'value'
157
+ value = v
158
+ else
159
+ new_params[k.intern] = v
160
+ end
161
+ end
162
+
163
+ if value
164
+ case new_params[:type]
165
+ when 'user', 'group'
166
+ new_params[:email_address] = value
167
+ when 'domain'
168
+ new_params[:domain] = value
169
+ end
170
+ end
131
171
 
172
+ new_params
173
+ end
174
+ end
175
+ end
@@ -1,52 +1,33 @@
1
1
  # Author: Hiroshi Ichikawa <http://gimite.net/>
2
2
  # The license of this source is "New BSD Licence"
3
3
 
4
- require "net/https"
5
- require "uri"
4
+ require 'net/https'
5
+ require 'uri'
6
+ require 'google/apis/drive_v3'
6
7
  Net::HTTP.version_1_2
7
8
 
8
-
9
9
  module GoogleDrive
10
+ class ApiClientFetcher
11
+ class Response
12
+ def initialize(code, body)
13
+ @code = code
14
+ @body = body
15
+ end
16
+
17
+ attr_reader(:code, :body)
18
+ end
10
19
 
11
- class ApiClientFetcher
12
-
13
- class Response
14
-
15
- def initialize(client_response)
16
- @client_response = client_response
17
- end
18
-
19
- def code
20
- return @client_response.status.to_s()
21
- end
22
-
23
- def body
24
- return @client_response.body
25
- end
26
-
27
- attr_reader(:client_response)
28
-
29
- end
20
+ def initialize(authorization)
21
+ @drive = Google::Apis::DriveV3::DriveService.new
22
+ @drive.authorization = authorization
23
+ end
30
24
 
31
- def initialize(client)
32
- @client = client
33
- # Sets virtually infinite default timeout because some operations (e.g., uploading
34
- # a large files/spreadsheets) can take very long.
35
- @client.connection.options[:timeout] ||= 100000000
36
- @drive = @client.discovered_api("drive", "v2")
37
- end
25
+ attr_reader(:drive)
38
26
 
39
- attr_reader(:client, :drive)
40
-
41
- def request_raw(method, url, data, extra_header, auth)
42
- client_response = @client.execute(
43
- :http_method => method,
44
- :uri => url,
45
- :body => data,
46
- :headers => extra_header)
47
- return Response.new(client_response)
48
- end
49
-
27
+ def request_raw(method, url, data, extra_header, _auth)
28
+ options = Google::Apis::RequestOptions.default.merge(header: extra_header)
29
+ body = @drive.http(method, url, body: data, options: options)
30
+ Response.new('200', body)
50
31
  end
51
-
32
+ end
52
33
  end
@@ -1,14 +1,10 @@
1
1
  # Author: Hiroshi Ichikawa <http://gimite.net/>
2
2
  # The license of this source is "New BSD Licence"
3
3
 
4
- require "google_drive/response_code_error"
5
-
4
+ require 'google_drive/response_code_error'
6
5
 
7
6
  module GoogleDrive
8
-
9
- # Raised when GoogleDrive.login has failed.
10
- class AuthenticationError < GoogleDrive::ResponseCodeError
11
-
12
- end
13
-
7
+ # Raised when GoogleDrive.login has failed.
8
+ class AuthenticationError < GoogleDrive::ResponseCodeError
9
+ end
14
10
  end
@@ -1,165 +1,143 @@
1
1
  # Author: Hiroshi Ichikawa <http://gimite.net/>
2
2
  # The license of this source is "New BSD Licence"
3
3
 
4
- require "google_drive/util"
5
- require "google_drive/error"
6
- require "google_drive/spreadsheet"
7
-
4
+ require 'google_drive/util'
5
+ require 'google_drive/error'
6
+ require 'google_drive/spreadsheet'
8
7
 
9
8
  module GoogleDrive
9
+ # Use GoogleDrive::Session#root_collection, GoogleDrive::Collection#subcollections,
10
+ # or GoogleDrive::Session#collection_by_url to get GoogleDrive::Collection object.
11
+ class Collection < GoogleDrive::File
12
+ include(Util)
10
13
 
11
- # Use GoogleDrive::Session#root_collection, GoogleDrive::Collection#subcollections,
12
- # or GoogleDrive::Session#collection_by_url to get GoogleDrive::Collection object.
13
- class Collection < GoogleDrive::File
14
-
15
- include(Util)
16
-
17
- alias collection_feed_url document_feed_url
18
-
19
- # Adds the given GoogleDrive::File to the collection.
20
- def add(file)
21
- new_child = @session.drive.children.insert.request_schema.new({
22
- "id" => file.id,
23
- })
24
- @session.execute!(
25
- :api_method => @session.drive.children.insert,
26
- :body_object => new_child,
27
- :parameters => {
28
- "folderId" => self.id,
29
- "childId" => file.id,
30
- })
31
- return nil
32
- end
14
+ alias_method :collection_feed_url, :document_feed_url
33
15
 
34
- # Creates a sub-collection with given title. Returns GoogleDrive::Collection object.
35
- def create_subcollection(title)
36
- file = @session.drive.files.insert.request_schema.new({
37
- "title" => title,
38
- "mimeType" => "application/vnd.google-apps.folder",
39
- "parents" => [{"id" => self.id}],
40
- })
41
- api_result = @session.execute!(
42
- :api_method => @session.drive.files.insert,
43
- :body_object => file)
44
- return @session.wrap_api_file(api_result.data)
45
- end
16
+ # Adds the given GoogleDrive::File to the collection.
17
+ def add(file)
18
+ @session.drive.update_file(file.id, add_parents: self.id, fields: '')
19
+ nil
20
+ end
46
21
 
47
- # Removes the given GoogleDrive::File from the collection.
48
- def remove(file)
49
- @session.execute!(
50
- :api_method => @session.drive.children.delete,
51
- :parameters => {
52
- "folderId" => self.id,
53
- "childId" => file.id,
54
- })
55
- return nil
56
- end
22
+ # Removes the given GoogleDrive::File from the collection.
23
+ def remove(file)
24
+ @session.drive.update_file(file.id, remove_parents: self.id, fields: '')
25
+ end
57
26
 
58
- # Returns true if this is a root collection
59
- def root?
60
- return self.api_file.parents.empty?
61
- end
27
+ # Creates a sub-collection with given title. Returns GoogleDrive::Collection object.
28
+ def create_subcollection(title)
29
+ file_metadata = {
30
+ name: title,
31
+ mime_type: 'application/vnd.google-apps.folder',
32
+ parents: [self.id],
33
+ }
34
+ file = @session.drive.create_file(file_metadata, fields: '*')
35
+ @session.wrap_api_file(file)
36
+ end
62
37
 
63
- # Returns all the files (including spreadsheets, documents, subcollections) in the collection.
64
- #
65
- # You can specify parameters documented at
66
- # https://developers.google.com/drive/v2/reference/files/list
67
- #
68
- # e.g.
69
- #
70
- # # Gets all the files in collection, including subcollections.
71
- # collection.files
72
- # # Gets only files with title "hoge".
73
- # collection.files("q" => "title = 'hoge'")
74
- # # Same as above with a placeholder.
75
- # collection.files("q" => ["title = ?", "hoge"])
76
- #
77
- # By default, it returns the first 100 files. See document of GoogleDrive::Session#files method
78
- # for how to get all files.
79
- def files(params = {}, &block)
80
- return files_with_type(nil, params, &block)
81
- end
38
+ # Returns true if this is a root collection
39
+ def root?
40
+ !api_file.parents || api_file.parents.empty?
41
+ end
82
42
 
83
- alias contents files
43
+ # Returns all the files (including spreadsheets, documents, subcollections) in the collection.
44
+ #
45
+ # You can specify parameters documented at
46
+ # https://developers.google.com/drive/v2/reference/files/list
47
+ #
48
+ # e.g.
49
+ #
50
+ # # Gets all the files in collection, including subcollections.
51
+ # collection.files
52
+ # # Gets only files with title "hoge".
53
+ # collection.files("q" => "title = 'hoge'")
54
+ # # Same as above with a placeholder.
55
+ # collection.files("q" => ["title = ?", "hoge"])
56
+ #
57
+ # By default, it returns the first 100 files. See document of GoogleDrive::Session#files method
58
+ # for how to get all files.
59
+ def files(params = {}, &block)
60
+ files_with_type(nil, params, &block)
61
+ end
84
62
 
85
- # Returns all the spreadsheets in the collection.
86
- #
87
- # By default, it returns the first 100 spreadsheets. See document of GoogleDrive::Session#files method
88
- # for how to get all spreadsheets.
89
- def spreadsheets(params = {}, &block)
90
- return files_with_type("application/vnd.google-apps.spreadsheet", params, &block)
91
- end
92
-
93
- # Returns all the Google Docs documents in the collection.
94
- #
95
- # By default, it returns the first 100 documents. See document of GoogleDrive::Session#files method
96
- # for how to get all documents.
97
- def documents(params = {}, &block)
98
- return files_with_type("application/vnd.google-apps.document", params, &block)
99
- end
100
-
101
- # Returns all its subcollections.
102
- #
103
- # By default, it returns the first 100 subcollections. See document of GoogleDrive::Session#files method
104
- # for how to get all subcollections.
105
- def subcollections(params = {}, &block)
106
- return files_with_type("application/vnd.google-apps.folder", params, &block)
107
- end
108
-
109
- # Returns a file (can be a spreadsheet, document, subcollection or other files) in the
110
- # collection which exactly matches +title+ as GoogleDrive::File.
111
- # Returns nil if not found. If multiple collections with the +title+ are found, returns
112
- # one of them.
113
- #
114
- # If given an Array, does a recursive subcollection traversal.
115
- def file_by_title(title)
116
- return file_by_title_with_type(title, nil)
117
- end
118
-
119
- # Returns its subcollection whose title exactly matches +title+ as GoogleDrive::Collection.
120
- # Returns nil if not found. If multiple collections with the +title+ are found, returns
121
- # one of them.
122
- #
123
- # If given an Array, does a recursive subcollection traversal.
124
- def subcollection_by_title(title)
125
- return file_by_title_with_type(title, "application/vnd.google-apps.folder")
126
- end
63
+ alias_method :contents, :files
127
64
 
128
- # Returns URL of the deprecated contents feed.
129
- def contents_url
130
- self.document_feed_url + "/contents"
131
- end
132
-
133
- protected
134
-
135
- def file_by_title_with_type(title, type)
136
- if title.is_a?(Array)
137
- rel_path = title
138
- if rel_path.empty?
139
- return self
140
- else
141
- parent = subcollection_by_title(rel_path[0...-1])
142
- return parent && parent.file_by_title_with_type(rel_path[-1], type)
143
- end
144
- else
145
- return files_with_type(type, "q" => ["title = ?", title], "maxResults" => 1)[0]
146
- end
147
- end
148
-
149
- private
150
-
151
- def files_with_type(type, params = {}, &block)
152
- params = convert_params(params)
153
- query = construct_and_query([
154
- ["? in parents", self.id],
155
- type ? ["mimeType = ?", type] : nil,
156
- params["q"],
157
- ])
158
- params = params.merge({"q" => query})
159
- # This is faster than calling children.list and then files.get for each file.
160
- return @session.files(params, &block)
65
+ # Returns all the spreadsheets in the collection.
66
+ #
67
+ # By default, it returns the first 100 spreadsheets. See document of GoogleDrive::Session#files method
68
+ # for how to get all spreadsheets.
69
+ def spreadsheets(params = {}, &block)
70
+ files_with_type('application/vnd.google-apps.spreadsheet', params, &block)
71
+ end
72
+
73
+ # Returns all the Google Docs documents in the collection.
74
+ #
75
+ # By default, it returns the first 100 documents. See document of GoogleDrive::Session#files method
76
+ # for how to get all documents.
77
+ def documents(params = {}, &block)
78
+ files_with_type('application/vnd.google-apps.document', params, &block)
79
+ end
80
+
81
+ # Returns all its subcollections.
82
+ #
83
+ # By default, it returns the first 100 subcollections. See document of GoogleDrive::Session#files method
84
+ # for how to get all subcollections.
85
+ def subcollections(params = {}, &block)
86
+ files_with_type('application/vnd.google-apps.folder', params, &block)
87
+ end
88
+
89
+ # Returns a file (can be a spreadsheet, document, subcollection or other files) in the
90
+ # collection which exactly matches +title+ as GoogleDrive::File.
91
+ # Returns nil if not found. If multiple collections with the +title+ are found, returns
92
+ # one of them.
93
+ #
94
+ # If given an Array, does a recursive subcollection traversal.
95
+ def file_by_title(title)
96
+ file_by_title_with_type(title, nil)
97
+ end
98
+
99
+ # Returns its subcollection whose title exactly matches +title+ as GoogleDrive::Collection.
100
+ # Returns nil if not found. If multiple collections with the +title+ are found, returns
101
+ # one of them.
102
+ #
103
+ # If given an Array, does a recursive subcollection traversal.
104
+ def subcollection_by_title(title)
105
+ file_by_title_with_type(title, 'application/vnd.google-apps.folder')
106
+ end
107
+
108
+ # Returns URL of the deprecated contents feed.
109
+ def contents_url
110
+ document_feed_url + '/contents'
111
+ end
112
+
113
+ protected
114
+
115
+ def file_by_title_with_type(title, type)
116
+ if title.is_a?(Array)
117
+ rel_path = title
118
+ if rel_path.empty?
119
+ return self
120
+ else
121
+ parent = subcollection_by_title(rel_path[0...-1])
122
+ return parent && parent.file_by_title_with_type(rel_path[-1], type)
161
123
  end
162
-
124
+ else
125
+ files_with_type(type, q: ['name = ?', title], page_size: 1)[0]
126
+ end
127
+ end
128
+
129
+ private
130
+
131
+ def files_with_type(type, params = {}, &block)
132
+ params = convert_params(params)
133
+ query = construct_and_query([
134
+ ['? in parents', id],
135
+ type ? ['mimeType = ?', type] : nil,
136
+ params[:q]
137
+ ])
138
+ params = params.merge(q: query)
139
+ # This is faster than calling children.list and then files.get for each file.
140
+ @session.files(params, &block)
163
141
  end
164
-
142
+ end
165
143
  end