google-cloud-firestore 0.20.0 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4b003b01ba72dbe743a7874f6978b886d395bea33a1cca2c13995b2e1054c5dc
4
- data.tar.gz: bdb2c649d040de24cb63a80bb6de03ce427288b0de2cf4311c19702a751a40ab
3
+ metadata.gz: c376cdc37ee4df6dda7cd41213f01a8662279cef89bd9a0bac780c68d470ccf1
4
+ data.tar.gz: 45b1405ee2ea98bd26a2220fbfc0c5018e95d4b5e9bd828474c65ccfaa74a7db
5
5
  SHA512:
6
- metadata.gz: edb498706f4a6ca151c28bbe7d45c5e62b2e86566879cfd394b2b7443b3d29db33f1bdeb53ab10c183a083a0a583e7069176b68ac136ec1ee700e200f1875b63
7
- data.tar.gz: 9cd66ddae9bc997f0d87616cef791a48e6a89e87304c29d79eb4b97997dae1294ff820dd1a57011e4a34ac5b782a77db44f1bcc6dd2ef733acb7fafcafb61813
6
+ metadata.gz: 894292d1be7d5175b880fd986a4ac5d7ff06a541b3f80fef4994242707d71680d7776b7be1f18892c9978ad3a89ba66a85cbda43a52f90ba032278722b8c981e
7
+ data.tar.gz: a45b97170718f6e751c2593f5e00cdcaab7bd19ae1d12bb371e0030ff33eaf5d4e590bda05bc9431e5273f9f62320db44a8fd7dd954bad31c4d467c146aa3be6
@@ -20,6 +20,8 @@
20
20
 
21
21
  gem "google-cloud-core"
22
22
  require "google/cloud"
23
+ require "google/cloud/config"
24
+ require "googleauth"
23
25
 
24
26
  module Google
25
27
  module Cloud
@@ -104,3 +106,26 @@ module Google
104
106
  end
105
107
  end
106
108
  end
109
+
110
+ # Set the default firestore configuration
111
+ Google::Cloud.configure.add_config! :firestore do |config|
112
+ default_project = Google::Cloud::Config.deferred do
113
+ ENV["FIRESTORE_PROJECT"]
114
+ end
115
+ default_creds = Google::Cloud::Config.deferred do
116
+ Google::Cloud::Config.credentials_from_env(
117
+ "FIRESTORE_CREDENTIALS", "FIRESTORE_CREDENTIALS_JSON",
118
+ "FIRESTORE_KEYFILE", "FIRESTORE_KEYFILE_JSON"
119
+ )
120
+ end
121
+
122
+ config.add_field! :project_id, default_project, match: String, allow_nil: true
123
+ config.add_alias! :project, :project_id
124
+ config.add_field! :credentials, default_creds,
125
+ match: [String, Hash, Google::Auth::Credentials],
126
+ allow_nil: true
127
+ config.add_alias! :keyfile, :credentials
128
+ config.add_field! :scope, nil, match: [String, Array]
129
+ config.add_field! :timeout, nil, match: Integer
130
+ config.add_field! :client_config, nil, match: Hash
131
+ end
@@ -15,6 +15,8 @@
15
15
 
16
16
  require "google-cloud-firestore"
17
17
  require "google/cloud/firestore/client"
18
+ require "google/cloud/config"
19
+ require "google/cloud/env"
18
20
 
19
21
  module Google
20
22
  module Cloud
@@ -469,7 +471,7 @@ module Google
469
471
  # present, the default project for the credentials is used.
470
472
  # @param [String, Hash, Google::Auth::Credentials] credentials The path to
471
473
  # the keyfile as a String, the contents of the keyfile as a Hash, or a
472
- # Google::Auth::Credentials object. (See {Datastore::Credentials})
474
+ # Google::Auth::Credentials object. (See {Firestore::Credentials})
473
475
  # @param [String, Array<String>] scope The OAuth 2.0 scopes controlling
474
476
  # the set of resources and operations that the connection can access.
475
477
  # See [Using OAuth 2.0 to Access Google
@@ -494,20 +496,67 @@ module Google
494
496
  #
495
497
  def self.new project_id: nil, credentials: nil, scope: nil, timeout: nil,
496
498
  client_config: nil, project: nil, keyfile: nil
497
- project_id ||= (project || Firestore::Service.default_project_id)
499
+ project_id ||= (project || default_project_id)
498
500
  project_id = project_id.to_s # Always cast to a string
499
- fail ArgumentError, "project_id is missing" if project_id.empty?
501
+ raise ArgumentError, "project_id is missing" if project_id.empty?
500
502
 
501
- credentials ||= keyfile
502
- credentials ||= Firestore::Credentials.default(scope: scope)
503
+ scope ||= configure.scope
504
+ timeout ||= configure.timeout
505
+ client_config ||= configure.client_config
506
+
507
+ credentials ||= (keyfile || default_credentials(scope: scope))
503
508
  unless credentials.is_a? Google::Auth::Credentials
