class Firebase

most methods have a static version that make use of Firebase.url = ‘global url’ to create a Firebase ref.

see www.firebase.com/docs/ios/guide/login/facebook.html for more info (that’s where this code came from)

see www.firebase.com/docs/ios/guide/login/github.html for more info (that’s where this code came from)

see www.firebase.com/docs/ios/guide/login/twitter.html for more info (that’s where this code came from)

Constants

ERRORS

Public Class Methods

auth_data() click to toggle source
# File lib/firebase/firebase_auth.rb, line 5
def self.auth_data
  Firebase.new.auth_data
end
authenticate(token, options={}, &block) click to toggle source

@example

Firebase.authenticate('secrettoken') do |error, auth_data|
  if auth_data
    # authenticated
  end
end
# File lib/firebase/firebase_auth.rb, line 18
def self.authenticate(token, options={}, &block)
  Firebase.new.authenticate(token, options, &block)
end
authenticated?(&block) click to toggle source
# File lib/firebase/firebase_auth.rb, line 46
def self.authenticated?(&block)
  Firebase.new.authenticated?(&block)
end
connected?(&block) click to toggle source
# File lib/firebase/firebase.rb, line 77
def self.connected?(&block)
  Firebase.new.connected?(&block)
end
convert_event_type(event_type) click to toggle source
# File lib/firebase/firebase.rb, line 7
def self.convert_event_type(event_type)
  case event_type
  when :child_added, :added, FEventTypeChildAdded
    return FEventTypeChildAdded
  when :child_moved, :moved, FEventTypeChildMoved
    return FEventTypeChildMoved
  when :child_changed, :changed, FEventTypeChildChanged
    return FEventTypeChildChanged
  when :child_removed, :removed, FEventTypeChildRemoved
    return FEventTypeChildRemoved
  when :value, FEventTypeValue
    return FEventTypeValue
  else
    NSLog("Unknown event type #{event_type.inspect}")
  end
  return event_type
end
create_user(credentials, &block) click to toggle source
# File lib/firebase/firebase_auth.rb, line 68
def self.create_user(credentials, &block)
  Firebase.new.create_user(credentials, &block)
end
create_user_and_login(credentials, &block) click to toggle source
# File lib/firebase/firebase_auth.rb, line 87
def self.create_user_and_login(credentials, &block)
  Firebase.new.create_user_and_login(credentials, &block)
end
dispatch_queue=(queue) click to toggle source

@example

Firebase.dispatch_queue(queue)
# => Firebase.setDispatchQueue(queue)
# File lib/firebase/firebase.rb, line 66
def self.dispatch_queue=(queue)
  if queue.is_a?(Dispatch::Queue)
    queue = queue.dispatch_object
  end
  setDispatchQueue(queue)
end
github_token() click to toggle source
# File lib/firebase/firebase_github_helper.rb, line 7
def self.github_token
  @github_token
end
github_token=(value) click to toggle source
# File lib/firebase/firebase_github_helper.rb, line 4
def self.github_token=(value)
  @github_token = value
end
logout(&block) click to toggle source
# File lib/firebase/firebase_auth.rb, line 35
def self.logout(&block)
  Firebase.new.logout(&block)
end
new(url=nil) click to toggle source
Calls superclass method
# File lib/firebase/firebase.rb, line 53
def self.new(url=nil)
  if url.nil?
    @shared ||= alloc.initWithUrl(@url)
  elsif url
    alloc.initWithUrl(url)
  else
    super
  end
end
off_auth(handler) click to toggle source

