class Firebase::Database

Constants

FIREBASE_URL_TEMPLATE

Attributes

auth[RW]
print[RW]
shallow[RW]

Public Class Methods

new() click to toggle source
# File lib/firebase-ruby/database.rb, line 10
def initialize()
end

Public Instance Methods

delete(path) click to toggle source
# File lib/firebase-ruby/database.rb, line 43
def delete(path)
  return operate(__method__, path)
end
get(path) click to toggle source
# File lib/firebase-ruby/database.rb, line 27
def get(path)
  return operate(__method__, path)
end
patch(path, data) click to toggle source
# File lib/firebase-ruby/database.rb, line 35
def patch(path, data)
  return operate(__method__, path, data)
end
post(path, data) click to toggle source
# File lib/firebase-ruby/database.rb, line 39
def post(path, data)
  return operate(__method__, path, data)
end
project_id() click to toggle source
# File lib/firebase-ruby/database.rb, line 21
def project_id
  return @project_id if @project_id
  return auth.project_id if auth
  return nil
end
project_id=(id) click to toggle source
# File lib/firebase-ruby/database.rb, line 17
def project_id=(id)
  @project_id = id
end
put(path, data) click to toggle source
# File lib/firebase-ruby/database.rb, line 31
def put(path, data)
  return operate(__method__, path, data)
end
set_auth_with_key(json: nil, path: nil) click to toggle source
# File lib/firebase-ruby/database.rb, line 13
def set_auth_with_key(json: nil, path: nil)
  @auth = Auth.new(json: json, path: path)
end

Private Instance Methods

format_path(path) click to toggle source
# File lib/firebase-ruby/database.rb, line 69
def format_path(path)
  path = '/' + path unless path.start_with?('/')
  return path + '.json'
end
handle_response_data(data) click to toggle source
# File lib/firebase-ruby/database.rb, line 74
def handle_response_data(data)
  if data[:code] != 200
    Firebase.logger.error("HTTP response error: #{data[:code]}\n#{data[:message]}")
    return nil
  end
  return JSON.parse(data[:body], {symbolize_names: true})
end
http() click to toggle source
# File lib/firebase-ruby/database.rb, line 60
def http
  unless @http
    url = FIREBASE_URL_TEMPLATE % project_id
    @http = Neko::HTTP.new(url, {'Content-Type' => 'application/json'})
  end
  @http.headers['Authorization'] = "Bearer #{auth.valid_token}"
  return @http
end
operate(method, path, data = nil) click to toggle source
# File lib/firebase-ruby/database.rb, line 49
def operate(method, path, data = nil)
  case method
  when :get, :delete
    res_data = http.public_send(method, path: format_path(path))
  when :put, :patch, :post
    data = JSON.fast_generate(data) if data.class == Hash
    res_data = http.public_send(method, path: format_path(path), body: data)
  end
  return handle_response_data(res_data)
end