Class: Firefighter::RealtimeDatabase

Inherits:
Object
  • Object
show all
Includes:
Web
Defined in:
lib/firefighter/realtime_database.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Web

#call, #logger, #sse

Constructor Details

#initialize(db_name:, db_secret:) ⇒ RealtimeDatabase

Returns a new instance of RealtimeDatabase.



12
13
14
15
# File 'lib/firefighter/realtime_database.rb', line 12

def initialize(db_name:, db_secret:)
  @db_name = db_name
  @db_secret = db_secret
end

Class Method Details

.from_envObject



5
6
7
8
9
10
# File 'lib/firefighter/realtime_database.rb', line 5

def self.from_env
  new(
    db_name: ENV['FIREBASE_WEB_DB_NAME'],
    db_secret: ENV['FIREBASE_WEB_DB_SECRET']
  )
end

Instance Method Details

#add(path, data) ⇒ Object



35
36
37
38
# File 'lib/firefighter/realtime_database.rb', line 35

def add(path, data)
  url = endpoint(path)
  call(:post, url, data)
end

#delete(path) ⇒ Object



40
41
42
43
# File 'lib/firefighter/realtime_database.rb', line 40

def delete(path)
  url = endpoint(path)
  call(:delete, url)
end

#listen(path) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/firefighter/realtime_database.rb', line 17

def listen(path)
  url = endpoint(path)
  sse(url) do |connection, event, payload|
    data = JSON.parse(payload)
    yield connection, event, data['path'], data['data']
  end
end

#read(path) ⇒ Object



25
26
27
28
# File 'lib/firefighter/realtime_database.rb', line 25

def read(path)
  url = endpoint(path)
  call(:get, url)
end

#write(path, data) ⇒ Object



30
31
32
33
# File 'lib/firefighter/realtime_database.rb', line 30

def write(path, data)
  url = endpoint(path)
  call(:put, url, data)
end