Class: RFGraph::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/rfgraph/auth.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_id, app_secret) ⇒ Auth

Returns a new instance of Auth.



11
12
13
14
# File 'lib/rfgraph/auth.rb', line 11

def initialize(app_id, app_secret)
  @app_id = app_id
  @app_secret = app_secret
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



8
9
10
# File 'lib/rfgraph/auth.rb', line 8

def access_token
  @access_token
end

#app_idObject

Returns the value of attribute app_id.



6
7
8
# File 'lib/rfgraph/auth.rb', line 6

def app_id
  @app_id
end

#app_secretObject

Returns the value of attribute app_secret.



7
8
9
# File 'lib/rfgraph/auth.rb', line 7

def app_secret
  @app_secret
end

#expiresObject

Returns the value of attribute expires.



9
10
11
# File 'lib/rfgraph/auth.rb', line 9

def expires
  @expires
end

Instance Method Details

#authorize(callback_url, code) ⇒ Object

Take the oauth2 request token and turn it into an access token which can be used to access private data



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rfgraph/auth.rb', line 33

def authorize(callback_url, code)
  data = open("#{BASE_URL}/oauth/access_token?client_id=#{app_id}&redirect_uri=#{CGI.escape callback_url}&client_secret=#{app_secret}&code=#{CGI.escape code}").read
  
  # The expiration date is not necessarily set, as the app might have
  # requested offline_access to the data
  match_data = data.match(/expires=([^&]+)/)
  @expires = match_data && match_data[1] || nil
  
  # Extract the access token
  @access_token = data.match(/access_token=([^&]+)/)[1]
end

#authorize_url(callback_url, options = {}) ⇒ Object

Options:

Array like [:user_photos, :user_videos, :stream_publish] For more permission scopes, see developers.facebook.com/docs/authentication/permissions

All other options in the options Hash will be put in the authorize_url.



23
24
25
26
27
28
29
# File 'lib/rfgraph/auth.rb', line 23

def authorize_url(callback_url, options = {})
  url = "#{BASE_URL}/oauth/authorize?client_id=#{app_id}&redirect_uri=#{callback_url}"
  scope = options.delete(:scope)
  url += "&scope=#{scope.join(',')}" unless scope.blank?
  url += "&#{options.to_query}" unless options.blank? # Add other options. FIXME: to_query method requires Rails?!
  return url
end