Class: MyFirebase

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeMyFirebase

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

#clientObject (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.

Parameters:

  • namespace (String)

    The namespace to retrieve.

  • query (Hash) (defaults to: {})

    The query parameters to include in the request.

Returns:

  • (Firebase::Response)

    The response from the Firebase server.



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.

Examples:

Update reservation status and delivery status

tracking_key = "reservations/#{reservation.id}"
attr = {
  status: reservation.status_as_symbol,
  delivery_status: 'Driver::FINDING_DRIVER',
  facebook_event_id: TrackFbConversionWorker.reservation_to_event_id(reservation),
}
firebase.update(tracking_key, attr)

Parameters:

  • namespace (String)

    The namespace to update.

  • data (Hash)

    The data to update.

  • query (Hash) (defaults to: {})

    The query parameters to include in the request.

Returns:

  • (Firebase::Response)

    The response from the Firebase server.



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
  data_with_timestamp = add_created_at_timestamp(namespace, data)
  client.update(namespace, data_with_timestamp, query)
end

#validated_update(namespace, data, query = {}) ⇒ Firebase::Response

Updates the data at the specified namespace with the given data, after validating the status.

Examples:

Update reservation status and delivery status

tracking_key = "reservations/#{reservation.id}"
attr = {
  status: reservation.status_as_symbol,
  delivery_status: 'Driver::FINDING_DRIVER',
  facebook_event_id: TrackFbConversionWorker.reservation_to_event_id(reservation),
}
firebase.validated_update(tracking_key, attr)

Parameters:

  • namespace (String)

    The namespace to update.

  • data (Hash)

    The data to update.

  • query (Hash) (defaults to: {})

    The query parameters to include in the request.

Returns:

  • (Firebase::Response)

    The response from the Firebase server.



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