504
509
  credentials = Firestore::Credentials.new credentials, scope: scope
505
510
  end
506
511
 
507
- Firestore::Client.new \
508
- Firestore::Service.new \
509
- project_id, credentials,
510
- timeout: timeout, client_config: client_config
512
+ Firestore::Client.new(
513
+ Firestore::Service.new(
514
+ project_id, credentials, timeout: timeout,
515
+ client_config: client_config
516
+ )
517
+ )
518
+ end
519
+
520
+ ##
521
+ # Configure the Google Cloud Firestore library.
522
+ #
523
+ # The following Firestore configuration parameters are supported:
524
+ #
525
+ # * `project_id` - (String) Identifier for a Firestore project. (The
526
+ # parameter `project` is considered deprecated, but may also be used.)
527
+ # * `credentials` - (String, Hash, Google::Auth::Credentials) The path to
528
+ # the keyfile as a String, the contents of the keyfile as a Hash, or a
529
+ # Google::Auth::Credentials object. (See {Firestore::Credentials}) (The
530
+ # parameter `keyfile` is considered deprecated, but may also be used.)
531
+ # * `scope` - (String, Array<String>) The OAuth 2.0 scopes controlling
532
+ # the set of resources and operations that the connection can access.
533
+ # * `timeout` - (Integer) Default timeout to use in requests.
534
+ # * `client_config` - (Hash) A hash of values to override the default
535
+ # behavior of the API client.
536
+ #
537
+ # @return [Google::Cloud::Config] The configuration object the
538
+ # Google::Cloud::Firestore library uses.
539
+ #
540
+ def self.configure
541
+ yield Google::Cloud.configure.firestore if block_given?
542
+
543
+ Google::Cloud.configure.firestore
544
+ end
545
+
546
+ ##
547
+ # @private Default project.
548
+ def self.default_project_id
549
+ Google::Cloud.configure.firestore.project_id ||
550
+ Google::Cloud.configure.project_id ||
551
+ Google::Cloud.env.project_id
552
+ end
553
+
554
+ ##
555
+ # @private Default credentials.
556
+ def self.default_credentials scope: nil
557
+ Google::Cloud.configure.firestore.credentials ||
558
+ Google::Cloud.configure.credentials ||
559
+ Firestore::Credentials.default(scope: scope)
511
560
  end
512
561
  end
513
562
  end
@@ -67,7 +67,7 @@ module Google
67
67
  def firestore
68
68
  @client
69
69
  end
70
- alias_method :client, :firestore
70
+ alias client firestore
71
71
 
72
72
  # @!group Modifications
73
73
 
@@ -388,7 +388,8 @@ module Google
388
388
  doc_path = coalesce_doc_path_argument doc
389
389
 
390
390
  @writes << Convert.write_for_delete(
391
- doc_path, exists: exists, update_time: update_time)
391
+ doc_path, exists: exists, update_time: update_time
392
+ )
392
393
 
393
394
  nil
394
395
  end
@@ -441,20 +442,20 @@ module Google
441
442
  ##
442
443
  # @private
443
444
  def ensure_not_closed!
444
- fail "batch is closed" if closed?
445
+ raise "batch is closed" if closed?
445
446
  end
446
447
 
447
448
  ##
448
449
  # @private Raise an error unless an database available.
449
450
  def ensure_client!
450
- fail "Must have active connection to service" unless firestore
451
+ raise "Must have active connection to service" unless firestore
451
452
  end
452
453
 
453
454
  ##
454
455
  # @private Raise an error unless an active connection to the service
455
456
  # is available.
456
457
  def ensure_service!
457
- fail "Must have active connection to service" unless service
458
+ raise "Must have active connection to service" unless service
458
459
  end
459
460
  end
460
461
  end
@@ -107,7 +107,7 @@ module Google
107
107
  collection_ids = service.list_collections "#{path}/documents"
108
108
  collection_ids.each { |collection_id| yield col(collection_id) }
109
109
  end
110
- alias_method :collections, :cols
110
+ alias collections cols
111
111
 
112
112
  ##
113
113
  # Retrieves a collection.
@@ -130,13 +130,13 @@ module Google
130
130
  #
131
131
  def col collection_path
132
132
  if collection_path.to_s.split("/").count.even?
133
- fail ArgumentError, "collection_path must refer to a collection."
133
+ raise ArgumentError, "collection_path must refer to a collection."
134
134
  end
135
135
 
136
136
  CollectionReference.from_path \
137
137
  "#{path}/documents/#{collection_path}", self
138
138
  end
139
- alias_method :collection, :col
139
+ alias collection col
140
140
 
141
141
  ##
142
142
  # Retrieves a document reference.
@@ -158,14 +158,14 @@ module Google
158
158
  #
159
159
  def doc document_path
160
160
  if document_path.to_s.split("/").count.odd?
