ons-firestore 1.1.1 → 1.2.1

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
  SHA256:
3
- metadata.gz: 7071e0cea0d0adb25db556b56c07e5e3db7373efb6568d80fa86ad3de7f0553a
4
- data.tar.gz: fa2f124217f08065cbeec384fa2d2c896c4b6c935dde744861a3b03d10e77019
3
+ metadata.gz: ffae582656edb80c22028523bd1b307e9878e20450ee25a2bba2924da506eb19
4
+ data.tar.gz: cdbe8832c4f0a5d49fbf340530b8d1f3211ebf6bf50e49814b8e382ec7433224
5
5
  SHA512:
6
- metadata.gz: 5d67f09a82059d39cd90ef7e5b153562d68e71e27f1b379748bb139bd498c58f4ea4c548b4f5132a44ea666e7ef4f7f19ba356fe65d55b9937a8e8b3601266fa
7
- data.tar.gz: 7098b9941d613ca972ca4078987e306b7d7ad2d01a2d1a9a4b821e0efa88a40da0f664103caaa41713875c788cfb2c059cd9be20b6a136190c2a277a23e590c2
6
+ metadata.gz: fcbc120f1016150d54bff96aee74d5d2d772c807a7d17c6bb8da58f9728d899ef1ff543688c2b96229c1a87c63ff9fb5845100383f444a9970b1d71454378ce3
7
+ data.tar.gz: 6f408ae0027ca9fc946acfc006081a5b018f837e0f8a5ada236d4d0f8aa826ef46fb2ed2bcdc712b960e6ea72a9fe22625d581968f95f6da5f4bc7b717609945
@@ -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
@@ -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 = 1
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.1
4
+ version: 1.2.1
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: