Module: FGraph::Rails::FGraphHelper

Defined in:
lib/fgraph/rails/fgraph_helper.rb

Instance Method Summary collapse

Instance Method Details

#fgraph_access_tokenObject



53
54
55
56
# File 'lib/fgraph/rails/fgraph_helper.rb', line 53

def fgraph_access_token
	return unless fgraph_session
	fgraph_session['access_token']
end

#fgraph_clientObject

Return FGraph::Client instance initialized with settings set in fgraph.yml. Initialized with :access_token as well if Facebook session exists.



75
76
77
78
79
80
81
82
83
# File 'lib/fgraph/rails/fgraph_helper.rb', line 75

def fgraph_client
  return @fgraph_client if @fgraph_client
  
  @fgraph_client = FGraph::Client.new(
	 :client_id => fgraph_config['app_id'],
	 :client_secret => fgraph_config['app_secret'],
	 :access_token => fgraph_access_token
  )
end

#fgraph_configObject

Access FGraph.config initialized with values set in [RAILS_ROOT]/config/fgraph.yml.



6
7
8
# File 'lib/fgraph/rails/fgraph_helper.rb', line 6

def fgraph_config
  FGraph.config || {}
end

#fgraph_current_userObject

Currently logged in facebook user



63
64
65
66
# File 'lib/fgraph/rails/fgraph_helper.rb', line 63

def fgraph_current_user
  return @fgraph_current_user if @fgraph_current_user
  @fgraph_current_user = fgraph_client.me 
end

#fgraph_logged_in?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/fgraph/rails/fgraph_helper.rb', line 58

def fgraph_logged_in?
  return true if fgraph_session and fgraph_access_token
end

#fgraph_picture_url(id, type = nil) ⇒ Object

Type Options

  • square - 50x50 (default)

  • small - 50 pixels wide, variable height

  • normal - 100 pixels wide, variable height

  • large - 200 pixels wide, variable height



93
94
95
96
97
98
# File 'lib/fgraph/rails/fgraph_helper.rb', line 93

def fgraph_picture_url(id, type=nil)
  id = FGraph.get_id(id)
  url = "http://graph.facebook.com/#{id}/picture"
  url += "?type=#{type}" if type
  url
end

#fgraph_session(app_id = fgraph_config['app_id'], app_secret = fgraph_config['app_secret']) ⇒ Object

Return Facebook session, default to retrieve session from cookies.



11
12
13
14
15
16
# File 'lib/fgraph/rails/fgraph_helper.rb', line 11

def fgraph_session(app_id = fgraph_config['app_id'], 
  app_secret = fgraph_config['app_secret'])
	
	return @fgraph_session if @fgraph_session
	@fgraph_session = fgraph_session_cookies(app_id, app_secret)
end

#fgraph_session_cookies(app_id = fgraph_config['app_id'], app_secret = fgraph_config['app_secret']) ⇒ Object

Return Facebook session cookies.



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
44
45
46
47
48
49
50
51
# File 'lib/fgraph/rails/fgraph_helper.rb', line 19

def fgraph_session_cookies(app_id = fgraph_config['app_id'], 
	app_secret = fgraph_config['app_secret'])
			
	return @fgraph_session_cookies if @fgraph_session_cookies
	return if @fgraph_session_cookies == false
			
	 # retrieve session from cookies
	fbs_cookies = request.cookies["fbs_#{app_id}"]
	if app_id.blank? or app_secret.blank? or fbs_cookies.blank?
		return @fgraph_session_cookies = false
	end

	# Parse facebook cookies
	fbs_cookies = CGI.parse(fbs_cookies.gsub(/(^\"|\"$)/, ''))
	session_cookies = {}
	fbs_cookies.each do |key, value|
		session_cookies[key] = value[0]
	end
	
	# Validate session cookies
	cookie_message = ''
	session_cookies_list = session_cookies.sort
	session_cookies_list.each do |cookie|
		cookie_message += "#{cookie[0]}=#{cookie[1]}" if cookie[0] != 'sig'
	end

	# Message digest does not match
	if Digest::MD5.hexdigest(cookie_message + app_secret) != session_cookies['sig']
		@fgraph_session_cookies = false
	end

	@fgraph_session_cookies = session_cookies
end

#fgraph_userObject

Alias for fgraph_current_user



69
70
71
# File 'lib/fgraph/rails/fgraph_helper.rb', line 69

def fgraph_user
  fgraph_current_user
end