161
- fail ArgumentError, "document_path must refer to a document."
161
+ raise ArgumentError, "document_path must refer to a document."
162
162
  end
163
163
 
164
164
  doc_path = "#{path}/documents/#{document_path}"
165
165
 
166
166
  DocumentReference.from_path doc_path, self
167
167
  end
168
- alias_method :document, :doc
168
+ alias document doc
169
169
 
170
170
  ##
171
171
  # Retrieves a list of document snapshots.
@@ -205,9 +205,9 @@ module Google
205
205
  yield DocumentSnapshot.from_batch_result(result, self)
206
206
  end
207
207
  end
208
- alias_method :get_docs, :get_all
209
- alias_method :get_documents, :get_all
210
- alias_method :find, :get_all
208
+ alias get_docs get_all
209
+ alias get_documents get_all
210
+ alias find get_all
211
211
 
212
212
  ##
213
213
  # Creates a field path object representing the sentinel ID of a
@@ -401,7 +401,7 @@ module Google
401
401
  return nil if retries > 0
402
402
  # Re-raise error.
403
403
  raise err
404
- rescue => err
404
+ rescue StandardError => err
405
405
  # Rollback transaction when handling unexpected error
406
406
  transaction.rollback rescue nil
407
407
  # Re-raise error.
@@ -416,17 +416,13 @@ module Google
416
416
  ##
417
417
  # @private
418
418
  def coalesce_get_argument obj
419
- if obj.is_a?(String) || obj.is_a?(Symbol)
420
- if obj.to_s.split("/").count.even?
421
- return doc obj # Convert a DocumentReference
422
- else
423
- return col obj # Convert to Query
424
- end
425
- end
426
-
427
419
  return obj.ref if obj.is_a? DocumentSnapshot
428
420
 
429
- obj
421
+ return obj unless obj.is_a?(String) || obj.is_a?(Symbol)
422
+
423
+ return doc obj if obj.to_s.split("/").count.even?
424
+
425
+ col obj # Convert to CollectionReference
430
426
  end
431
427
 
432
428
  ##
@@ -441,7 +437,7 @@ module Google
441
437
  # @private Raise an error unless an active connection to the service is
442
438
  # available.
443
439
  def ensure_service!
444
- fail "Must have active connection to service" unless service
440
+ raise "Must have active connection to service" unless service
445
441
  end
446
442
  end
447
443
  end
@@ -124,7 +124,7 @@ module Google
124
124
  ensure_client!
125
125
  client.doc "#{collection_path}/#{document_path}"
126
126
  end
127
- alias_method :document, :doc
127
+ alias document doc
128
128
 
129
129
  ##
130
130
  # The document reference or database the collection reference belongs
@@ -241,7 +241,7 @@ module Google
241
241
  ##
242
242
  # @private Raise an error unless an database available.
243
243
  def ensure_client!
244
- fail "Must have active connection to service" unless client
244
+ raise "Must have active connection to service" unless client
245
245
  end
246
246
  end
247
247
  end
@@ -118,7 +118,7 @@ module Google
118
118
  encoded_content = Base64.strict_encode64 content
119
119
  Google::Firestore::V1beta1::Value.new bytes_value: encoded_content
120
120
  else
121
- fail ArgumentError,
121
+ raise ArgumentError,
122
122
  "A value of type #{obj.class} is not supported."
123
123
  end
124
124
  end
@@ -127,9 +127,9 @@ module Google
127
127
  writes = []
128
128
 
129
129
  if is_field_value_nested data, :delete
130
- fail ArgumentError, "DELETE not allowed on create"
130
+ raise ArgumentError, "DELETE not allowed on create"
131
131
  end
132
- fail ArgumentError, "data is required" unless data.is_a? Hash
132
+ raise ArgumentError, "data is required" unless data.is_a? Hash
133
133
 
134
134
  data, server_time_paths = remove_field_value_from data, :server_time
135
135
 
@@ -159,7 +159,7 @@ module Google
159
159
  end
160
160
 
161
161
  def writes_for_set doc_path, data, merge: nil
162
- fail ArgumentError, "data is required" unless data.is_a? Hash
162
+ raise ArgumentError, "data is required" unless data.is_a? Hash
163
163
 
164
164
  if merge
165
165
  if merge == true
@@ -178,7 +178,7 @@ module Google
178
178
 
179
179
  data, delete_paths = remove_field_value_from data, :delete
180
180
  if delete_paths.any?
181
- fail ArgumentError, "DELETE not allowed on set"
181
+ raise ArgumentError, "DELETE not allowed on set"
182
182
  end
183
183
 
184
184
  data, server_time_paths = remove_field_value_from data, :server_time
@@ -197,7 +197,7 @@ module Google
197
197
  end
198
198
 
199
199
  def writes_for_set_merge doc_path, data, field_paths
200
- fail ArgumentError, "data is required" unless data.is_a? Hash
200
+ raise ArgumentError, "data is required" unless data.is_a? Hash
201
201
 
