google_drive 1.0.5 → 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6b4bb25ca507e0809ba9fc7a57f5a49667b61536
4
- data.tar.gz: d6664ab492c6a12051d75890179eb06738df6821
3
+ metadata.gz: 63bf6efd4050300790543c7ed135632f86e61432
4
+ data.tar.gz: 481431da22d9bbebbf0a44067eaef26e7b05dc57
5
5
  SHA512:
6
- metadata.gz: b0a978d591952834520f3c8832c6c795de5936893a492e885eff1589b422a60aa61b094bd52067258f3e7158b669d6fbd9152ee924100baddb629667c9e655dc
7
- data.tar.gz: 14a9596a037ffbceb349c2f2c95bdb688ddbcbef68a5a1460d9426ecc10f52d82e415e3208991cd99ee18fa16469a32e7cd2462d29a6936f06ce9a0d8a1e9cec
6
+ metadata.gz: ff5e59d1b8b1a3bfcc59a6bda70d03b71f179d896e9dcb7c7cac836ce674569929a06dcfc5e792f19fac1d23398c4b043a64951b3597544acaae42a351a652d9
7
+ data.tar.gz: 77f7e1b096e71e4cfe7f3c4cb745f0efa9ce202fd41bfbbb38fb52a341ac7d4d1b3af55927d9cef7ec3eadf79ac41d7b5cef9a7a269af83554c2707308625835
@@ -17,7 +17,7 @@ Ver. 1.0.x is not 100% backward compatible with 0.3.x. Some methods have been re
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. 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.
20
+ First, follow Step 1 and 2 of "Authorizing requests with OAuth 2.0" in {this page}[https://developers.google.com/drive/v3/web/about-auth] 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
  Next, create a file config.json which contains the client ID and crient secret you got above, which looks like:
23
23
 
@@ -17,8 +17,8 @@ module GoogleDrive
17
17
  # require "google/api_client"
18
18
  # client = Google::APIClient.new
19
19
  # auth = client.authorization
20
- # # Follow "Create a client ID and client secret" in
21
- # # https://developers.google.com/drive/web/auth/web-server] to get a client ID and client secret.
20
+ # # Follow Step 1 and 2 of “Authorizing requests with OAuth 2.0” in
21
+ # # https://developers.google.com/drive/v3/web/about-auth to get a client ID and client secret.
22
22
  # auth.client_id = "YOUR CLIENT ID"
23
23
  # auth.client_secret = "YOUR CLIENT SECRET"
24
24
  # auth.scope =
@@ -105,9 +105,25 @@ module GoogleDrive
105
105
  #
106
106
  # If the file doesn't exist or client ID/secret are not given in the file, the default client
107
107
  # ID/secret embedded in the library is used.
108
+ #
109
+ # You can also provide a config object that must respond to:
110
+ # client_id
111
+ # client_secret
112
+ # refesh_token
113
+ # refresh_token=
114
+ # scope
115
+ # scope=
116
+ # save
108
117
  def self.saved_session(
109
- path = nil, proxy = nil, client_id = nil, client_secret = nil)
110
- config = Config.new(path || ENV['HOME'] + '/.ruby_google_drive.token')
118
+ path_or_config = nil, proxy = nil, client_id = nil, client_secret = nil)
119
+ config = case path_or_config
120
+ when String
121
+ Config.new(path_or_config)
122
+ when nil
123
+ Config.new(ENV['HOME'] + '/.ruby_google_drive.token')
124
+ else
125
+ path_or_config
126
+ end
111
127
 
112
128
  config.scope ||= [
113
129
  'https://www.googleapis.com/auth/drive',
@@ -15,18 +15,18 @@ module GoogleDrive
15
15
  # feed, not list feed. In this way, we can easily provide consistent API as
16
16
  # GoogleDrive::Worksheet using save()/reload().
17
17
  class List
18
-
18
+
19
19
  include(Enumerable)
20
-
20
+
21
21
  def initialize(worksheet) #:nodoc:
22
22
  @worksheet = worksheet
23
23
  end
24
-
24
+
25
25
  # Number of non-empty rows in the worksheet excluding the first row.
26
26
  def size
27
27
  return @worksheet.num_rows - 1
28
28
  end
29
-
29
+
30
30
  # Returns Hash-like object (GoogleDrive::ListRow) for the row with the
31
31
  # index. Keys of the object are colum names (the first row).
32
32
  # The second row has index 0.
@@ -36,7 +36,7 @@ module GoogleDrive
36
36
  def [](index)
37
37
  return ListRow.new(self, index)
38
38
  end
39
-
39
+
40
40
  # Updates the row with the index with the given Hash object.
41
41
  # Keys of +hash+ are colum names (the first row).
42
42
  # The second row has index 0.
@@ -46,7 +46,7 @@ module GoogleDrive
46
46
  def []=(index, hash)
47
47
  self[index].replace(hash)
48
48
  end
49
-
49
+
50
50
  # Iterates over Hash-like object (GoogleDrive::ListRow) for each row
51
51
  # (except for the first row).
52
52
  # Keys of the object are colum names (the first row).
@@ -55,13 +55,13 @@ module GoogleDrive
55
55
  yield(self[i])
56
56
  end
57
57
  end
58
-
58
+
59
59
  # Column names i.e. the contents of the first row.
60
60
  # Duplicates are removed.
61
61
  def keys
62
62
  return (1..@worksheet.num_cols).map(){ |i| @worksheet[1, i] }.uniq()
63
63
  end
64
-
64
+
65
65
  # Updates column names i.e. the contents of the first row.
66
66
  #
67
67
  # Note that update is not sent to the server until
@@ -74,7 +74,7 @@ module GoogleDrive
74
74
  @worksheet[1, i] = ""
75
75
  end
76
76
  end
77
-
77
+
78
78
  # Adds a new row to the bottom.
79
79
  # Keys of +hash+ are colum names (the first row).
80
80
  # Returns GoogleDrive::ListRow for the new row.
@@ -86,34 +86,38 @@ module GoogleDrive
86
86
  row.update(hash)
87
87
  return row
88
88
  end
89
-
89
+
90
90
  # Returns all rows (except for the first row) as Array of Hash.
91
91
  # Keys of Hash objects are colum names (the first row).
92
92
  def to_hash_array()
93
93
  return self.map(){ |r| r.to_hash() }
94
94
  end
95
-
95
+
96
96
  def get(index, key) #:nodoc:
97
97
  return @worksheet[index + 2, key_to_col(key)]
98
98
  end
99
-
99
+
100
100
  def numeric_value(index, key) #:nodoc:
101
101
  return @worksheet.numeric_value(index + 2, key_to_col(key))
102
102
  end
103
-
103
+
104
+ def input_value(index, key) #:nodoc:
105
+ return @worksheet.input_value(index + 2, key_to_col(key))
106
+ end
107
+
104
108
  def set(index, key, value) #:nodoc:
105
109
  @worksheet[index + 2, key_to_col(key)] = value
106
110
  end
107
-
111
+
108
112
  private
109
-
113
+
110
114
  def key_to_col(key)
111
115
  key = key.to_s()
112
116
  col = (1..@worksheet.num_cols).find(){ |c| @worksheet[1, c] == key }
113
117
  raise(GoogleDrive::Error, "Column doesn't exist: %p" % key) if !col
114
118
  return col
115
119
  end
116
-
120
+
117
121
  end
118
-
122
+
119
123
  end
@@ -11,59 +11,63 @@ module GoogleDrive
11
11
 
12
12
  # Hash-like object returned by GoogleDrive::List#[].
13
13
  class ListRow
14
-
14
+
15
15
  include(Enumerable)
16
16
  extend(Forwardable)
17
-
17
+
18
18
  def_delegators(:to_hash,
19
19
  :keys, :values, :each_key, :each_value, :each, :each_pair, :hash,
20
20
  :assoc, :fetch, :flatten, :key, :invert, :size, :length, :rassoc,
21
21
  :merge, :reject, :select, :sort, :to_a, :values_at)
22
-
22
+
23
23
  def initialize(list, index) #:nodoc:
24
24
  @list = list
25
25
  @index = index
26
26
  end
27
-
27
+
28
28
  def [](key)
29
29
  return @list.get(@index, key)
30
30
  end
31
-
31
+
32
32
  def numeric_value(key)
33
33
  return @list.numeric_value(@index, key)
34
34
  end
35
-
35
+
36
+ def input_value(key)
37
+ return @list.input_value(@index, key)
38
+ end
39
+
36
40
  def []=(key, value)
37
41
  @list.set(@index, key, value)
38
42
  end
39
-
43
+
40
44
  def has_key?(key)
41
45
  return @list.keys.include?(key)
42
46
  end
43
-
47
+
44
48
  alias include? has_key?
45
49
  alias key? has_key?
46
50
  alias member? has_key?
47
-
51
+
48
52
  def update(hash)
49
53
  for k, v in hash
50
54
  self[k] = v
51
55
  end
52
56
  end
53
-
57
+
54
58
  alias merge! update
55
-
59
+
56
60
  def replace(hash)
57
61
  clear()
58
62
  update(hash)
59
63
  end
60
-
64
+
61
65
  def clear()
62
66
  for key in @list.keys
63
67
  self[key] = ""
64
68
  end
65
69
  end
66
-
70
+
67
71
  def to_hash()
68
72
  result = {}
69
73
  for key in @list.keys
@@ -71,18 +75,18 @@ module GoogleDrive
71
75
  end
72
76
  return result
73
77
  end
74
-
78
+
75
79
  def ==(other)
76
80
  return self.class == other.class && self.to_hash() == other.to_hash()
77
81
  end
78
-
82
+
79
83
  alias === ==
80
84
  alias eql? ==
81
-
85
+
82
86
  def inspect
83
87
  return "\#<%p %p>" % [self.class, to_hash()]
84
88
  end
85
-
89
+
86
90
  end
87
-
91
+
88
92
  end
@@ -116,6 +116,7 @@ module GoogleDrive
116
116
  def []=(*args)
117
117
  (row, col) = parse_cell_args(args[0...-1])
118
118
  value = args[-1].to_s()
119
+ validate_cell_value(value)
119
120
  reload_cells() if !@cells
120
121
  @cells[[row, col]] = value
121
122
  @input_values[[row, col]] = value
@@ -244,6 +245,64 @@ module GoogleDrive
244
245
  return result.freeze()
245
246
  end
246
247
 
248
+ # Inserts rows.
249
+ #
250
+ # e.g.
251
+ # # Inserts 2 empty rows before row 3.
252
+ # worksheet.insert_rows(3, 2)
253
+ # # Inserts 2 rows with values before row 3.
254
+ # worksheet.insert_rows(3, [["a, "b"], ["c, "d"]])
255
+ #
256
+ # Note that this method is implemented by shifting all cells below the row.
257
+ # Its behavior is different from inserting rows on the web interface if the
258
+ # worksheet contains inter-cell reference.
259
+ def insert_rows(row_num, rows)
260
+
261
+ if rows.is_a?(Integer)
262
+ rows = Array.new(rows, [])
263
+ end
264
+
265
+ # Shifts all cells below the row.
266
+ self.max_rows += rows.size
267
+ r = self.num_rows
268
+ while r >= row_num
269
+ for c in 1..self.num_cols
270
+ self[r + rows.size, c] = self[r, c]
271
+ end
272
+ r -= 1
273
+ end
274
+
275
+ # Fills in the inserted rows.
276
+ num_cols = self.num_cols
277
+ rows.each_with_index() do |row, r|
278
+ for c in 0...[row.size, num_cols].max
279
+ self[row_num + r, 1 + c] = row[c] || ""
280
+ end
281
+ end
282
+
283
+ end
284
+
285
+ # Deletes rows.
286
+ #
287
+ # e.g.
288
+ # # Deletes 2 rows starting from row 3 (i.e., deletes row 3 and 4).
289
+ # worksheet.delete_rows(3, 2)
290
+ #
291
+ # Note that this method is implemented by shifting all cells below the row.
292
+ # Its behavior is different from deleting rows on the web interface if the
293
+ # worksheet contains inter-cell reference.
294
+ def delete_rows(row_num, rows)
295
+ if row_num + rows - 1 > self.max_rows
296
+ raise(ArgumentError, "The row number is out of range")
297
+ end
298
+ for r in row_num..(self.max_rows - rows)
299
+ for c in 1..self.num_cols
300
+ self[r, c] = self[r + rows, c]
301
+ end
302
+ end
303
+ self.max_rows -= rows
304
+ end
305
+
247
306
  # Reloads content of the worksheets from the server.
248
307
  # Note that changes you made by []= etc. is discarded if you haven't called save().
249
308
  def reload()
@@ -471,6 +530,12 @@ module GoogleDrive
471
530
  end
472
531
  end
473
532
 
533
+ def validate_cell_value(value)
534
+ if value.include?("\x1a")
535
+ raise(ArgumentError, "Contains invalid character \\x1a for xml: %p" % value)
536
+ end
537
+ end
538
+
474
539
  end
475
540
 
476
541
  end
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.5
4
+ version: 1.0.6
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-12-17 00:00:00.000000000 Z
11
+ date: 2016-02-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -112,6 +112,20 @@ dependencies:
112
112
  - - ">="
113
113
  - !ruby/object:Gem::Version
114
114
  version: 0.8.0
115
+ - !ruby/object:Gem::Dependency
116
+ name: rspec-mocks
117
+ requirement: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: 3.4.0
122
+ type: :development
123
+ prerelease: false
124
+ version_requirements: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: 3.4.0
115
129
  description: A library to read/write files/spreadsheets in Google Drive/Docs.
116
130
  email:
117
131
  - gimite+github@gmail.com
@@ -161,7 +175,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
161
175
  version: '0'
162
176
  requirements: []
163
177
  rubyforge_project:
164
- rubygems_version: 2.4.8
178
+ rubygems_version: 2.5.1
165
179
  signing_key:
166
180
  specification_version: 4
167
181
  summary: A library to read/write files/spreadsheets in Google Drive/Docs.