Class: GraphAPI
- Inherits:
-
Object
- Object
- GraphAPI
- Defined in:
- lib/graph_api.rb,
lib/graph_api/version.rb
Overview
Public: GraphAPI is a Ruby Gem created to simplifiy and help manage
authentication using the Facebook Graph API.
Example:
get '/facebook_login' do
redirect GraphAPI.auth_url
end
get '/facebook_callback' do
@facebook_user = GraphAPI.new(false, params[:code])
session[:auth_token] = @facebook_user.auth_token
render :signed_in
end
Constant Summary collapse
- VERSION =
'1.0.0'
Class Attribute Summary collapse
-
.access_scope ⇒ Object
Public: Required setting used for setting Facebook application requirements.
-
.app_secret ⇒ Object
Public: Required setting used for Facebook private application secret.
-
.callback_url ⇒ Object
Public: Reqired setting used for Facebook call back URL when receiving the Facebook connect code param.
-
.client_id ⇒ Object
Public: Required setting used for Facebook application Id.
-
.logout_url ⇒ Object
Public: Non-reqired setting used for Facebook call back URL when logging out of a Facebook application.
-
.user_fields ⇒ Object
Public: Required setting used for setting the fields pulled for.
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Public: Get and set the facebook access token.
-
#user_data ⇒ Object
Public: Get and set a users data based on the current access token.
Class Method Summary collapse
-
.auth_url(callback_url = nil) ⇒ Object
Public: Creates and returns a Facebook Authentication URL based on the supplied settings.
-
.config(&block) ⇒ Object
Public: Method for configuring the setting settings for a nicer syntax.
-
.fetch_token(code, callback_url = nil) ⇒ Object
Public: Requests the Access Token from the Facebook Graph API and returns it as a string.
-
.request(url, access_token) ⇒ Object
Creates a request to the Facebook graph API and returns the response.
Instance Method Summary collapse
-
#initialize(access_token, code = nil, callback_url = nil) ⇒ GraphAPI
constructor
Public: Creates a new Graph API instance.
-
#logout_url ⇒ Object
Public: Returns a URL designed to log a user out of Facebook and redirect them back to your Facebook application.
-
#method_missing(method, *args, &block) ⇒ Object
Public: Meta methods for each of the user fields declared.
-
#photo ⇒ Object
Public: Fetches and returns the cover photo src for a Facebook user.
-
#thumbnail ⇒ Object
Public: Fetches and returns the current thumbnail src for a Facebook user.
Constructor Details
#initialize(access_token, code = nil, callback_url = nil) ⇒ GraphAPI
Public: Creates a new Graph API instance.
Returns a object representing the current Facebook user with properties specified in the self.class.user_fields hash array.
131 132 133 134 135 136 137 |
# File 'lib/graph_api.rb', line 131 def initialize(access_token, code=nil, callback_url=nil) @access_token = if not code.nil? self.class.fetch_token(code, callback_url) else access_token end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
163 164 165 166 167 168 169 170 171 |
# File 'lib/graph_api.rb', line 163 def method_missing(method, *args, &block) user_fields = self.class.user_fields << :picture if user_fields.include? method.to_sym @user_data ||= self.class.request("/me?fields=#{user_fields.join(',')}", @access_token) @user_data[method.to_s] else super end end |
Class Attribute Details
.access_scope ⇒ Object
Public: Required setting used for setting Facebook application requirements.
Example
55 56 57 |
# File 'lib/graph_api.rb', line 55 def access_scope @access_scope end |
.app_secret ⇒ Object
Public: Required setting used for Facebook private application secret.
Example:
27 28 29 |
# File 'lib/graph_api.rb', line 27 def app_secret @app_secret end |
.callback_url ⇒ Object
Public: Reqired setting used for Facebook call back URL when receiving the Facebook connect code param.
Example
41 42 43 |
# File 'lib/graph_api.rb', line 41 def callback_url @callback_url end |
.client_id ⇒ Object
Public: Required setting used for Facebook application Id.
Example
34 35 36 |
# File 'lib/graph_api.rb', line 34 def client_id @client_id end |
.logout_url ⇒ Object
Public: Non-reqired setting used for Facebook call back URL when logging out of a Facebook application.
Example
48 49 50 |
# File 'lib/graph_api.rb', line 48 def logout_url @logout_url end |
.user_fields ⇒ Object
Public: Required setting used for setting the fields pulled for.
Example
62 63 64 |
# File 'lib/graph_api.rb', line 62 def user_fields @user_fields end |
Instance Attribute Details
#access_token ⇒ Object
Public: Get and set the facebook access token.
123 124 125 |
# File 'lib/graph_api.rb', line 123 def access_token @access_token end |
#user_data ⇒ Object
Public: Get and set a users data based on the current access token.
125 126 127 |
# File 'lib/graph_api.rb', line 125 def user_data @user_data end |
Class Method Details
.auth_url(callback_url = nil) ⇒ Object
Public: Creates and returns a Facebook Authentication URL based on the supplied settings.
callback_url - With @callback_url set to nil setting this parameter will use
the sent callback. This is useful when you're using dynamic
URIs with subdomains.
90 91 92 93 94 |
# File 'lib/graph_api.rb', line 90 def self.auth_url(callback_url=nil) "https://graph.facebook.com/oauth/authorize?client_id=#{@client_id}" + "&redirect_uri=#{@callback_url or callback_url}" + "&scope=#{@access_scope.join(',')}" end |
.config(&block) ⇒ Object
Public: Method for configuring the setting settings for a nicer syntax.
Example:
GraphAPI.config do
app_secret '124ca2a483f12723cafa7a5da33a3492'
client_id '234513432316919'
end
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/graph_api.rb', line 74 def self.config(&block) config = Class.new do def self.method_missing(setting, value) @settings ||= [] @settings << [setting, value] end end.class_eval(&block).each do |setting, value| self.send("#{setting}=", value) end end |
.fetch_token(code, callback_url = nil) ⇒ Object
Public: Requests the Access Token from the Facebook Graph API and returns it as a string.
code - The code parameter is the param you receive when the Facebook Graph
API hits your call back URI.
callback_url - With @callback_url set to nil setting this parameter will use
the sent callback. This is useful when you're using dynamic
URIs with subdomains.
104 105 106 107 108 109 110 |
# File 'lib/graph_api.rb', line 104 def self.fetch_token(code, callback_url=nil) RestClient.post('https://graph.facebook.com/oauth/access_token', { client_id: @client_id, redirect_uri: (@callback_url or callback_url), client_secret: @app_secret, code: code })[/access_token=(.+?)&/, 1] end |
.request(url, access_token) ⇒ Object
Creates a request to the Facebook graph API and returns the response.
url - The URL of the request begining with a forward slash. access_token - The access token required for making the request on the Facebook users behalf.
Returns a parsed JSON array returned from the Facebook service with a format like [‘example’ => ‘some_data’].
118 119 120 |
# File 'lib/graph_api.rb', line 118 def self.request(url, access_token) JSON.parse(RestClient.get "https://graph.facebook.com#{url}&access_token=#{access_token}") end |
Instance Method Details
#logout_url ⇒ Object
Public: Returns a URL designed to log a user out of Facebook and redirect
them back to your Facebook application.
153 154 155 |
# File 'lib/graph_api.rb', line 153 def logout_url "https://www.facebook.com/logout.php?next=#{self.class.logout_url}&access_token=#@access_token" end |
#photo ⇒ Object
Public: Fetches and returns the cover photo src for a Facebook user.
140 141 142 143 144 |
# File 'lib/graph_api.rb', line 140 def photo albums = self.class.request('/me/albums?fields=id,cover_photo,type', @access_token)['data'] photo_id = albums.find{|x| x['type'] == 'profile'}['cover_photo'] self.class.request("/#{photo_id}/?fields=source", @access_token)['source'] end |
#thumbnail ⇒ Object
Public: Fetches and returns the current thumbnail src for a Facebook user.
147 148 149 |
# File 'lib/graph_api.rb', line 147 def thumbnail self.picture['data']['url'] end |