202
202
  writes = []
203
203
 
@@ -214,7 +214,7 @@ module Google
214
214
  end
215
215
  end
216
216
  all_valid_check = all_valid_check.include? false
217
- fail ArgumentError, "all fields must be in data" if all_valid_check
217
+ raise ArgumentError, "all fields must be in data" if all_valid_check
218
218
 
219
219
  data, delete_paths = remove_field_value_from data, :delete
220
220
  data, server_time_paths = remove_field_value_from data, :server_time
@@ -230,7 +230,7 @@ module Google
230
230
  end
231
231
  end
232
232
  delete_valid_check = delete_valid_check.include? false
233
- fail ArgumentError, "deleted field not included in merge" if delete_valid_check
233
+ raise ArgumentError, "deleted field not included in merge" if delete_valid_check
234
234
 
235
235
  # Choose only the data there are field paths for
236
236
  field_paths -= delete_paths
@@ -239,7 +239,7 @@ module Google
239
239
 
240
240
  if data.empty?
241
241
  if server_time_paths.empty?
242
- fail ArgumentError, "data required for set with merge"
242
+ raise ArgumentError, "data required for set with merge"
243
243
  end
244
244
  else
245
245
  writes << Google::Firestore::V1beta1::Write.new(
@@ -261,7 +261,7 @@ module Google
261
261
  def writes_for_update doc_path, data, update_time: nil
262
262
  writes = []
263
263
 
264
- fail ArgumentError, "data is required" unless data.is_a? Hash
264
+ raise ArgumentError, "data is required" unless data.is_a? Hash
265
265
 
266
266
  # Convert data to use FieldPath
267
267
  new_data_pairs = data.map do |key, value|
@@ -272,14 +272,14 @@ module Google
272
272
  # Duplicate field paths check
273
273
  dup_keys = new_data_pairs.map(&:first).map(&:formatted_string)
274
274
  if dup_keys.size != dup_keys.uniq.size
275
- fail ArgumentError, "duplicate field paths"
275
+ raise ArgumentError, "duplicate field paths"
276
276
  end
277
277
  dup_keys.each do |field_path|
278
278
  prefix_check = dup_keys.select do |this_path|
279
279
  this_path.start_with? "#{field_path}."
280
280
  end
281
281
  if prefix_check.any?
282
- fail ArgumentError, "one field cannot be a prefix of another"
282
+ raise ArgumentError, "one field cannot be a prefix of another"
283
283
  end
284
284
  end
285
285
 
@@ -298,7 +298,7 @@ module Google
298
298
  root_server_time_paths.map!(&:first)
299
299
 
300
300
  data, nested_deletes = remove_field_value_from data, :delete
301
- fail ArgumentError, "DELETE cannot be nested" if nested_deletes.any?
301
+ raise ArgumentError, "DELETE cannot be nested" if nested_deletes.any?
302
302
 
303
303
  data, nested_server_time_paths = remove_field_value_from data, :server_time
304
304
 
@@ -307,11 +307,11 @@ module Google
307
307
 
308
308
  field_paths = (field_paths - (field_paths - identify_all_file_paths(data)) + delete_paths).uniq
309
309
  field_paths.each do |field_path|
310
- fail ArgumentError, "empty paths not allowed" if field_path.fields.empty?
310
+ raise ArgumentError, "empty paths not allowed" if field_path.fields.empty?
311
311
  end
312
312
 
313
313
  if data.empty? && delete_paths.empty? && server_time_paths.empty?
314
- fail ArgumentError, "data is required"
314
+ raise ArgumentError, "data is required"
315
315
  end
316
316
 
317
317
  if data.any? || delete_paths.any?
@@ -346,7 +346,7 @@ module Google
346
346
 
347
347
  def write_for_delete doc_path, exists: nil, update_time: nil
348
348
  if !exists.nil? && !update_time.nil?
349
- fail ArgumentError, "cannot specify both exists and update_time"
349
+ raise ArgumentError, "cannot specify both exists and update_time"
350
350
  end
351
351
 
352
352
  write = Google::Firestore::V1beta1::Write.new(
@@ -402,7 +402,7 @@ module Google
402
402
  else
403
403
  if value.is_a? Array
404
404
  if is_field_value_nested value, field_value_type
405
- fail ArgumentError, "cannot nest #{field_value_type} under arrays"
405
+ raise ArgumentError, "cannot nest #{field_value_type} under arrays"
406
406
  end
407
407
  end
408
408
 
@@ -515,7 +515,7 @@ module Google
515
515
  tmp_dup = dup_hash
516
516
  last_field = nil
517
517
  field_path.fields.map(&:to_sym).each do |field|
518
- fail ArgumentError, "empty paths not allowed" if field.empty?
518
+ raise ArgumentError, "empty paths not allowed" if field.empty?
519
519
  tmp_dup = tmp_dup[last_field] unless last_field.nil?
520
520
  last_field = field
521
521
  tmp_dup[field] ||= {}
@@ -19,16 +19,35 @@ module Google
19
19
  module Cloud
20
20
  module Firestore
21
21
  ##
22
- # @private Represents the OAuth 2.0 signing logic for Firestore.
22
+ # # Credentials
23
+ #
24
+ # Represents the authentication and authorization used to connect to the
25
+ # Firestore service.
26
+ #
27
+ # @example
28
+ # require "google/cloud/firestore"
29
+ #
30
+ # keyfile = "/path/to/keyfile.json"
31
+ # creds = Google::Cloud::Firestore::Credentials.new keyfile
32
+ #
33
+ # firestore = Google::Cloud::Firestore.new(
34
+ # project_id: "my-project",
35
+ # credentials: creds
36
+ # )
37
+ #
38
+ # firestore.project_id #=> "my-project"
39
+ #
23
40
  class Credentials < Google::Auth::Credentials
24
- SCOPE = ["https://www.googleapis.com/auth/datastore"]
25
- PATH_ENV_VARS = %w(FIRESTORE_CREDENTIALS FIRESTORE_KEYFILE
41
+ SCOPE = ["https://www.googleapis.com/auth/datastore"].freeze
42
+ PATH_ENV_VARS = %w[FIRESTORE_CREDENTIALS FIRESTORE_KEYFILE
26
43
  GOOGLE_CLOUD_CREDENTIALS GOOGLE_CLOUD_KEYFILE
27
- GCLOUD_KEYFILE)
28
- JSON_ENV_VARS = %w(FIRESTORE_CREDENTIALS_JSON FIRESTORE_KEYFILE_JSON
44
+ GCLOUD_KEYFILE].freeze
45
+ JSON_ENV_VARS = %w[FIRESTORE_CREDENTIALS_JSON FIRESTORE_KEYFILE_JSON
29
46
  GOOGLE_CLOUD_CREDENTIALS_JSON
30
47
  GOOGLE_CLOUD_KEYFILE_JSON
31
- GCLOUD_KEYFILE_JSON)
48
+ GCLOUD_KEYFILE_JSON].freeze
49
+ DEFAULT_PATHS = \
50
+ ["~/.config/gcloud/application_default_credentials.json"].freeze
32
51
  end
33
52
  end
34
53
  end
@@ -95,7 +95,7 @@ module Google
95
95
  collection_ids = service.list_collections path
96
96
  collection_ids.each { |collection_id| yield col(collection_id) }
97
97
  end
98
- alias_method :collections, :cols
98
+ alias collections cols
99
99
 
100
100
  ##
101
101
  # Retrieves a collection nested under the document snapshot.
@@ -118,12 +118,12 @@ module Google
118
118
  #
119
119
  def col collection_path
120
120
  if collection_path.to_s.split("/").count.even?
121
- fail ArgumentError, "collection_path must refer to a collection."
121
+ raise ArgumentError, "collection_path must refer to a collection."
122
122
  end
123
123
 
124
124
  CollectionReference.from_path "#{path}/#{collection_path}", client
125
125
  end
126
- alias_method :collection, :col
126
+ alias collection col
127
127
 
128
128
  ##
129
129
  # Retrieve the document data.
@@ -453,14 +453,14 @@ module Google
453
453
  ##
454
454
  # @private Raise an error unless an database available.
455
455
  def ensure_client!
456
- fail "Must have active connection to service" unless client
456
+ raise "Must have active connection to service" unless client
457
457
  end
458
458
 
459
459
  ##
460
460
  # @private Raise an error unless an active connection to the service
461
461
  # is available.
462
462
  def ensure_service!
463
- fail "Must have active connection to service" unless service
463
+ raise "Must have active connection to service" unless service
464
464
  end
465
465
  end
466
466
  end
@@ -91,7 +91,7 @@ module Google
91
91
  def ref
92
92
  @ref
93
93
  end
94
- alias_method :reference, :ref
94
+ alias reference ref
95
95
 
96
96
  ##
97
97
  # The collection the document snapshot belongs to.
@@ -136,7 +136,7 @@ module Google
136
136
  return nil if missing?
137
137
  Convert.fields_to_hash grpc.fields, ref.client
138
138
  end
139
- alias_method :fields, :data
139
+ alias fields data
140
140
 
141
141
  ##
142
142
  # Retrieves the document data.
@@ -196,14 +196,15 @@ module Google
196
196
 
197
197
  nodes.each do |node|
198
198
  unless selected_data.is_a? Hash
199
- fail ArgumentError,
200
- "#{field_path.formatted_string} is not contained in the data"
199
+ err_msg = "#{field_path.formatted_string} is not " \
200
+ "contained in the data"
201
+ raise ArgumentError, err_msg
201
202
  end
202
203
  selected_data = selected_data[node]
203
204
  end
204
205
  selected_data
205
206
  end
206
- alias_method :[], :get
207
+ alias [] get
207
208
 
208
209
  # @!endgroup
209
210
 
@@ -218,7 +219,7 @@ module Google
218
219
  return nil if missing?
219
220
  Convert.timestamp_to_time grpc.create_time
220
221
  end
221
- alias_method :create_time, :created_at
222
+ alias create_time created_at
222
223
 
223
224
  ##
224
225
  # The time at which the document was last changed.
@@ -232,7 +233,7 @@ module Google
232
233
  return nil if missing?
233
234
  Convert.timestamp_to_time grpc.update_time
234
235
  end
235
- alias_method :update_time, :updated_at
236
+ alias update_time updated_at
236
237
 
237
238
  ##
238
239
  # The time at which the document was read.
@@ -244,7 +245,7 @@ module Google
244
245
  def read_at
245
246
  @read_at
246
247
  end
247
- alias_method :read_time, :read_at
248
+ alias read_time read_at
248
249
 
249
250
  ##
250
251
  # Determines whether the document exists.
@@ -59,7 +59,7 @@ module Google
59
59
  def initialize *fields
60
60
  @fields = fields.flatten.map(&:to_s)
61
61
  @fields.each do |field|
62
- fail ArgumentError, "empty paths not allowed" if field.empty?
62
+ raise ArgumentError, "empty paths not allowed" if field.empty?
63
63
  end
64
64
  end
65
65
 
@@ -167,7 +167,7 @@ module Google
167
167
  fields = String(dotted_string).split(".")
168
168
  fields.each do |field|
169
169
  if INVALID_FIELD_PATH_CHARS.match field
170
- fail ArgumentError, "invalid character, use FieldPath instead"
170
+ raise ArgumentError, "invalid character, use FieldPath instead"
171
171
  end
172
172
  end
173
173
  new fields
@@ -21,11 +21,11 @@ module Google
21
21
  ##
22
22
  # @private Helper module for generating random values
23
23
  module Generate
24
- CHARS = [*"a".."z", *"A".."Z", *"0".."9"]
24
+ CHARS = [*"a".."z", *"A".."Z", *"0".."9"].freeze
25
25
 
26
26
  def self.unique_id length: 20, chars: CHARS
27
27
  size = chars.size
28
- length.times.map do
28
+ Array.new(length) do
29
29
  chars[SecureRandom.random_number(size)]
30
30
  end.join
31
31
  end
@@ -130,7 +130,7 @@ module Google
130
130
  new_query ||= StructuredQuery.new
131
131
 
132
132
  if new_query.from.empty?
133
- fail "missing collection_id to specify descendants."
133
+ raise "missing collection_id to specify descendants."
134
134
  end
135
135
 
136
136
  new_query.from.last.all_descendants = true
@@ -166,7 +166,7 @@ module Google
166
166
  new_query ||= StructuredQuery.new
167
167
 
168
168
  if new_query.from.empty?
169
- fail "missing collection_id to specify descendants."
169
+ raise "missing collection_id to specify descendants."
170
170
  end
171
171
 
172
172
  new_query.from.last.all_descendants = false
@@ -283,11 +283,12 @@ module Google
283
283
  field: StructuredQuery::FieldReference.new(
284
284
  field_path: field.formatted_string
285
285
  ),
286
- direction: order_direction(direction))
286
+ direction: order_direction(direction)
287
+ )
287
288
 