You should call this when you no longer need ‘authenticated?` data.

# File lib/firebase/firebase_auth.rb, line 61
def self.off_auth(handler)
  Firebase.new.off_auth(handler)
end
offline!() click to toggle source
# File lib/firebase/firebase.rb, line 90
def self.offline!
  Firebase.goOffline
end
online!() click to toggle source
# File lib/firebase/firebase.rb, line 98
def self.online!
  Firebase.goOnline
end
open_facebook_session(options={}, &block) click to toggle source
# File lib/firebase/firebase_facebook_helper.rb, line 4
def self.open_facebook_session(options={}, &block)
  self.new.open_facebook_session(options={}, &block)
end
open_github_session(options={}, &block) click to toggle source
# File lib/firebase/firebase_github_helper.rb, line 10
def self.open_github_session(options={}, &block)
  self.new.open_github_session(options={}, &block)
end
open_twitter_session(options={}, &block) click to toggle source
# File lib/firebase/firebase_twitter_helper.rb, line 10
def self.open_twitter_session(options={}, &block)
  self.new.open_twitter_session(options={}, &block)
end
remove_user(credentials, &block) click to toggle source
# File lib/firebase/firebase_auth.rb, line 141
def self.remove_user(credentials, &block)
  Firebase.new.remove_user(credentials, &block)
end
send_password_reset(credentials, &block) click to toggle source
# File lib/firebase/firebase_auth.rb, line 134
def self.send_password_reset(credentials, &block)
  Firebase.new.send_password_reset(credentials, &block)
end
twitter_api_key() click to toggle source
# File lib/firebase/firebase_twitter_helper.rb, line 7
def self.twitter_api_key
  @twitter_api_key
end
twitter_api_key=(value) click to toggle source
# File lib/firebase/firebase_twitter_helper.rb, line 4
def self.twitter_api_key=(value)
  @twitter_api_key = value
end
update_user(credentials, &block) click to toggle source
# File lib/firebase/firebase_auth.rb, line 108
def self.update_user(credentials, &block)
  Firebase.new.update_user(credentials, &block)
end
update_user_email(credentials, &block) click to toggle source
# File lib/firebase/firebase_auth.rb, line 121
def self.update_user_email(credentials, &block)
  Firebase.new.update_user_email(credentials, &block)
end
url() click to toggle source
# File lib/firebase/firebase.rb, line 49
def self.url
  @url
end
url=(url) click to toggle source
# File lib/firebase/firebase.rb, line 25
def self.url=(url)
  if url.start_with?('http://')
    raise "Invalid URL #{url.inspect} in #{__method__}: URL scheme should be 'https://', not 'http://'"
  elsif url.start_with?('https://')
    # all good
  elsif url =~ %r'^\w+://'
    raise "Invalid URL #{url.inspect} in #{__method__}: URL scheme should be 'https://', not '#{$~}'"
  else
    url = "https://#{url}"
  end

  # should we support `Firebase.url = 'myapp/path/to/child/'` ?  I'm gonna say
  # NO for now...
  unless url.include?('.firebaseio.com/') || url.include?('.firebaseio-demo.com/')
    after_scheme = url.index('//') + 2
    if url[after_scheme..-1].include?('/')
      raise "Invalid URL #{url.inspect} in #{__method__}: URL does not include 'firebaseio.com'"
    end
    url = "#{url}.firebaseio.com/"
  end

  @url = url
end

Public Instance Methods

<<(value) click to toggle source
# File lib/firebase/firebase.rb, line 158
def <<(value)
  ref = childByAutoId
  ref.set(value)
  return ref
end
[](*names) click to toggle source

@example

firebase = Firebase.new('http://..../')
firebase[]  # => childByAutoId
firebase['fred']  # => childByAppendingPath('fred')
firebase['fred', 'name', 'first']  # => childByAppendingPath('fred/name/first')
firebase['fred']['name']['first']
# => childByAppendingPath('fred').childByAppendingPath('name').childByAppendingPath('first'),
# same as => childByAppendingPath('fred/name/first')
# File lib/firebase/firebase.rb, line 133
def [](*names)
  if names.length == 0
    childByAutoId
  else
    childByAppendingPath(names.join('/'))
  end
end
[]=(key, value) click to toggle source
# File lib/firebase/firebase.rb, line 145
def []=(key, value)
  childByAppendingPath(key).set(value)
end
auth_data() click to toggle source
# File lib/firebase/firebase_auth.rb, line 8
def auth_data
  authData
end
authenticate(token, options={}, &and_then) click to toggle source
# File lib/firebase/firebase_auth.rb, line 21
def authenticate(token, options={}, &and_then)
  and_then ||= options[:completion]
  disconnect_block = options[:disconnect]
  if disconnect_block || and_then.arity < 2
    NSLog('Warning!  The Firebase authWithCredential method is deprecated.')
    NSLog('Instead of using a completion and cancel block, pass one block:')
    NSLog('fb.auth(token) do |error, auth_data| .. end')
    authWithCredential(token, withCompletionBlock:and_then, withCancelBlock: disconnect_block)
  else
    authWithCustomToken(token, withCompletionBlock: and_then)
  end
  return self
end
authenticated?(&block) click to toggle source

checks the authenticated status. If you pass a block the observeAuthEventWithBlock is used to determine the status. If you don’t pass a block, this method returns true or false.

# File lib/firebase/firebase_auth.rb, line 52
def authenticated?(&block)
  if block
    observeAuthEventWithBlock(block)
  else
    !!authData
  end
end
cancel_disconnect(&and_then) click to toggle source
# File lib/firebase/firebase.rb, line 214
def cancel_disconnect(&and_then)
  if block_given?
    cancelDisconnectOperationsWithCompletionBlock(and_then)
  else
    cancelDisconnectOperations
  end
  return self
end
child(name) click to toggle source
# File lib/firebase/firebase.rb, line 141
def child(name)
  childByAppendingPath(name)
end
clear!(&and_then) click to toggle source
# File lib/firebase/firebase.rb, line 149
def clear!(&and_then)
  if block_given?
    removeValueWithCompletionBlock(and_then)
  else
    removeValue
  end
  return self
end
connected?(&block) click to toggle source
# File lib/firebase/firebase.rb, line 80
def connected?(&block)
  if block
    connected_state.on(:value) do |snapshot|
      block.call(snapshot.value?)
    end
  else
    self.root['.info/connected']
  end
end
connected_state(&block) click to toggle source
# File lib/firebase/firebase.rb, line 73
def connected_state(&block)
  connected?
end
create_user(credentials, &block) click to toggle source
# File lib/firebase/firebase_auth.rb, line 71
def create_user(credentials, &block)
  raise ":email is required in #{__method__}" unless credentials.key?(:email)
  raise ":password is required in #{__method__}" unless credentials.key?(:password)
  email = credentials[:email]
  password = credentials[:password]
  begin
    if block && block.arity == 2
      createUser(email, password: password, withValueCompletionBlock: block)
    else
      createUser(email, password: password, withCompletionBlock: block)
    end
  rescue RuntimeError => e
    block.call(e, nil)
  end
end
create_user_and_login(credentials, &block) click to toggle source
# File lib/firebase/firebase_auth.rb, line 90
def create_user_and_login(credentials, &block)
  raise ":email is required in #{__method__}" unless credentials.key?(:email)
  raise ":password is required in #{__method__}" unless credentials.key?(:password)
  email = credentials[:email]
  password = credentials[:password]
  begin
    createUser(email, password: password, withCompletionBlock: -> (error) do
      if error
        block.call(error, nil)
      else
        login(credentials, &block)
      end
    end)
  rescue RuntimeError => e
    block.call(e, nil)
  end
end
inspect() click to toggle source
# File lib/firebase/firebase.rb, line 261
def inspect
  "#<#{self.class}:0x#{self.object_id.to_s(16)}>"
end
login(credentials, &block) click to toggle source
# File lib/firebase/firebase_auth.rb, line 156
def login(credentials, &block)
  raise ":email is required in #{__method__}" unless credentials.key?(:email)
  raise ":password is required in #{__method__}" unless credentials.key?(:password)
  email = credentials[:email]
  password = credentials[:password]
  begin
    authUser(email, password: password, withCompletionBlock: block)
  rescue RuntimeError => e
    block.call(e, nil)
  end
end
login_anonymously(&block) click to toggle source
# File lib/firebase/firebase_auth.rb, line 152
def login_anonymously(&block)
  authAnonymouslyWithCompletionBlock(block)
end
login_to_facebook(credentials, &block) click to toggle source
# File lib/firebase/firebase_auth.rb, line 189
def login_to_facebook(credentials, &block)
  if credentials.is_a?(NSString)
    token = credentials
  else
    token = credentials[:token]
  end
  raise ":token is required in #{__method__}" unless token

  authWithOAuthProvider('facebook', token: token, withCompletionBlock: block)
end
login_to_github(credentials, &block) click to toggle source
# File lib/firebase/firebase_auth.rb, line 214
def login_to_github(credentials, &block)
  if credentials.is_a?(NSString)
    token = credentials
  else
    token = credentials[:token]
  end
  raise ":token is required in #{__method__}" unless token

  authWithOAuthProvider('github', token: token, withCompletionBlock: block)
end
login_to_google(credentials, &block) click to toggle source
# File lib/firebase/firebase_auth.rb, line 225
def login_to_google(credentials, &block)
  if credentials.is_a?(NSString)
    token = credentials
  else
    token = credentials[:token]
  end
  raise ":token is required in #{__method__}" unless token

  authWithOAuthProvider('google', token: token, withCompletionBlock: block)
end
login_to_oauth(provider, parameters={}, &block) click to toggle source
# File lib/firebase/firebase_auth.rb, line 168
def login_to_oauth(provider, parameters={}, &block)
  if parameters.is_a?(NSString)
    token = parameters
  elsif parameters.key?(:token)
    token = parameters[:token]
  else
    token = nil
  end

  if token
    authWithOAuthProvider(provider, token: token, withCompletionBlock: block)
  else
    objc_params = {}
    parameters.each do |key, value|
      # explicitly convert :sym to 'sym'
      objc_params[key.to_s] = value
    end
    authWithOAuthProvider(provider, parameters: objc_params, withCompletionBlock: block)
  end
end
login_to_twitter(credentials, &block) click to toggle source
# File lib/firebase/firebase_auth.rb, line 200
def login_to_twitter(credentials, &block)
  oauth_token = credentials[:token] || credentials[:oauth_token]
  oauth_token_secret = credentials[:secret] || credentials[:oauth_token_secret]
  raise ":token is required in #{__method__}" unless oauth_token
  raise ":secret is required in #{__method__}" unless oauth_token_secret

  provider = 'twitter'
  options = {
    'oauth_token' => oauth_token,
    'oauth_token_secret' => oauth_token_secret,
  }
  login_to_oauth(provider, options, &block)
end
logout(&block) click to toggle source
# File lib/firebase/firebase_auth.rb, line 38
def logout(&block)
  if block_given?
    unauthWithCompletionBlock(block)
  else
    unauth
  end
end
off_auth(handler) click to toggle source
# File lib/firebase/firebase_auth.rb, line 64
def off_auth(handler)
  removeAuthEventObserverWithHandle(handler)
end
offline!() click to toggle source
# File lib/firebase/firebase.rb, line 94
def offline!
  Firebase.goOffline
end
on_disconnect(value, &and_then) click to toggle source
# File lib/firebase/firebase.rb, line 227
def on_disconnect(value, &and_then)
  if block_given?
    if value.nil?
      onDisconnectRemoveValueWithCompletionBlock(and_then)
    elsif NSDictionary === value
      onDisconnectUpdateChildValues(value, withCompletionBlock: and_then)
    else
      onDisconnectSetValue(value, withCompletionBlock: and_then)
    end
  else
    if value.nil?
      onDisconnectRemoveValue
    elsif NSDictionary === value
      onDisconnectUpdateChildValues(value)
    else
      onDisconnectSetValue(value)
    end
  end
  return self
end
online!() click to toggle source
# File lib/firebase/firebase.rb, line 102
def online!
  Firebase.goOnline
end
open_facebook_session(options={}, &block) click to toggle source
# File lib/firebase/firebase_facebook_helper.rb, line 8
def open_facebook_session(options={}, &block)
  ref = self
  permissions = options[:permissions] || ['email']
  fb_login = FBSDKLoginManager.alloc.init
  fb_login.logInWithReadPermissions(permissions, 
    handler: -> (facebookResult, facebookError) do
      if facebookError
        block.call(facebookError, nil)
      elsif facebookResult.isCancelled
        block.call("Facebook login got cancelled.", nil)
      else
        access_token = FBSDKAccessToken.currentAccessToken.tokenString

        ref.authWithOAuthProvider('facebook', token: access_token, withCompletionBlock:block)
      end
    end)
  nil
end
open_github_session(options={}, &block) click to toggle source
# File lib/firebase/firebase_github_helper.rb, line 14
def open_github_session(options={}, &block)
  ref = self
  token = options[:token] || Firebase.github_token
  raise "token is required in #{__method__}" unless token
  firebase_ref.login_to_github('github', token)
end
open_twitter_session(options={}, &block) click to toggle source
# File lib/firebase/firebase_twitter_helper.rb, line 14
def open_twitter_session(options={}, &block)
  ref = self
  api_key = options[:api_key] || Firebase.twitter_api_key
  raise "api_key is required in #{__method__}" unless api_key

  helper = Motion::Firebase::TwitterAuthHelper.new(ref, api_key)
  helper.select_twitter_account(&block)
  nil
end
priority(value, &and_then) click to toggle source
# File lib/firebase/firebase.rb, line 187
def priority(value, &and_then)
  if block_given?
    setPriority(value, withCompletionBlock: and_then)
  else
    setPriority(value)
  end
  return self
end
priority=(value) click to toggle source
# File lib/firebase/firebase.rb, line 183
def priority=(value)
  priority(value)
end
push(value, &and_then) click to toggle source
# File lib/firebase/firebase.rb, line 164
def push(value, &and_then)
  ref = childByAutoId
  ref.set(value, &and_then)
  return ref
end
remove_on_disconnect(&and_then) click to toggle source
# File lib/firebase/firebase.rb, line 223
def remove_on_disconnect(&and_then)
  on_disconnect(nil, &and_then)
end
remove_user(credentials, &block) click to toggle source
# File lib/firebase/firebase_auth.rb, line 144
def remove_user(credentials, &block)
  raise ":email is required in #{__method__}" unless credentials.key?(:email)
  raise ":password is required in #{__method__}" unless credentials.key?(:password)
  email = credentials[:email]
  password = credentials[:password]
  removeUser(email, password: password, withCompletionBlock: block)
end
send_password_reset(email, &block) click to toggle source
# File lib/firebase/firebase_auth.rb, line 137
def send_password_reset(email, &block)
  resetPasswordForUser(email, withCompletionBlock: block)
end
set(value, &and_then) click to toggle source
# File lib/firebase/firebase.rb, line 174
def set(value, &and_then)
  if block_given?
    setValue(value, withCompletionBlock: and_then)
  else
    setValue(value)
  end
  return self
end
to_s() click to toggle source
# File lib/firebase/firebase.rb, line 257
def to_s
  description
end
transaction(options={}, &transaction) click to toggle source
# File lib/firebase/firebase.rb, line 106
def transaction(options={}, &transaction)
  transaction = transaction || options[:transaction]
  completion_block = options[:completion]
  with_local_events = options[:local]
  if with_local_events.nil?
    if block_given?
      runTransactionBlock(transaction, andCompletionBlock: completion_block)
    else
      runTransactionBlock(transaction)
    end
  else
    if block_given?
      runTransactionBlock(transaction, andCompletionBlock: completion_block, withLocalEvents: with_local_events)
    else
      runTransactionBlock(transaction, withLocalEvents: with_local_events)
    end
  end
end
update(values, &and_then) click to toggle source
# File lib/firebase/firebase.rb, line 205
def update(values, &and_then)
  if block_given?
    updateChildValues(values, withCompletionBlock: and_then)
  else
    updateChildValues(values)
  end
  return self
end
update_user(credentials, &block) click to toggle source
# File lib/firebase/firebase_auth.rb, line 111
def update_user(credentials, &block)
  raise ":email is required in #{__method__}" unless credentials.key?(:email)
  raise ":old_password is required in #{__method__}" unless credentials.key?(:old_password)
  raise ":new_password is required in #{__method__}" unless credentials.key?(:new_password)
  email = credentials[:email]
  old_password = credentials[:old_password]
  new_password = credentials[:new_password]
  changePasswordForUser(email, fromOld: old_password, toNew: new_password, withCompletionBlock: block)
end
update_user_email(credentials, &block) click to toggle source
# File lib/firebase/firebase_auth.rb, line 124
def update_user_email(credentials, &block)
  raise ":email is required in #{__method__}" unless credentials.key?(:email)
  raise ":password is required in #{__method__}" unless credentials.key?(:password)
  raise ":new_email is required in #{__method__}" unless credentials.key?(:new_email)
  email = credentials[:email]
  password = credentials[:password]
  new_email = credentials[:new_email]
  changeEmailForUser(email, password: password, toNewEmail: new_email, withCompletionBlock: block)
end
value=(value) click to toggle source
# File lib/firebase/firebase.rb, line 170
def value=(value)
  setValue(value)
end