ons-firestore 1.1.0 → 1.2.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: '0077852086ffe77e35e89820a68feae7bc2711f74f0727dca47587344c8d0a41'
4
- data.tar.gz: 3fa7333f538ac6f6ed5038d8c6190a02473c7f451ca2dfe04b4e5d151ec27ceb
3
+ metadata.gz: b1409410ed2eeda669760d9de820c52475fdc0430dc2ca073830587102d8cb23
4
+ data.tar.gz: 956e2ad87753f9a8e13dca996ad3d1f3be3362689e993ac077c7d5865b34e413
5
5
  SHA512:
6
- metadata.gz: f01fb9331db325667ac3e60f6e1a9310f05cc3fdd0025462913bfc5c328191fefc1deb5b2de921aacb48377ade65cb090af4ac622b493b14c5a8df49d74bcedf
7
- data.tar.gz: cf2ca0fb7c3fd1093c56d12388174e726133d1e8df59fe8da319d97e18d1cd089c6aaba7e31ed8eee17591d960aaa7f84bdd16f9825106c3de8a69dee80791cb
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
- require 'logger'
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 [Enumberator] list of documents within the collection
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 if value.is_a?(Array)
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
- logger = Logger.new($stderr)
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
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Class representing an error that occurred whilst reading or writing Firestore data.
4
+ class FirestoreError < StandardError
5
+ end
@@ -3,7 +3,7 @@
3
3
  module ONSFirestore
4
4
  module Version
5
5
  MAJOR = 1
6
- MINOR = 1
6
+ MINOR = 2
7
7
  TINY = 0
8
8
  end
9
9
  VERSION = [Version::MAJOR, Version::MINOR, Version::TINY].compact * '.'
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.1.0
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-03 00:00:00.000000000 Z
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: