Module: FGraph::Rails::FGraphHelper

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

Instance Method Summary collapse

Instance Method Details

#fgraph_access_tokenObject



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

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

#fgraph_base64_url_decode(str) ⇒ Object



39
40
41
42
# File 'lib/fgraph/rails/fgraph_helper.rb', line 39

def fgraph_base64_url_decode(str)
  str += '=' * (4 - str.length.modulo(4))
  Base64.decode64(str.tr('-_', '+/'))
end

#fgraph_clientObject

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



85
86
87
88
89
90
91
92
93
# File 'lib/fgraph/rails/fgraph_helper.rb', line 85

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



73
74
75
76
# File 'lib/fgraph/rails/fgraph_helper.rb', line 73

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)


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

def fgraph_logged_in?
  return true if fgraph_session and fgraph_access_token
end

#fgraph_parse_signed_request(input, app_secret) ⇒ Object

Parses a signed request string provided by Facebook to canvas apps or in a secure cookie.

Parameters:

  • Input

    the signed request from Facebook

Returns:

  • A hash of the validated request information

Raises:

  • RuntimeError if the signature is incomplete, invalid, or using an unsupported algorithm



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fgraph/rails/fgraph_helper.rb', line 49

def fgraph_parse_signed_request(input, app_secret)
  encoded_sig, encoded_envelope = input.split('.', 2)
  raise FGraph::OAuthError, 'SignedRequest: Invalid (incomplete) signature data' unless encoded_sig && encoded_envelope

  signature = fgraph_base64_url_decode(encoded_sig).unpack("H*").first
  envelope = ActiveSupport::JSON.decode(fgraph_base64_url_decode(encoded_envelope))
  raise FGraph::OAuthError, "SignedRequest: Unsupported algorithm #{envelope['algorithm']}" if envelope['algorithm'] != 'HMAC-SHA256'

  hmac = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new, app_secret, encoded_envelope)
  raise FGraph::OAuthError, 'SignedRequest: Invalid signature' if (signature != hmac)

  envelope
end

#fgraph_picture_url(id, type = nil) ⇒ Object

Return Facebook object picture url: graph.facebook.com/[id]/picture

Type Options

  • square - 50x50 (default)

  • small - 50 pixels wide, variable height

  • normal - 100 pixels wide, variable height

  • large - 200 pixels wide, variable height



103
104
105
106
107
108
# File 'lib/fgraph/rails/fgraph_helper.rb', line 103

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
# 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

  fbsr_cookie = request.cookies["fbsr_#{app_id}"]
  if app_id.blank? or app_secret.blank? or fbsr_cookie.blank?
    return @fgraph_session_cookies = false
  end

  # Get authorization code and access token
  signed_request = fgraph_parse_signed_request(fbsr_cookie, app_secret)
  resp = FGraph.oauth_access_token(app_id, app_secret, :code => signed_request['code'])

  @fgraph_session_cookies = { 
    'access_token' => resp['access_token'] 
  }
end

#fgraph_userObject

Alias for fgraph_current_user



79
80
81
# File 'lib/fgraph/rails/fgraph_helper.rb', line 79

def fgraph_user
  fgraph_current_user
end