Module: TinyFB
- Defined in:
- lib/tiny_fb_graph.rb
Defined Under Namespace
Classes: FaceBookError
Constant Summary collapse
- API_URL =
'https://graph.facebook.com/'
Class Method Summary collapse
-
.access_token(client_id, redirect_uri, secret, code) ⇒ Object
Returns the access token ( in a hash value ).
-
.login_url(client_id, redirect_uri, scope = nil) ⇒ Object
Returns the Facebook Login URL.
-
.node(path, token, params = {}) ⇒ Object
Returns information from the Facebook Graph.
-
.post(path, token, params = {}) ⇒ Object
Posts data to the Facebook Graph.
-
.post_file(path, token, file_path, params = {}) ⇒ Object
Posts a file to the Facebook Graph.
- .raw_node(path, token, params = {}) ⇒ Object
Class Method Details
.access_token(client_id, redirect_uri, secret, code) ⇒ Object
Returns the access token ( in a hash value )
This method handles the second step of the authorization process With the access code generated through the first step, we ask Facebook for the access token
options
-
client_id : Your Facebook Application ID
-
redirect_uri : The url of the page the user will be redirected to, meaning the page of your site that will receive the access token as a query parameter
-
secret : Your Facebook Application Secret
-
code : The code supplied by the first step
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/tiny_fb_graph.rb', line 46 def self.access_token(client_id,redirect_uri,secret,code) url = API_URL + 'oauth/access_token' url << '?client_id=' + client_id.to_s url << '&redirect_uri=' + URI.escape(redirect_uri) url << '&client_secret=' + secret url << '&code=' + URI.escape(code) response = RestClient.get url params = {} params_array = response.split("&") params_array.each do |param| p = param.split("=") params[p[0]] = p[1] end params end |
.login_url(client_id, redirect_uri, scope = nil) ⇒ Object
Returns the Facebook Login URL
This method handles the first step of the authorization process After Clicking on that Link, the user will authenticate and/or authorize your application on the Facebook website
He will be then redirected to your site with a code ( in a query parameter ) the access_token method will then use that code to request the access token
parameters
-
client_id : Your Facebook Application ID
-
redirect_uri : The url of the page the user will be redirected to, meaning the page of your site that will get the code query paramater and use it to ask for the access_token
-
scope (optional ) : a string made of comma separated extended permissions, ex: “publish_stream,offline_access”
example
TinyFB.login_url(112233445566778,"http://localhost:3000")
=> "https://graph.facebook.com/oauth/authorize?client_id=112233445566778&redirect_uri=http://localhost:3000"
26 27 28 29 30 31 32 |
# File 'lib/tiny_fb_graph.rb', line 26 def self.login_url(client_id,redirect_uri,scope = nil ) url = API_URL + 'oauth/authorize' url << '?client_id=' + client_id.to_s url << '&redirect_uri=' + URI.escape(redirect_uri) url << '&scope=' + scope if scope url end |
.node(path, token, params = {}) ⇒ Object
Returns information from the Facebook Graph
The raw_node returns the raw JSON response while the node returns a Ruby Hash
parameters
-
path : the path to the desired node
-
token : the access token
-
params ( optional ) : additional parameters
example
TinyFB.node 'me',"xxxxxxxx"
=> A Hash ( see the Facebook Graph API Documentation )
76 77 78 |
# File 'lib/tiny_fb_graph.rb', line 76 def self.node(path,token,params = {}) JSON.parse raw_node(path,token,params) end |
.post(path, token, params = {}) ⇒ Object
Posts data to the Facebook Graph
parameters
-
path : the path you are publishing to
-
token : the access token
-
params ( optional ) : additional parameters
example
TinyFB.post "me/feed", "xxxx" , :message => "message"
97 98 99 100 101 102 103 104 105 |
# File 'lib/tiny_fb_graph.rb', line 97 def self.post(path,token,params ={}) params = params.merge! :access_token => token response = RestClient.post API_URL + path, params JSON.parse response rescue RestClient::Exception => e error_hash = JSON.parse(e.http_body) raise FaceBookError.new(e.http_code, "#{error_hash["error"]["type"]}: #{error_hash["error"]["message"]}") end |
.post_file(path, token, file_path, params = {}) ⇒ Object
118 119 120 121 122 123 124 125 126 |
# File 'lib/tiny_fb_graph.rb', line 118 def self.post_file(path,token,file_path,params = {}) params = params.merge! :access_token => token,:source => File.new(file_path,'rb') response = RestClient.post API_URL + path, params JSON.parse response rescue RestClient::Exception => e error_hash = JSON.parse(e.http_body) raise FaceBookError.new(e.http_code, "#{error_hash["error"]["type"]}: #{error_hash["error"]["message"]}") end |
.raw_node(path, token, params = {}) ⇒ Object
79 80 81 82 83 84 85 86 |
# File 'lib/tiny_fb_graph.rb', line 79 def self.raw_node(path,token,params = {}) params = params.merge! :access_token => token response = RestClient.get API_URL + path, :params => params rescue RestClient::Exception => ex res_hash = JSON.parse(ex.http_body) raise FaceBookError.new(ex.http_code, "#{res_hash["error"]["type"]}: #{res_hash["error"]["message"]}") end |