Class: FacebookAccount

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/facebook_account.rb

Constant Summary collapse

FACEBOOK_CLIENT_ID =

Get these from facebook!

ENV['FACEBOOK_CLIENT_ID']
FACEBOOK_CLIENT_SECRET =
ENV['FACEBOOK_CLIENT_SECRET']

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.post(message, user) ⇒ Object


40
41
42
43
# File 'app/models/facebook_account.rb', line 40

def self.post(message, user)
   = FacebookAccount.find_by_user_id(user)
  RestClient.post 'https://graph.facebook.com/me/feed', { :access_token => .access_token, :message => message }
end

Instance Method Details

#authorize_url(callback_url = '') ⇒ Object


11
12
13
14
15
16
17
# File 'app/models/facebook_account.rb', line 11

def authorize_url(callback_url = '')
  if self.oauth_authorize_url.blank?
    self.oauth_authorize_url = "https://graph.facebook.com/oauth/authorize?client_id=#{FACEBOOK_CLIENT_ID}&redirect_uri=#{callback_url}&scope=offline_access,publish_stream"
    self.save!
  end
  self.oauth_authorize_url
end

#show_facebookurl_label_methodObject

show the user email in the admin UI instead of the user id


46
47
48
# File 'app/models/facebook_account.rb', line 46

def show_facebookurl_label_method
  "#{self.stream_url}"
end

#validate_oauth_token(oauth_verifier, callback_url = '') ⇒ Object


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/models/facebook_account.rb', line 19

def validate_oauth_token(oauth_verifier, callback_url = '')
  response = RestClient.get 'https://graph.facebook.com/oauth/access_token', :params => {
                 :client_id => FACEBOOK_CLIENT_ID,
                 :redirect_uri => "#{callback_url.html_safe}",
                 :client_secret => FACEBOOK_CLIENT_SECRET,
                 :code => "#{oauth_verifier.html_safe}"
              }
  pair = response.body.split("&")[0].split("=")
  if (pair[0] == "access_token")
    self.access_token = pair[1]
    response = RestClient.get 'https://graph.facebook.com/me', :params => { :access_token => self.access_token }
    self.stream_url = JSON.parse(response.body)["link"]
    self.active = true
  else
    self.errors.add(:oauth_verifier, "Invalid token, unable to connect to facebook: #{pair[1]}")
    self.active = false
  end
  #self.user_id = current_user.id
  self.save!
end