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
# 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?('/')
  default_header = {
    'Content-Type' => 'application/json'
  }
  if auth && valid_json?(auth)
    # Using Admin SDK private key
    scopes = %w(https://www.googleapis.com/auth/firebase.database https://www.googleapis.com/auth/userinfo.email)
    credentials = Google::Auth::DefaultCredentials.make_creds(json_key_io: StringIO.new(auth), scope: scopes)
    default_header = credentials.apply(default_header)
  else
    # Using deprecated Database Secret
    @auth = auth
  end
  @request = HTTPClient.new({
    :base_url => base_uri,
    :default_header => default_header
  })
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



53
54
55
# File 'lib/firebase.rb', line 53

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

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

Returns the data at path



42
43
44
# File 'lib/firebase.rb', line 42

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"}


48
49
50
# File 'lib/firebase.rb', line 48

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' }


37
38
39
# File 'lib/firebase.rb', line 37

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' }


59
60
61
# File 'lib/firebase.rb', line 59

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