Class: Firebase

Inherits:
Object
  • Object
show all
Defined in:
lib/firebase/firebase_auth.rb,
lib/firebase/firebase.rb,
lib/firebase/firebase_github_helper.rb,
lib/firebase/firebase_twitter_helper.rb,
lib/firebase/firebase_facebook_helper.rb

Overview

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

Constant Summary collapse

ERRORS =
{
  -9999 => :email_in_use,
  -2 => :wrong_password,
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.auth_dataObject



5
6
7
# File 'lib/firebase/firebase_auth.rb', line 5

def self.auth_data
  Firebase.new.auth_data
end

.authenticate(token, options = {}, &block) ⇒ Object

Examples:

Firebase.authenticate('secrettoken') do |error, auth_data|
  if auth_data
    # authenticated
  end
end


18
19
20
# File 'lib/firebase/firebase_auth.rb', line 18

def self.authenticate(token, options={}, &block)
  Firebase.new.authenticate(token, options, &block)
end

.authenticated?(&block) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/firebase/firebase_auth.rb', line 46

def self.authenticated?(&block)
  Firebase.new.authenticated?(&block)
end

.connected?(&block) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/firebase/firebase.rb', line 77

def self.connected?(&block)
  Firebase.new.connected?(&block)
end

.convert_event_type(event_type) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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) ⇒ Object



68
69
70
# 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) ⇒ Object



83
84
85
# File 'lib/firebase/firebase_auth.rb', line 83

def self.(credentials, &block)
  Firebase.new.(credentials, &block)
end

.dispatch_queue=(queue) ⇒ Object

Examples:

Firebase.dispatch_queue(queue)
# => Firebase.setDispatchQueue(queue)


66
67
68
69
70
71
# 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_tokenObject



7
8
9
# File 'lib/firebase/firebase_github_helper.rb', line 7

def self.github_token
  @github_token
end

.github_token=(value) ⇒ Object



4
5
6
# File 'lib/firebase/firebase_github_helper.rb', line 4

def self.github_token=(value)
  @github_token = value
end

.logout(&block) ⇒ Object



35
36
37
# File 'lib/firebase/firebase_auth.rb', line 35

def self.logout(&block)
  Firebase.new.logout(&block)
end