288
289
  Query.start new_query, parent_path, client
289
290
  end
290
- alias_method :order_by, :order
291
+ alias order_by order
291
292
 
292
293
  ##
293
294
  # Skips to an offset in a query. If the current query already has
@@ -389,12 +390,12 @@ module Google
389
390
 
390
391
  values = values.flatten.map { |value| Convert.raw_to_value value }
391
392
  new_query.start_at = Google::Firestore::V1beta1::Cursor.new(
392
- values: values, before: true)
393
+ values: values, before: true
394
+ )
393
395
 
394
396
  Query.start new_query, parent_path, client
395
397
  end
396
398
 
397
-
398
399
  ##
399
400
  # Starts query results after a set of field values. The result set will
400
401
  # not include the document specified by `values`.
@@ -431,7 +432,8 @@ module Google
431
432
 
432
433
  values = values.flatten.map { |value| Convert.raw_to_value value }
433
434
  new_query.start_at = Google::Firestore::V1beta1::Cursor.new(
434
- values: values, before: false)
435
+ values: values, before: false
436
+ )
435
437
 
436
438
  Query.start new_query, parent_path, client
437
439
  end
@@ -472,7 +474,8 @@ module Google
472
474
 
473
475
  values = values.flatten.map { |value| Convert.raw_to_value value }
