ons-firestore 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/ons-firestore/firestore.rb +9 -7
- data/lib/ons-firestore/firestore_error.rb +5 -0
- data/lib/ons-firestore/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b1409410ed2eeda669760d9de820c52475fdc0430dc2ca073830587102d8cb23
|
4
|
+
data.tar.gz: 956e2ad87753f9a8e13dca996ad3d1f3be3362689e993ac077c7d5865b34e413
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b9f264f04c6e0f64ef1befda1294236a2153aa64f4862d63c0d4addfca5ac0931a4358c37f707a7786388616849a29e56970523bcd831938087f58add0eb5409
|
7
|
+
data.tar.gz: 11e1f4f2512587bc54de61647cae2e007f1cbac20b0cbd7f50bddb32a19b265de464ce2e1e90bbd1eddf5dd0d620d406aede404052f2df8b1629deabe6fe9694
|
@@ -1,7 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'google/cloud/firestore'
|
4
|
-
|
4
|
+
|
5
|
+
require_relative 'firestore_error'
|
5
6
|
|
6
7
|
# Class to manage access to Firestore.
|
7
8
|
class Firestore
|
@@ -22,7 +23,7 @@ class Firestore
|
|
22
23
|
# Returns all Firestore documents within a collection.
|
23
24
|
#
|
24
25
|
# @param collection_name [String] the name of the Firestore collection containing the documents
|
25
|
-
# @return [
|
26
|
+
# @return [Enumerator] list of documents within the collection
|
26
27
|
# @raise [ArgumentError] if collection_name is nil
|
27
28
|
def all_documents(collection_name)
|
28
29
|
raise ArgumentError.new('collection_name cannot be nil') if collection_name.nil?
|
@@ -49,12 +50,15 @@ class Firestore
|
|
49
50
|
# @param document_name [String] the name of the Firestore document
|
50
51
|
# @return [Object] document data
|
51
52
|
# @raise [ArgumentError] if collection_name or document_name are nil
|
53
|
+
# @raise [FirestoreError] if the +data+ key isn't present within the Firestore document
|
52
54
|
def read_document(collection_name, document_name)
|
53
55
|
raise ArgumentError.new('collection_name cannot be nil') if collection_name.nil?
|
54
56
|
raise ArgumentError.new('document_name cannot be nil') if document_name.nil?
|
55
57
|
|
56
58
|
document = @client.col(collection_name).doc(document_name)
|
57
59
|
snapshot = document.get
|
60
|
+
raise FirestoreError('data key is missing') if snapshot.data.nil?
|
61
|
+
|
58
62
|
snapshot[:data]
|
59
63
|
end
|
60
64
|
|
@@ -67,6 +71,7 @@ class Firestore
|
|
67
71
|
# @param document_name [String] the name of the Firestore document
|
68
72
|
# @param data [Object] data to save to the Firestore document
|
69
73
|
# @raise [ArgumentError] if collection_name or document_name are nil
|
74
|
+
# @raise [FirestoreError] if an error occurred whilst saving the document
|
70
75
|
def save_document(collection_name, document_name, data)
|
71
76
|
raise ArgumentError.new('collection_name cannot be nil') if collection_name.nil?
|
72
77
|
raise ArgumentError.new('document_name cannot be nil') if document_name.nil?
|
@@ -80,17 +85,14 @@ class Firestore
|
|
80
85
|
if data.is_a?(Hash)
|
81
86
|
hash_data = {}
|
82
87
|
data.each do |key, value|
|
83
|
-
hash_data[key] = value
|
84
|
-
hash_data[key] = value.map(&:to_h) unless value.is_a?(Array)
|
88
|
+
hash_data[key] = value.class.method_defined?(:to_h) ? value.map(&:to_h) : value
|
85
89
|
end
|
86
90
|
end
|
87
91
|
|
88
92
|
begin
|
89
93
|
document.set({ data: hash_data, updated: Time.now.strftime(DATE_TIME_FORMAT) })
|
90
94
|
rescue StandardError => e
|
91
|
-
|
92
|
-
logger.error("Failed to save Firestore document #{document_name} in collection #{collection_name}: #{e.message}")
|
93
|
-
logger.error(e.backtrace.join("\n"))
|
95
|
+
raise FirestoreError("Failed to save Firestore document #{document_name} in collection #{collection_name}: #{e.message}")
|
94
96
|
end
|
95
97
|
end
|
96
98
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ons-firestore
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Topley
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-05-
|
11
|
+
date: 2022-05-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: google-cloud-firestore
|
@@ -78,6 +78,7 @@ files:
|
|
78
78
|
- README.md
|
79
79
|
- lib/ons-firestore.rb
|
80
80
|
- lib/ons-firestore/firestore.rb
|
81
|
+
- lib/ons-firestore/firestore_error.rb
|
81
82
|
- lib/ons-firestore/version.rb
|
82
83
|
homepage: https://github.com/ONSdigital/ons-firestore
|
83
84
|
licenses:
|