Class: Firebase::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/firebase.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_uri, auth = nil) ⇒ Client

Returns a new instance of Client.


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/firebase.rb', line 12

def initialize(base_uri, auth=nil)
  if base_uri !~ URI::regexp(%w(https))
    raise ArgumentError.new('base_uri must be a valid https uri')
  end
  base_uri += '/' unless base_uri.end_with?('/')
  @request = HTTPClient.new({
    :base_url => base_uri,
    :default_header => {
      'Content-Type' => 'application/json'
    }
  })
  if auth && valid_json?(auth)
    # Using Admin SDK service account
    @credentials = Google::Auth::DefaultCredentials.make_creds(
      json_key_io: StringIO.new(auth),
      scope: %w(https://www.googleapis.com/auth/firebase.database https://www.googleapis.com/auth/userinfo.email)
    )
    @credentials.apply!(@request.default_header)
    @expires_at = @credentials.issued_at + 0.95 * @credentials.expires_in
  else
    # Using deprecated Database Secret
    @secret = auth
  end
end

Instance Attribute Details

#authObject (readonly)

Returns the value of attribute auth


10
11
12
# File 'lib/firebase.rb', line 10

def auth
  @auth
end

#requestObject (readonly)

Returns the value of attribute request


10
11
12
# File 'lib/firebase.rb', line 10

def request
  @request
end

Instance Method Details

#delete(path, query = {}) ⇒ Object

Deletes the data at path and returs true


55
56
57
# File 'lib/firebase.rb', line 55

def delete(path, query={})
  process :delete, path, nil, query
end

#get(path, query = {}) ⇒ Object

Returns the data at path


44
45
46
# File 'lib/firebase.rb', line 44

def get(path, query={})
  process :get, path, nil, query
end

#push(path, data, query = {}) ⇒ Object

Writes the data, returns the key name of the data added

Firebase.push('users', { 'age' => 18}) => {"name":"-INOQPH-aV_psbk3ZXEX"}

50
51
52
# File 'lib/firebase.rb', line 50

def push(path, data, query={})
  process :post, path, data, query
end

#set(path, data, query = {}) ⇒ Object

Writes and returns the data

Firebase.set('users/info', { 'name' => 'Oscar' }) => { 'name' => 'Oscar' }

39
40
41
# File 'lib/firebase.rb', line 39

def set(path, data, query={})
  process :put, path, data, query
end

#update(path, data, query = {}) ⇒ Object

Write the data at path but does not delete ommited children. Returns the data

Firebase.update('users/info', { 'name' => 'Oscar' }) => { 'name' => 'Oscar' }

61
62
63
# File 'lib/firebase.rb', line 61

def update(path, data, query={})
  process :patch, path, data, query
end