474
476
  new_query.end_at = Google::Firestore::V1beta1::Cursor.new(
475
- values: values, before: true)
477
+ values: values, before: true
478
+ )
476
479
 
477
480
  Query.start new_query, parent_path, client
478
481
  end
@@ -513,7 +516,8 @@ module Google
513
516
 
514
517
  values = values.flatten.map { |value| Convert.raw_to_value value }
515
518
  new_query.end_at = Google::Firestore::V1beta1::Cursor.new(
516
- values: values, before: false)
519
+ values: values, before: false
520
+ )
517
521
 
518
522
  Query.start new_query, parent_path, client
519
523
  end
@@ -552,7 +556,7 @@ module Google
552
556
  yield DocumentSnapshot.from_query_result(result, self)
553
557
  end
554
558
  end
555
- alias_method :run, :get
559
+ alias run get
556
560
 
557
561
  ##
558
562
  # @private Start a new Query.
@@ -567,25 +571,36 @@ module Google
567
571
 
568
572
  protected
569
573
 
574
+ ##
575
+ # @private
570
576
  StructuredQuery = Google::Firestore::V1beta1::StructuredQuery
571
577
 
578
+ ##
579
+ # @private
572
580
  FILTER_OPS = {
573
- "<" => :LESS_THAN,
574
- "lt" => :LESS_THAN,
575
- "<=" => :LESS_THAN_OR_EQUAL,
576
- "lte" => :LESS_THAN_OR_EQUAL,
577
- ">" => :GREATER_THAN,
578
- "gt" => :GREATER_THAN,
579
- ">=" => :GREATER_THAN_OR_EQUAL,
580
- "gte" => :GREATER_THAN_OR_EQUAL,
581
- "=" => :EQUAL,
582
- "==" => :EQUAL,
583
- "eq" => :EQUAL,
584
- "eql" => :EQUAL,
585
- "is" => :EQUAL }
586
- UNARY_NIL_VALUES = [nil, :null, :nil]
587
- UNARY_NAN_VALUES = [:nan, Float::NAN]
588
- UNARY_VALUES = UNARY_NIL_VALUES + UNARY_NAN_VALUES
581
+ "<" => :LESS_THAN,
582
+ "lt" => :LESS_THAN,
583
+ "<=" => :LESS_THAN_OR_EQUAL,
584
+ "lte" => :LESS_THAN_OR_EQUAL,
585
+ ">" => :GREATER_THAN,
586
+ "gt" => :GREATER_THAN,
587
+ ">=" => :GREATER_THAN_OR_EQUAL,
588
+ "gte" => :GREATER_THAN_OR_EQUAL,
589
+ "=" => :EQUAL,
590
+ "==" => :EQUAL,
591
+ "eq" => :EQUAL,
592
+ "eql" => :EQUAL,
593
+ "is" => :EQUAL
594
+ }.freeze
595
+ ##
596
+ # @private
597
+ UNARY_NIL_VALUES = [nil, :null, :nil].freeze
598
+ ##
599
+ # @private
600
+ UNARY_NAN_VALUES = [:nan, Float::NAN].freeze
601
+ ##
602
+ # @private
603
+ UNARY_VALUES = (UNARY_NIL_VALUES + UNARY_NAN_VALUES).freeze
589
604
 
590
605
  def filter name, op, value
591
606
  field = StructuredQuery::FieldReference.new field_path: name.to_s
@@ -594,7 +609,8 @@ module Google
594
609
  is_value_nan = value.respond_to?(:nan?) && value.nan?
595
610
  if UNARY_VALUES.include?(value) || is_value_nan
596
611
  if op != :EQUAL
597
- fail ArgumentError, "can only check equality for #{value} values."
612
+ raise ArgumentError,
613
+ "can only check equality for #{value} values."
598
614
  end
599
615
 
600
616
  op = :IS_NULL
@@ -628,7 +644,7 @@ module Google
628
644
  ##
629
645
  # @private Raise an error unless an database available.
630
646
  def ensure_client!
631
- fail "Must have active connection to service" unless client
647
+ raise "Must have active connection to service" unless client
632
648
  end
633
649
 
634
650
  ##
@@ -643,7 +659,7 @@ module Google
643
659
  # @private Raise an error unless an active connection to the service
644
660
  # is available.
645
661
  def ensure_service!
646
- fail "Must have active connection to service" unless service
662
+ raise "Must have active connection to service" unless service
647
663
  end
648
664
  end
649
665
  end
@@ -28,15 +28,6 @@ module Google
28
28
  class Service
29
29
  attr_accessor :project, :credentials, :timeout, :client_config
30
30
 
31
- ##
32
- # @private Default project.
33
- def self.default_project_id
34
- ENV["FIRESTORE_PROJECT"] ||
35
- ENV["GOOGLE_CLOUD_PROJECT"] ||
36
- ENV["GCLOUD_PROJECT"] ||
37
- Google::Cloud.env.project_id
38
- end
39
-
40
31
  ##
41
32
  # Creates a new Service instance.
42
33
  def initialize project, credentials, timeout: nil, client_config: nil
@@ -53,7 +44,8 @@ module Google
53
44
  timeout: timeout,
