google_drive 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.rdoc +35 -42
- data/lib/google_drive.rb +3 -2
- data/lib/google_drive/acl.rb +13 -2
- data/lib/google_drive/acl_entry.rb +13 -2
- data/lib/google_drive/session.rb +5 -2
- data/lib/google_drive/spreadsheet.rb +0 -24
- data/lib/google_drive/util.rb +3 -3
- data/lib/google_drive/worksheet.rb +6 -50
- metadata +22 -4
- data/lib/google_drive/record.rb +0 -31
- data/lib/google_drive/table.rb +0 -60
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ea4f78acd448f0bec2b79cce06961178ba502ad3
|
4
|
+
data.tar.gz: 4c0ddf477184f57e660de0ddbf89254b0a8d14cf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4620ee74e9995463e97fbc6f59f2eb03ad317550ec7e7d43047ae26f960b40a0f1e4f6de567839ff00254550e42301f2d0eed78a9681c88d2fa7537249aba19b
|
7
|
+
data.tar.gz: 49b0e4cbf285044fd58219d34308c94556623ea63274661f96f22da88ccd7c5c6157862525c5f1250b4dcc593d5cdf5455cc2ee0fb30ebb214859f607b5e099a
|
data/README.rdoc
CHANGED
@@ -1,13 +1,13 @@
|
|
1
|
-
This is a Ruby
|
1
|
+
This is a Ruby library to read/write files/spreadsheets in Google Drive/Docs.
|
2
2
|
|
3
3
|
NOTE: This is NOT a library to create Google Drive App.
|
4
4
|
|
5
5
|
|
6
|
-
=
|
6
|
+
= Migration from 0.3.x or before to ver. 1.0.x
|
7
7
|
|
8
|
-
Ver.
|
8
|
+
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.
|
9
9
|
|
10
|
-
|
10
|
+
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.
|
11
11
|
|
12
12
|
|
13
13
|
= How to install
|
@@ -17,83 +17,76 @@ I recommend to switch to the new API. But if you somehow want to continue using
|
|
17
17
|
|
18
18
|
= How to use
|
19
19
|
|
20
|
-
First, follow "Create a client ID and client secret" in {this page}[https://developers.google.com/drive/web/auth/web-server] to get a client ID and client secret for OAuth.
|
20
|
+
First, follow "Create a client ID and client secret" in {this page}[https://developers.google.com/drive/web/auth/web-server] 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.
|
21
21
|
|
22
22
|
Example to read/write files in Google Drive:
|
23
23
|
|
24
|
-
require "rubygems"
|
25
24
|
require "google/api_client"
|
26
25
|
require "google_drive"
|
27
26
|
|
28
|
-
#
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
auth.code = $stdin.gets.chomp
|
40
|
-
auth.fetch_access_token!
|
41
|
-
access_token = auth.access_token
|
27
|
+
# The client ID and client secret you obtained in the step above.
|
28
|
+
CLIENT_ID = ...
|
29
|
+
CLIENT_SECRET = ...
|
30
|
+
|
31
|
+
# Creates a session. This will prompt the credential via command line for the
|
32
|
+
# first time and save it to ./stored_token.json file for later usages.
|
33
|
+
#
|
34
|
+
# If you are developing a Web app, and you want to ask the user to log in in
|
35
|
+
# the Web app instead of via command line, follow the example code in:
|
36
|
+
# http://gimite.net/doc/google-drive-ruby/GoogleDrive.html#method-c-login_with_oauth
|
37
|
+
session = GoogleDrive.saved_session("./stored_token.json", nil, CLIENT_ID, CLIENT_SECRET)
|
42
38
|
|
43
|
-
# Creates a session.
|
44
|
-
session = GoogleDrive.login_with_oauth(access_token)
|
45
|
-
|
46
39
|
# Gets list of remote files.
|
47
|
-
|
40
|
+
session.files.each do |file|
|
48
41
|
p file.title
|
49
42
|
end
|
50
|
-
|
43
|
+
|
51
44
|
# Uploads a local file.
|
52
|
-
session.upload_from_file("/path/to/hello.txt", "hello.txt", :
|
53
|
-
|
45
|
+
session.upload_from_file("/path/to/hello.txt", "hello.txt", convert: false)
|
46
|
+
|
54
47
|
# Downloads to a local file.
|
55
48
|
file = session.file_by_title("hello.txt")
|
56
49
|
file.download_to_file("/path/to/hello.txt")
|
57
|
-
|
50
|
+
|
58
51
|
# Updates content of the remote file.
|
59
52
|
file.update_from_file("/path/to/hello.txt")
|
60
|
-
|
61
|
-
Example to read/write spreadsheets:
|
62
53
|
|
63
|
-
|
54
|
+
Example to read/write spreadsheets:
|
55
|
+
|
64
56
|
require "google/api_client"
|
65
57
|
require "google_drive"
|
66
58
|
|
67
59
|
# Same as the code above to get access_token...
|
68
|
-
|
60
|
+
|
69
61
|
# Creates a session.
|
70
62
|
session = GoogleDrive.login_with_oauth(access_token)
|
71
|
-
|
63
|
+
|
72
64
|
# First worksheet of
|
73
65
|
# https://docs.google.com/spreadsheet/ccc?key=pz7XtlQC-PYx-jrVMJErTcg
|
66
|
+
# Or https://docs.google.com/a/someone.com/spreadsheets/d/pz7XtlQC-PYx-jrVMJErTcg/edit?usp=drive_web
|
74
67
|
ws = session.spreadsheet_by_key("pz7XtlQC-PYx-jrVMJErTcg").worksheets[0]
|
75
|
-
|
68
|
+
|
76
69
|
# Gets content of A2 cell.
|
77
70
|
p ws[2, 1] #==> "hoge"
|
78
|
-
|
71
|
+
|
79
72
|
# Changes content of cells.
|
80
73
|
# Changes are not sent to the server until you call ws.save().
|
81
74
|
ws[2, 1] = "foo"
|
82
75
|
ws[2, 2] = "bar"
|
83
|
-
ws.save
|
84
|
-
|
76
|
+
ws.save
|
77
|
+
|
85
78
|
# Dumps all cells.
|
86
|
-
|
87
|
-
|
79
|
+
(1..ws.num_rows).each do |row|
|
80
|
+
(1..ws.num_cols).each do |col|
|
88
81
|
p ws[row, col]
|
89
82
|
end
|
90
83
|
end
|
91
|
-
|
84
|
+
|
92
85
|
# Yet another way to do so.
|
93
86
|
p ws.rows #==> [["fuga", ""], ["foo", "bar]]
|
94
|
-
|
87
|
+
|
95
88
|
# Reloads the worksheet to get changes by other clients.
|
96
|
-
ws.reload
|
89
|
+
ws.reload
|
97
90
|
|
98
91
|
API document: http://gimite.net/doc/google-drive-ruby/
|
99
92
|
|
data/lib/google_drive.rb
CHANGED
@@ -124,9 +124,10 @@ module GoogleDrive
|
|
124
124
|
auth = client.authorization
|
125
125
|
auth.client_id = client_id
|
126
126
|
auth.client_secret = client_secret
|
127
|
-
auth.scope =
|
128
|
-
"https://www.googleapis.com/auth/drive
|
127
|
+
auth.scope = [
|
128
|
+
"https://www.googleapis.com/auth/drive",
|
129
129
|
"https://spreadsheets.google.com/feeds/"
|
130
|
+
]
|
130
131
|
auth.redirect_uri = "urn:ietf:wg:oauth:2.0:oob"
|
131
132
|
|
132
133
|
if token_data
|
data/lib/google_drive/acl.rb
CHANGED
@@ -33,6 +33,13 @@ module GoogleDrive
|
|
33
33
|
# :scope_type, :scope and :role. See GoogleDrive::AclEntry#scope_type and
|
34
34
|
# GoogleDrive::AclEntry#role for the document of the fields.
|
35
35
|
#
|
36
|
+
# Also you can pass the second hash argument +options+, which specifies
|
37
|
+
# optional query parameters for the API.
|
38
|
+
# Possible keys of +options+ are,
|
39
|
+
# * :emailMessage -- A custom message to include in notification emails
|
40
|
+
# * :sendNotificationEmails -- Whether to send notification emails
|
41
|
+
# when sharing to users or groups.
|
42
|
+
#
|
36
43
|
# NOTE: This sends email to the new people.
|
37
44
|
#
|
38
45
|
# e.g.
|
@@ -47,16 +54,20 @@ module GoogleDrive
|
|
47
54
|
# # Anyone who knows the link can read.
|
48
55
|
# spreadsheet.acl.push(
|
49
56
|
# {:type => "anyone", :withLink => true, :role => "reader"})
|
57
|
+
# # Set ACL without sending notification emails
|
58
|
+
# spreadsheet.acl.push(
|
59
|
+
# {:type => "user", :value => "example2@gmail.com", :role => "reader"},
|
60
|
+
# {:sendNotificationEmails => false})
|
50
61
|
#
|
51
62
|
# See here for parameter detais:
|
52
63
|
# https://developers.google.com/drive/v2/reference/permissions/insert
|
53
|
-
def push(params_or_entry)
|
64
|
+
def push(params_or_entry, options = {})
|
54
65
|
entry = params_or_entry.is_a?(AclEntry) ? params_or_entry : AclEntry.new(params_or_entry)
|
55
66
|
new_permission = @session.drive.permissions.insert.request_schema.new(entry.params)
|
56
67
|
api_result = @session.execute!(
|
57
68
|
:api_method => @session.drive.permissions.insert,
|
58
69
|
:body_object => new_permission,
|
59
|
-
:parameters => { "fileId" => @file.id })
|
70
|
+
:parameters => options.merge({ "fileId" => @file.id }))
|
60
71
|
new_entry = AclEntry.new(api_result.data, self)
|
61
72
|
@entries.push(new_entry)
|
62
73
|
return new_entry
|
@@ -22,7 +22,7 @@ module GoogleDrive
|
|
22
22
|
if acl
|
23
23
|
@api_permission = params_or_api_permission
|
24
24
|
@params = nil
|
25
|
-
delegate_api_methods(self, @api_permission)
|
25
|
+
delegate_api_methods(self, @api_permission, ["value"])
|
26
26
|
else
|
27
27
|
@api_permission = nil
|
28
28
|
@params = convert_params(params_or_api_permission)
|
@@ -63,7 +63,18 @@ module GoogleDrive
|
|
63
63
|
|
64
64
|
# The value of the scope. See type.
|
65
65
|
def value
|
66
|
-
|
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
|
67
78
|
end
|
68
79
|
|
69
80
|
alias scope value
|
data/lib/google_drive/session.rb
CHANGED
@@ -460,7 +460,10 @@ module GoogleDrive
|
|
460
460
|
# Human-readable new spreadsheet/document.
|
461
461
|
when /\/d\/([^\/]+)/
|
462
462
|
return $1
|
463
|
-
# Human-readable
|
463
|
+
# Human-readable new collection page.
|
464
|
+
when /^\/drive\/[^\/]+\/([^\/]+)/
|
465
|
+
return $1
|
466
|
+
# Human-readable old folder view.
|
464
467
|
when /\/folderview$/
|
465
468
|
if (uri.query || "").split(/&/).find(){ |s| s=~ /^id=(.*)$/ }
|
466
469
|
return $1
|
@@ -472,7 +475,7 @@ module GoogleDrive
|
|
472
475
|
end
|
473
476
|
end
|
474
477
|
case uri.fragment
|
475
|
-
# Human-readable collection page.
|
478
|
+
# Human-readable old collection page.
|
476
479
|
when /^folders\/(.+)$/
|
477
480
|
return $1
|
478
481
|
end
|
@@ -6,7 +6,6 @@ require "time"
|
|
6
6
|
require "google_drive/util"
|
7
7
|
require "google_drive/error"
|
8
8
|
require "google_drive/worksheet"
|
9
|
-
require "google_drive/table"
|
10
9
|
require "google_drive/acl"
|
11
10
|
require "google_drive/file"
|
12
11
|
|
@@ -43,17 +42,6 @@ module GoogleDrive
|
|
43
42
|
return "https://spreadsheets.google.com/feeds/spreadsheets/private/full/" + self.id
|
44
43
|
end
|
45
44
|
|
46
|
-
# DEPRECATED: Table and Record feeds are deprecated and they will not be available after
|
47
|
-
# March 2012.
|
48
|
-
#
|
49
|
-
# Tables feed URL of the spreadsheet.
|
50
|
-
def tables_feed_url
|
51
|
-
warn(
|
52
|
-
"DEPRECATED: Google Spreadsheet Table and Record feeds are deprecated and they " +
|
53
|
-
"will not be available after March 2012.")
|
54
|
-
return "https://spreadsheets.google.com/feeds/%s/tables" % self.id
|
55
|
-
end
|
56
|
-
|
57
45
|
# Returns worksheets of the spreadsheet as array of GoogleDrive::Worksheet.
|
58
46
|
def worksheets
|
59
47
|
doc = @session.request(:get, self.worksheets_feed_url)
|
@@ -95,18 +83,6 @@ module GoogleDrive
|
|
95
83
|
return Worksheet.new(@session, self, doc.root)
|
96
84
|
end
|
97
85
|
|
98
|
-
# DEPRECATED: Table and Record feeds are deprecated and they will not be available after
|
99
|
-
# March 2012.
|
100
|
-
#
|
101
|
-
# Returns list of tables in the spreadsheet.
|
102
|
-
def tables
|
103
|
-
warn(
|
104
|
-
"DEPRECATED: Google Spreadsheet Table and Record feeds are deprecated and they " +
|
105
|
-
"will not be available after March 2012.")
|
106
|
-
doc = @session.request(:get, self.tables_feed_url)
|
107
|
-
return doc.css("entry").map(){ |e| Table.new(@session, e) }.freeze()
|
108
|
-
end
|
109
|
-
|
110
86
|
end
|
111
87
|
|
112
88
|
end
|
data/lib/google_drive/util.rb
CHANGED
@@ -153,15 +153,15 @@ module GoogleDrive
|
|
153
153
|
return new_params
|
154
154
|
end
|
155
155
|
|
156
|
-
def
|
156
|
+
def get_singleton_class(obj)
|
157
157
|
class << obj
|
158
158
|
return self
|
159
159
|
end
|
160
160
|
end
|
161
161
|
|
162
162
|
def delegate_api_methods(obj, api_obj, exceptions = [])
|
163
|
-
sc =
|
164
|
-
names = api_obj.class.keys.keys - exceptions
|
163
|
+
sc = get_singleton_class(obj)
|
164
|
+
names = api_obj.class.keys.keys - exceptions.map(&:to_s)
|
165
165
|
names.each() do |name|
|
166
166
|
sc.__send__(:define_method, name) do
|
167
167
|
api_obj.__send__(name)
|
@@ -7,7 +7,6 @@ require "uri"
|
|
7
7
|
|
8
8
|
require "google_drive/util"
|
9
9
|
require "google_drive/error"
|
10
|
-
require "google_drive/table"
|
11
10
|
require "google_drive/list"
|
12
11
|
|
13
12
|
|
@@ -307,7 +306,12 @@ module GoogleDrive
|
|
307
306
|
value = @cells[[row, col]]
|
308
307
|
entry = cell_entries[[row, col]]
|
309
308
|
id = entry.css("id").text
|
310
|
-
|
309
|
+
edit_link = entry.css("link[rel='edit']")[0]
|
310
|
+
if !edit_link
|
311
|
+
raise(GoogleDrive::Error,
|
312
|
+
"The user doesn't have write permission to the spreadsheet: %p" % self.spreadsheet)
|
313
|
+
end
|
314
|
+
edit_url = edit_link["href"]
|
311
315
|
xml << <<-EOS
|
312
316
|
<entry>
|
313
317
|
<batch:id>#{h(row)},#{h(col)}</batch:id>
|
@@ -368,54 +372,6 @@ module GoogleDrive
|
|
368
372
|
return !@modified.empty?
|
369
373
|
end
|
370
374
|
|
371
|
-
# DEPRECATED: Table and Record feeds are deprecated and they will not be available after
|
372
|
-
# March 2012.
|
373
|
-
#
|
374
|
-
# Creates table for the worksheet and returns GoogleDrive::Table.
|
375
|
-
# See this document for details:
|
376
|
-
# http://code.google.com/intl/en/apis/spreadsheets/docs/3.0/developers_guide_protocol.html#TableFeeds
|
377
|
-
def add_table(table_title, summary, columns, options)
|
378
|
-
|
379
|
-
warn(
|
380
|
-
"DEPRECATED: Google Spreadsheet Table and Record feeds are deprecated and they " +
|
381
|
-
"will not be available after March 2012.")
|
382
|
-
default_options = { :header_row => 1, :num_rows => 0, :start_row => 2}
|
383
|
-
options = default_options.merge(options)
|
384
|
-
|
385
|
-
column_xml = ""
|
386
|
-
columns.each() do |index, name|
|
387
|
-
column_xml += "<gs:column index='#{h(index)}' name='#{h(name)}'/>\n"
|
388
|
-
end
|
389
|
-
|
390
|
-
xml = <<-"EOS"
|
391
|
-
<entry xmlns="http://www.w3.org/2005/Atom"
|
392
|
-
xmlns:gs="http://schemas.google.com/spreadsheets/2006">
|
393
|
-
<title type='text'>#{h(table_title)}</title>
|
394
|
-
<summary type='text'>#{h(summary)}</summary>
|
395
|
-
<gs:worksheet name='#{h(self.title)}' />
|
396
|
-
<gs:header row='#{options[:header_row]}' />
|
397
|
-
<gs:data numRows='#{options[:num_rows]}' startRow='#{options[:start_row]}'>
|
398
|
-
#{column_xml}
|
399
|
-
</gs:data>
|
400
|
-
</entry>
|
401
|
-
EOS
|
402
|
-
|
403
|
-
result = @session.request(:post, self.spreadsheet.tables_feed_url, :data => xml)
|
404
|
-
return Table.new(@session, result)
|
405
|
-
|
406
|
-
end
|
407
|
-
|
408
|
-
# DEPRECATED: Table and Record feeds are deprecated and they will not be available after
|
409
|
-
# March 2012.
|
410
|
-
#
|
411
|
-
# Returns list of tables for the workwheet.
|
412
|
-
def tables
|
413
|
-
warn(
|
414
|
-
"DEPRECATED: Google Spreadsheet Table and Record feeds are deprecated and they " +
|
415
|
-
"will not be available after March 2012.")
|
416
|
-
return self.spreadsheet.tables.select(){ |t| t.worksheet_title == self.title }
|
417
|
-
end
|
418
|
-
|
419
375
|
# List feed URL of the worksheet.
|
420
376
|
def list_feed_url
|
421
377
|
return @worksheet_feed_entry.css(
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: google_drive
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hiroshi Ichikawa
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-10-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|
@@ -71,6 +71,9 @@ dependencies:
|
|
71
71
|
- - ">="
|
72
72
|
- !ruby/object:Gem::Version
|
73
73
|
version: 0.7.0
|
74
|
+
- - "<"
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0.9'
|
74
77
|
type: :runtime
|
75
78
|
prerelease: false
|
76
79
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -78,6 +81,23 @@ dependencies:
|
|
78
81
|
- - ">="
|
79
82
|
- !ruby/object:Gem::Version
|
80
83
|
version: 0.7.0
|
84
|
+
- - "<"
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0.9'
|
87
|
+
- !ruby/object:Gem::Dependency
|
88
|
+
name: minitest
|
89
|
+
requirement: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 5.1.0
|
94
|
+
type: :runtime
|
95
|
+
prerelease: false
|
96
|
+
version_requirements: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: 5.1.0
|
81
101
|
- !ruby/object:Gem::Dependency
|
82
102
|
name: rake
|
83
103
|
requirement: !ruby/object:Gem::Requirement
|
@@ -113,11 +133,9 @@ files:
|
|
113
133
|
- lib/google_drive/file.rb
|
114
134
|
- lib/google_drive/list.rb
|
115
135
|
- lib/google_drive/list_row.rb
|
116
|
-
- lib/google_drive/record.rb
|
117
136
|
- lib/google_drive/response_code_error.rb
|
118
137
|
- lib/google_drive/session.rb
|
119
138
|
- lib/google_drive/spreadsheet.rb
|
120
|
-
- lib/google_drive/table.rb
|
121
139
|
- lib/google_drive/util.rb
|
122
140
|
- lib/google_drive/worksheet.rb
|
123
141
|
- lib/google_drive_v0.rb
|
data/lib/google_drive/record.rb
DELETED
@@ -1,31 +0,0 @@
|
|
1
|
-
# Author: Hiroshi Ichikawa <http://gimite.net/>
|
2
|
-
# The license of this source is "New BSD Licence"
|
3
|
-
|
4
|
-
require "google_drive/util"
|
5
|
-
require "google_drive/error"
|
6
|
-
|
7
|
-
|
8
|
-
module GoogleDrive
|
9
|
-
|
10
|
-
# DEPRECATED: Table and Record feeds are deprecated and they will not be available after
|
11
|
-
# March 2012.
|
12
|
-
#
|
13
|
-
# Use GoogleDrive::Table#records to get GoogleDrive::Record objects.
|
14
|
-
class Record < Hash
|
15
|
-
include(Util)
|
16
|
-
|
17
|
-
def initialize(session, entry) #:nodoc:
|
18
|
-
@session = session
|
19
|
-
entry.css("gs|field").each() do |field|
|
20
|
-
self[field["name"]] = field.inner_text
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
def inspect #:nodoc:
|
25
|
-
content = self.map(){ |k, v| "%p => %p" % [k, v] }.join(", ")
|
26
|
-
return "\#<%p:{%s}>" % [self.class, content]
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|
data/lib/google_drive/table.rb
DELETED
@@ -1,60 +0,0 @@
|
|
1
|
-
# Author: Hiroshi Ichikawa <http://gimite.net/>
|
2
|
-
# The license of this source is "New BSD Licence"
|
3
|
-
|
4
|
-
require "google_drive/util"
|
5
|
-
require "google_drive/error"
|
6
|
-
require "google_drive/record"
|
7
|
-
|
8
|
-
|
9
|
-
module GoogleDrive
|
10
|
-
|
11
|
-
# DEPRECATED: Table and Record feeds are deprecated and they will not be available after
|
12
|
-
# March 2012.
|
13
|
-
#
|
14
|
-
# Use GoogleDrive::Worksheet#add_table to create table.
|
15
|
-
# Use GoogleDrive::Worksheet#tables to get GoogleDrive::Table objects.
|
16
|
-
class Table
|
17
|
-
|
18
|
-
include(Util)
|
19
|
-
|
20
|
-
def initialize(session, entry) #:nodoc:
|
21
|
-
@columns = {}
|
22
|
-
@worksheet_title = entry.css("gs|worksheet")[0]["name"]
|
23
|
-
@records_url = entry.css("content")[0]["src"]
|
24
|
-
@edit_url = entry.css("link[rel='edit']")[0]["href"]
|
25
|
-
@session = session
|
26
|
-
end
|
27
|
-
|
28
|
-
# Title of the worksheet the table belongs to.
|
29
|
-
attr_reader(:worksheet_title)
|
30
|
-
|
31
|
-
# Adds a record.
|
32
|
-
def add_record(values)
|
33
|
-
fields = ""
|
34
|
-
values.each() do |name, value|
|
35
|
-
fields += "<gs:field name='#{h(name)}'>#{h(value)}</gs:field>"
|
36
|
-
end
|
37
|
-
xml =<<-EOS
|
38
|
-
<entry
|
39
|
-
xmlns="http://www.w3.org/2005/Atom"
|
40
|
-
xmlns:gs="http://schemas.google.com/spreadsheets/2006">
|
41
|
-
#{fields}
|
42
|
-
</entry>
|
43
|
-
EOS
|
44
|
-
@session.request(:post, @records_url, :data => xml)
|
45
|
-
end
|
46
|
-
|
47
|
-
# Returns records in the table.
|
48
|
-
def records
|
49
|
-
doc = @session.request(:get, @records_url)
|
50
|
-
return doc.css("entry").map(){ |e| Record.new(@session, e) }
|
51
|
-
end
|
52
|
-
|
53
|
-
# Deletes this table. Deletion takes effect right away without calling save().
|
54
|
-
def delete
|
55
|
-
@session.request(:delete, @edit_url, :header => {"If-Match" => "*"})
|
56
|
-
end
|
57
|
-
|
58
|
-
end
|
59
|
-
|
60
|
-
end
|