Class: MyFirebase
- Inherits:
-
Object
- Object
- MyFirebase
- Defined in:
- app/my_lib/my_firebase.rb
Overview
MyFirebase class is a wrapper around Firebase::Client that provides an interface to interact with Firebase database.
Defined Under Namespace
Classes: RetryError
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
-
#get(namespace, query = {}) ⇒ Firebase::Response
Retrieves the data at the specified namespace.
-
#initialize ⇒ MyFirebase
constructor
Initializes a new instance of MyFirebase.
-
#update(namespace, data, query = {}) ⇒ Firebase::Response
Updates the data at the specified namespace with the given data.
-
#validated_update(namespace, data, query = {}) ⇒ Firebase::Response
Updates the data at the specified namespace with the given data, after validating the status.
Constructor Details
#initialize ⇒ MyFirebase
Initializes a new instance of MyFirebase.
12 13 14 15 16 17 18 19 |
# File 'app/my_lib/my_firebase.rb', line 12 def initialize @client = if dev_mode? Firebase::Client.new(uri) else gcs = File.open(Rails.root.join('firebase_secret.json')).read Firebase::Client.new(uri, gcs) end end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
7 8 9 |
# File 'app/my_lib/my_firebase.rb', line 7 def client @client end |
Instance Method Details
#get(namespace, query = {}) ⇒ Firebase::Response
Retrieves the data at the specified namespace.
46 47 48 |
# File 'app/my_lib/my_firebase.rb', line 46 def get(namespace, query = {}) client.get(namespace, query) end |
#update(namespace, data, query = {}) ⇒ Firebase::Response
Updates the data at the specified namespace with the given data.
35 36 37 38 39 |
# File 'app/my_lib/my_firebase.rb', line 35 def update(namespace, data, query = {}) # Auto-inject created_at timestamp for records if not present = (namespace, data) client.update(namespace, , query) end |
#validated_update(namespace, data, query = {}) ⇒ Firebase::Response
Updates the data at the specified namespace with the given data, after validating the status.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'app/my_lib/my_firebase.rb', line 64 def validated_update(namespace, data, query = {}) # Get the status from the database status = get_status_from_database(namespace).to_sym status_from_param = data['status'].to_sym if status_from_param == status.to_sym # Update the data if the status is valid update(namespace, data, query) elsif status == :pending_arrival && status_from_param == :waiting_for_payment # do nothing else # Raise an error if the status is invalid raise RetryError, "Invalid status. current status is #{status}, but expected is #{data['status']}" end end |