54
45
  client_config: client_config,
55
46
  lib_name: "gccl",
56
- lib_version: Google::Cloud::Firestore::VERSION)
47
+ lib_version: Google::Cloud::Firestore::VERSION
48
+ )
57
49
  end
58
50
 
59
51
  def get_documents document_paths, mask: nil, transaction: nil
@@ -74,7 +74,7 @@ module Google
74
74
  def firestore
75
75
  @client
76
76
  end
77
- alias_method :client, :firestore
77
+ alias client firestore
78
78
 
79
79
  # @!group Access
80
80
 
@@ -120,9 +120,9 @@ module Google
120
120
  yield DocumentSnapshot.from_batch_result(result, self)
121
121
  end
122
122
  end
123
- alias_method :get_docs, :get_all
124
- alias_method :get_documents, :get_all
125
- alias_method :find, :get_all
123
+ alias get_docs get_all
124
+ alias get_documents get_all
125
+ alias find get_all
126
126
 
127
127
  ##
128
128
  # Retrieves document snapshots for the given value. Valid values can be
@@ -233,7 +233,7 @@ module Google
233
233
  yield DocumentSnapshot.from_query_result(result, self)
234
234
  end
235
235
  end
236
- alias_method :run, :get
236
+ alias run get
237
237
 
238
238
  # @!endgroup
239
239
 
@@ -562,7 +562,8 @@ module Google
562
562
  doc_path = coalesce_doc_path_argument doc
563
563
 
564
564
  @writes << Convert.write_for_delete(
565
- doc_path, exists: exists, update_time: update_time)
565
+ doc_path, exists: exists, update_time: update_time
566
+ )
566
567
 
567
568
  nil
568
569
  end
@@ -637,17 +638,13 @@ module Google
637
638
  ##
638
639
  # @private
639
640
  def coalesce_get_argument obj
640
- if obj.is_a?(String) || obj.is_a?(Symbol)
641
- if obj.to_s.split("/").count.even?
642
- return client.doc obj # Convert a DocumentReference
643
- else
644
- return client.col obj # Convert to CollectionReference
645
- end
646
- end
647
-
648
641
  return obj.ref if obj.is_a? DocumentSnapshot
649
642
 
650
- obj
643
+ return obj unless obj.is_a?(String) || obj.is_a?(Symbol)
644
+
645
+ return client.doc obj if obj.to_s.split("/").count.even?
646
+
647
+ client.col obj # Convert to CollectionReference
651
648
  end
652
649
 
653
650
  ##
@@ -695,7 +692,7 @@ module Google
695
692
  ##
696
693
  # @private
697
694
  def ensure_not_closed!
698
- fail "transaction is closed" if closed?
695
+ raise "transaction is closed" if closed?
699
696
  end
700
697
 
701
698
  ##
@@ -711,14 +708,14 @@ module Google
711
708
  ##
712
709
  # @private Raise an error unless an database available.
713
710
  def ensure_client!
714
- fail "Must have active connection to service" unless firestore
711
+ raise "Must have active connection to service" unless firestore
715
712
  end
716
713
 
717
714
  ##
718
715
  # @private Raise an error unless an active connection to the service
719
716
  # is available.
720
717
  def ensure_service!
721
- fail "Must have active connection to service" unless service
718
+ raise "Must have active connection to service" unless service
722
719
  end
723
720
  end
724
721
  end
@@ -16,7 +16,7 @@
16
16
  module Google
17
17
  module Cloud
18
18
  module Firestore
19
- VERSION = "0.20.0"
19
+ VERSION = "0.21.0".freeze
20
20
  end
21
21
  end
22
22
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-cloud-firestore
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.20.0
4
+ version: 0.21.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Google Inc
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-10 00:00:00.000000000 Z
11
+ date: 2018-02-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: google-cloud-core
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.1'
19
+ version: '1.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.1'
26
+ version: '1.2'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: google-gax
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -126,16 +126,16 @@ dependencies:
126
126
  name: rubocop
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
- - - "<="
129
+ - - "~>"
130
130
  - !ruby/object:Gem::Version
131
- version: 0.35.1
131
+ version: 0.50.0
132
132
  type: :development
133
133
  prerelease: false
134
134
  version_requirements: !ruby/object:Gem::Requirement
135
135
  requirements:
136
- - - "<="
136
+ - - "~>"
137
137
  - !ruby/object:Gem::Version
138
- version: 0.35.1
138
+ version: 0.50.0
139
139
  - !ruby/object:Gem::Dependency
140
140
  name: simplecov
141
141
  requirement: !ruby/object:Gem::Requirement
@@ -244,7 +244,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
244
244
  version: '0'
245
245
  requirements: []
246
246
  rubyforge_project:
247
- rubygems_version: 2.7.4
247
+ rubygems_version: 2.7.6
248
248
  signing_key:
249
249
  specification_version: 4
250
250
  summary: API Client library for Google Cloud Firestore API