Module: GraphAPI

Extended by:
GraphAPI
Included in:
GraphAPI
Defined in:
lib/graph_api.rb,
lib/graph_api/version.rb

Overview

Public: Various methods useful for interfacing with Facebook Graph protocol.

Example:

get '/facebook_login' do
  redirect FaceGraph.auth_url
end

get '/facebook_auth' do
  @facebook_user = GraphAPI.fetch_user(params[:code])
  @photo = GraphAPI.fetch_photo(@facebook_user['access_token'])
  render :signed_in
end

Constant Summary collapse

VERSION =
'0.9.6'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#access_scopeObject

Public: Required setting used for setting Facebook application requirements.

Example



46
47
48
# File 'lib/graph_api.rb', line 46

def access_scope
  @access_scope
end

#app_secretObject

Public: Required setting used for Facebook private application secret.

Example:



25
26
27
# File 'lib/graph_api.rb', line 25

def app_secret
  @app_secret
end

#callback_urlObject

Public: Reqired setting used for Facebook call back URL when receiving the Facebook connect code param.

Example



39
40
41
# File 'lib/graph_api.rb', line 39

def callback_url
  @callback_url
end

#client_idObject

Public: Required setting used for Facebook private application client ID.

Example



32
33
34
# File 'lib/graph_api.rb', line 32

def client_id
  @client_id
end

#user_fieldsObject

Public: Required setting used for setting the fields pulled for.

Example



53
54
55
# File 'lib/graph_api.rb', line 53

def user_fields
  @user_fields
end

Instance 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.


73
74
75
76
77
# File 'lib/graph_api.rb', line 73

def 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(settings) ⇒ Object

Public: Method for configuring the setting settings for a nicer syntax.

Example:

GraphAPI.config app_secret: ‘124ca2a483f12723cafa7a5da33a3492’,

client_id:  '234513432316919'


62
63
64
65
66
# File 'lib/graph_api.rb', line 62

def config(settings)
  settings.each do |setting, value|
    self.send("#{setting}=", value)
  end
end

#fetch_photo(access_token) ⇒ Object

Public: Fetches and returns the cover photo src for a Facebook user.

access_token - This method requires an Facebook Authentication token.



126
127
128
129
130
# File 'lib/graph_api.rb', line 126

def fetch_photo(access_token)
  albums = request('/me/albums?fields=id,cover_photo,type', access_token)['data']
  photo_id = albums.find{|x| x['type'] == 'profile'}['cover_photo']
  request("/#{photo_id}/?fields=source", access_token)['source']
end

#fetch_thumbnail(access_token) ⇒ Object

Public: Fetches and returns the current thumbnail src for a Facebook user.

access_token - This method requires an Facebook Authentication token.



135
136
137
# File 'lib/graph_api.rb', line 135

def fetch_thumbnail(access_token)
  request('/me?fields=picture', access_token)['picture']
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.


87
88
89
90
91
92
93
# File 'lib/graph_api.rb', line 87

def fetch_token(code, callback_url=nil)
  RestClient.get('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

#fetch_user(code, callback_url = nil) ⇒ Object

Public: Convenience method for fetching a Facebook user array from the

Facebook token code.

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.


118
119
120
121
# File 'lib/graph_api.rb', line 118

def fetch_user(code, callback_url=nil)
  access_token = fetch_token(code, callback_url)
  request_user(access_token)
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’].



101
102
103
# File 'lib/graph_api.rb', line 101

def request(url, access_token)
  JSON.parse(RestClient.get "https://graph.facebook.com#{url}&access_token=#{access_token}")
end

#request_user(access_token) ⇒ Object

Public: Returns a Facebook user array containing the fields set by the

@user_fields setting and the access token for convenience.


107
108
109
110
# File 'lib/graph_api.rb', line 107

def request_user(access_token)
  request("/me?&fields=#{@user_fields.join(',')}", access_token).
    merge('access_token' => access_token)
end