.new(url = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
# 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) ⇒ Object

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



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

def self.off_auth(handler)
  Firebase.new.off_auth(handler)
end

.offline!Object



90
91
92
# File 'lib/firebase/firebase.rb', line 90

def self.offline!
  Firebase.new.offline!
end

.online!Object



97
98
99
# File 'lib/firebase/firebase.rb', line 97

def self.online!
  Firebase.new.online!
end

.open_facebook_session(options = {}, &block) ⇒ Object



4
5
6
# 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) ⇒ Object



10
11
12
# 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) ⇒ Object



10
11
12
# 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) ⇒ Object



124
125
126
# File 'lib/firebase/firebase_auth.rb', line 124

def self.remove_user(credentials, &block)
  Firebase.new.remove_user(credentials, &block)
end

.send_password_reset(credentials, &block) ⇒ Object



117
118
119
# File 'lib/firebase/firebase_auth.rb', line 117

def self.send_password_reset(credentials, &block)
  Firebase.new.send_password_reset(credentials, &block)
end

.twitter_api_keyObject



7
8
9
# File 'lib/firebase/firebase_twitter_helper.rb', line 7

def self.twitter_api_key
  @twitter_api_key
end

.twitter_api_key=(value) ⇒ Object



4
5
6
# File 'lib/firebase/firebase_twitter_helper.rb', line 4

def self.twitter_api_key=(value)
  @twitter_api_key = value
end

.update_user(credentials, &block) ⇒ Object



104
105
106
# File 'lib/firebase/firebase_auth.rb', line 104

def self.update_user(credentials, &block)
  Firebase.new.update_user(credentials, &block)
end

.urlObject



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

def self.url
  @url
end

.url=(url) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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/')
    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

Instance Method Details

#<<(value) ⇒ Object



156
157
158
159
160
# File 'lib/firebase/firebase.rb', line 156

def <<(value)
  ref = childByAutoId
  ref.set(value)
  return ref
end

#[](*names) ⇒ Object

Examples:

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


131
132
133
134
135
136
137
# File 'lib/firebase/firebase.rb', line 131

def [](*names)
  if names.length == 0
    childByAutoId
  else
    childByAppendingPath(names.join('/'))
  end
end

#[]=(key, value) ⇒ Object



143
144
145
# File 'lib/firebase/firebase.rb', line 143

def []=(key, value)
  childByAppendingPath(key).set(value)
end

#auth_dataObject



8
9
10
# File 'lib/firebase/firebase_auth.rb', line 8

def auth_data
  authData
end

#authenticate(token, options = {}, &and_then) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# 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) ⇒ Boolean

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.

Returns:

  • (Boolean)


52
53
54
55
56
57
58
# File 'lib/firebase/firebase_auth.rb', line 52

def authenticated?(&block)
  if block
    observeAuthEventWithBlock(block)
  else
    !!authData
  end
end

#cancel_disconnect(&and_then) ⇒ Object



212
213
214
215
216
217
218
219
# File 'lib/firebase/firebase.rb', line 212

def cancel_disconnect(&and_then)
  if block_given?
    cancelDisconnectOperationsWithCompletionBlock(and_then)
  else
    cancelDisconnectOperations
  end
  return self
end

#child(name) ⇒ Object



139
140
141
# File 'lib/firebase/firebase.rb', line 139

def child(name)
  childByAppendingPath(name)
end

#clear!(&and_then) ⇒ Object



147
148
149
150
151
152
153
154
# File 'lib/firebase/firebase.rb', line 147

def clear!(&and_then)
  if block_given?
    removeValueWithCompletionBlock(and_then)
  else
    removeValue
  end
  return self
end

#connected?(&block) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
88
# 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) ⇒ Object



73
74
75
# File 'lib/firebase/firebase.rb', line 73

def connected_state(&block)
  connected?
end

#create_user(credentials, &block) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# 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
    createUserWithEmail(email, password: password, withCompletionBlock: block)
  rescue RuntimeError => e
    block.call(e, nil)
  end
end

#create_user_and_login(credentials, &block) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/firebase/firebase_auth.rb', line 86

def (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
    createUserWithEmail(email, password: password, withCompletionBlock: -> (error) do
      if error
        block.call(error, nil)
      else
        (credentials, &block)
      end
    end)
  rescue RuntimeError => e
    block.call(e, nil)
  end
end

#inspectObject



259
260
261
# File 'lib/firebase/firebase.rb', line 259

def inspect
  "#<#{self.class}:0x#{self.object_id.to_s(16)}>"
end

#login(credentials, &block) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/firebase/firebase_auth.rb', line 139

def (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) ⇒ Object



135
136
137
# File 'lib/firebase/firebase_auth.rb', line 135

def (&block)
  authAnonymouslyWithCompletionBlock(block)
end

#login_to_facebook(credentials, &block) ⇒ Object



172
173
174
175
176
177
178
179
180
181
# File 'lib/firebase/firebase_auth.rb', line 172

def (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) ⇒ Object



197
198
199
200
201
202
203
204
205
206
# File 'lib/firebase/firebase_auth.rb', line 197

def (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) ⇒ Object



208
209
210
211
212
213
214
215
216
217
# File 'lib/firebase/firebase_auth.rb', line 208

def (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) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/firebase/firebase_auth.rb', line 151

def (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) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/firebase/firebase_auth.rb', line 183

def (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,
  }
  (provider, options, &block)
end

#logout(&block) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/firebase/firebase_auth.rb', line 38

def logout(&block)
  if block_given?
    unauthWithCompletionBlock(block)
  else
    unauth
  end
end

#off_auth(handler) ⇒ Object



64
65
66
# File 'lib/firebase/firebase_auth.rb', line 64

def off_auth(handler)
  removeAuthEventObserverWithHandle(handler)
end

#offline!Object



93
94
95
# File 'lib/firebase/firebase.rb', line 93

def offline!
  self.goOffline
end

#on_disconnect(value, priority: priority, &and_then) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/firebase/firebase.rb', line 225

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!Object



100
101
102
# File 'lib/firebase/firebase.rb', line 100

def online!
  self.goOnline
end

#open_facebook_session(options = {}, &block) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/firebase/firebase_facebook_helper.rb', line 8

def open_facebook_session(options={}, &block)
  ref = self
  permissions = options[:permissions] || ['public_profile']
  allow_ui = options.fetch(:allow_login_ui, true)
  FBSession.openActiveSessionWithReadPermissions(permissions, allowLoginUI: allow_ui,
    completionHandler: -> (session, state, error) do
      if error
        block.call(error, nil)
      elsif state == FBSessionStateOpen
        token = session.accessTokenData.accessToken

        ref.authWithOAuthProvider('facebook', token:token, withCompletionBlock:block)
      end
    end)
  nil
end

#open_github_session(options = {}, &block) ⇒ Object



14
15
16
17
18
19
# 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.('github', token)
end

#open_twitter_session(options = {}, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
# 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.(&block)
  nil
end

#priority(value, &and_then) ⇒ Object



185
186
187
188
189
190
191
192
# File 'lib/firebase/firebase.rb', line 185

def priority(value, &and_then)
  if block_given?
    setPriority(value, withCompletionBlock: and_then)
  else
    setPriority(value)
  end
  return self
end

#priority=(value) ⇒ Object



181
182
183
# File 'lib/firebase/firebase.rb', line 181

def priority=(value)
  priority(value)
end

#push(value, &and_then) ⇒ Object



162
163
164
165
166
# File 'lib/firebase/firebase.rb', line 162

def push(value, &and_then)
  ref = childByAutoId
  ref.set(value, &and_then)
  return ref
end

#remove_on_disconnect(&and_then) ⇒ Object



221
222
223
# File 'lib/firebase/firebase.rb', line 221

def remove_on_disconnect(&and_then)
  on_disconnect(nil, &and_then)
end

#remove_user(credentials, &block) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/firebase/firebase_auth.rb', line 127

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) ⇒ Object



120
121
122
# File 'lib/firebase/firebase_auth.rb', line 120

def send_password_reset(email, &block)
  resetPasswordForUser(email, withCompletionBlock: block)
end

#set(value, priority: priority, &and_then) ⇒ Object



172
173
174
175
176
177
178
179
# File 'lib/firebase/firebase.rb', line 172

def set(value, &and_then)
  if block_given?
    setValue(value, withCompletionBlock: and_then)
  else
    setValue(value)
  end
  return self
end

#to_sObject



255
256
257
# File 'lib/firebase/firebase.rb', line 255

def to_s
  description
end

#transaction(options = {}, &transaction) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/firebase/firebase.rb', line 104

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) ⇒ Object



203
204
205
206
207
208
209
210
# File 'lib/firebase/firebase.rb', line 203

def update(values, &and_then)
  if block_given?
    updateChildValues(values, withCompletionBlock: and_then)
  else
    updateChildValues(values)
  end
  return self
end

#update_user(credentials, &block) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/firebase/firebase_auth.rb', line 107

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

#value=(value) ⇒ Object



168
169
170
# File 'lib/firebase/firebase.rb', line 168

def value=(value)
  setValue(value)
end