Class: Atreides::FacebookUser

Inherits:
Object
  • Object
show all
Defined in:
app/models/atreides/facebook_user.rb

Class Method Summary collapse

Class Method Details

.authorize_page(page_id) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/models/atreides/facebook_user.rb', line 67

def authorize_page(page_id)
  Rails.logger.debug("[FacebookUser] authorize_page")
  response = fb.get(fb_pref("user_id"), :type => "accounts/#{page_id}")
  found = false
  response.data.each do ||
    Rails.logger.debug("[FacebookUser] checking account #{.id}")
    if .id.to_s == page_id.to_s
      Rails.logger.debug("[FacebookUser] found account #{.id}")
      fb_pref("page_token", .access_token)
      found = true
      break
    end
  end

  found
end

.clearObject



5
6
7
8
9
# File 'app/models/atreides/facebook_user.rb', line 5

def clear
  ["user_id", "access_token", "expiration", "page_token"].each do |key|
    Atreides::Preference.set("facebook.#{key}",nil)
  end
end

.current_token_expired?Boolean

def has_valid_token?

!(fb_pref("access_token").blank? || current_token_expired?)

end

Returns:

  • (Boolean)


49
50
51
52
# File 'app/models/atreides/facebook_user.rb', line 49

def current_token_expired?
  return true if fb_pref("expiration").blank?
  Time.parse(fb_pref("expiration")) < Time.now
end

.extend_token(token) ⇒ Object

Returns @response You may check for errors with [“status”] == “error”



57
58
59
60
61
62
63
64
65
# File 'app/models/atreides/facebook_user.rb', line 57

def extend_token(token)
  begin
    response = MiniFB.fb_exchange_token(Settings.facebook.app_id, Settings.facebook.app_secret, token)
    response["status"] = "ok"
    response
  rescue => e
    {"status" => "error"}
  end
end

.find_or_create(params) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/atreides/facebook_user.rb', line 11

def find_or_create(params)
  Rails.logger.debug("[FacebookUser] find_or_create")
  access_token = params[:access_token]
  user_id = params[:user_id]
  expiration = params[:expiry]

  # if !has_valid_token?
    # Current user is invalid or does not exist
    Rails.logger.debug("[FacebookUser] determined invalid token")
    # See if we can get an extended token
    token_hash = extend_token(access_token)
    if token_hash["status"] == "ok"
      Rails.logger.debug("[FacebookUser] got extended token")
      access_token = token_hash["access_token"]
      expiration = token_hash["expires"]
    else
      Rails.logger.debug("[FacebookUser] did not get extended token")
    end

    expiration_date = Time.now + expiration.to_i
    expiration_string = expiration_date.to_s(:db)

    fb_pref("user_id", user_id)
    fb_pref("access_token", access_token)
    fb_pref("expiration", expiration_string)
    Rails.logger.debug("[FacebookUser] set all vars")
  # else
  #   Rails.logger.debug("[FacebookUser] token is valid")
  # end

  Rails.logger.debug("[FacebookUser] leaving")
  true
end