tiny_fb_graph 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/tiny_fb_graph.rb +144 -0
- metadata +94 -0
@@ -0,0 +1,144 @@
|
|
1
|
+
require 'rest_client'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module TinyFB
|
5
|
+
API_URL = 'https://graph.facebook.com/'
|
6
|
+
|
7
|
+
# Returns the Facebook Login URL
|
8
|
+
#
|
9
|
+
# This method handles the first step of the authorization process
|
10
|
+
# After Clicking on that Link, the user
|
11
|
+
# will authenticate and/or authorize your application
|
12
|
+
# on the Facebook website
|
13
|
+
#
|
14
|
+
# He will be then redirected to your site with a code ( in a query parameter )
|
15
|
+
# the access_token method will then use that code to request the access token
|
16
|
+
#
|
17
|
+
# === parameters
|
18
|
+
# * client_id : Your Facebook Application ID
|
19
|
+
# * redirect_uri : The url of the page the user will be redirected to, meaning
|
20
|
+
# the page of your site that will get the code query paramater and use it to ask for the access_token
|
21
|
+
# * scope (optional ) : a string made of comma separated extended permissions, ex: "publish_stream,offline_access"
|
22
|
+
#
|
23
|
+
# === example
|
24
|
+
# TinyFB.login_url(112233445566778,"http://localhost:3000")
|
25
|
+
# => "https://graph.facebook.com/oauth/authorize?client_id=112233445566778&redirect_uri=http://localhost:3000"
|
26
|
+
def self.login_url(client_id,redirect_uri,scope = nil )
|
27
|
+
url = API_URL + 'oauth/authorize'
|
28
|
+
url << '?client_id=' + client_id.to_s
|
29
|
+
url << '&redirect_uri=' + URI.escape(redirect_uri)
|
30
|
+
url << '&scope=' + scope if scope
|
31
|
+
url
|
32
|
+
end
|
33
|
+
|
34
|
+
# Returns the access token ( in a hash value )
|
35
|
+
#
|
36
|
+
# This method handles the second step of the authorization process
|
37
|
+
# With the access code generated through the first step, we ask Facebook
|
38
|
+
# for the access token
|
39
|
+
#
|
40
|
+
# === options
|
41
|
+
# * client_id : Your Facebook Application ID
|
42
|
+
# * redirect_uri : The url of the page the user will be redirected to, meaning
|
43
|
+
# the page of your site that will receive the access token as a query parameter
|
44
|
+
# * secret : Your Facebook Application Secret
|
45
|
+
# * code : The code supplied by the first step
|
46
|
+
def self.access_token(client_id,redirect_uri,secret,code)
|
47
|
+
url = API_URL + 'oauth/access_token'
|
48
|
+
url << '?client_id=' + client_id.to_s
|
49
|
+
url << '&redirect_uri=' + URI.escape(redirect_uri)
|
50
|
+
url << '&client_secret=' + secret
|
51
|
+
url << '&code=' + URI.escape(code)
|
52
|
+
response = RestClient.get url
|
53
|
+
params = {}
|
54
|
+
params_array = response.split("&")
|
55
|
+
params_array.each do |param|
|
56
|
+
p = param.split("=")
|
57
|
+
params[p[0]] = p[1]
|
58
|
+
end
|
59
|
+
params
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
# Returns information from the Facebook Graph
|
64
|
+
#
|
65
|
+
# The raw_node returns the raw JSON response while the
|
66
|
+
# node returns a Ruby Hash
|
67
|
+
#
|
68
|
+
# === parameters
|
69
|
+
# * path : the path to the desired node
|
70
|
+
# * token : the access token
|
71
|
+
# * params ( optional ) : additional parameters
|
72
|
+
#
|
73
|
+
# === example
|
74
|
+
# TinyFB.node 'me',"xxxxxxxx"
|
75
|
+
# => A Hash ( see the Facebook Graph API Documentation )
|
76
|
+
def self.node(path,token,params = {})
|
77
|
+
JSON.parse raw_node(path,token,params)
|
78
|
+
end
|
79
|
+
def self.raw_node(path,token,params = {})
|
80
|
+
params = params.merge! :access_token => token
|
81
|
+
response = RestClient.get API_URL + path, :params => params
|
82
|
+
|
83
|
+
rescue RestClient::Exception => ex
|
84
|
+
res_hash = JSON.parse(ex.http_body)
|
85
|
+
raise FaceBookError.new(ex.http_code, "#{res_hash["error"]["type"]}: #{res_hash["error"]["message"]}")
|
86
|
+
end
|
87
|
+
|
88
|
+
# Posts data to the Facebook Graph
|
89
|
+
#
|
90
|
+
# === parameters
|
91
|
+
# * path : the path you are publishing to
|
92
|
+
# * token : the access token
|
93
|
+
# * params ( optional ) : additional parameters
|
94
|
+
#
|
95
|
+
# === example
|
96
|
+
# TinyFB.post "me/feed", "xxxx" , :message => "message"
|
97
|
+
def self.post(path,token,params ={})
|
98
|
+
params = params.merge! :access_token => token
|
99
|
+
response = RestClient.post API_URL + path, params
|
100
|
+
JSON.parse response
|
101
|
+
|
102
|
+
rescue RestClient::Exception => e
|
103
|
+
error_hash = JSON.parse(e.http_body)
|
104
|
+
raise FaceBookError.new(e.http_code, "#{error_hash["error"]["type"]}: #{error_hash["error"]["message"]}")
|
105
|
+
end
|
106
|
+
|
107
|
+
# Posts a file to the Facebook Graph
|
108
|
+
#
|
109
|
+
# Returns the Hash converted JSON response
|
110
|
+
#
|
111
|
+
# === parameters
|
112
|
+
# * path : the path to the node you want to post the file to
|
113
|
+
# * token : the access token
|
114
|
+
# * params ( optional ) : additional parameters
|
115
|
+
#
|
116
|
+
# === example
|
117
|
+
# TinyFB.post "me/photos", "xxxx" ,"/path/to/image.jpg"
|
118
|
+
def self.post_file(path,token,file_path,params = {})
|
119
|
+
params = params.merge! :access_token => token,:source => File.new(file_path,'rb')
|
120
|
+
response = RestClient.post API_URL + path, params
|
121
|
+
JSON.parse response
|
122
|
+
|
123
|
+
rescue RestClient::Exception => e
|
124
|
+
error_hash = JSON.parse(e.http_body)
|
125
|
+
raise FaceBookError.new(e.http_code, "#{error_hash["error"]["type"]}: #{error_hash["error"]["message"]}")
|
126
|
+
end
|
127
|
+
|
128
|
+
|
129
|
+
# Exception Class
|
130
|
+
#
|
131
|
+
# May be raised by node, raw_node, post and post_file
|
132
|
+
#
|
133
|
+
# You have to catch this exception in your code
|
134
|
+
class FaceBookError < StandardError
|
135
|
+
attr_accessor :code,:error_msg
|
136
|
+
# Error that happens during a facebook call.
|
137
|
+
def initialize(error_code, error_msg)
|
138
|
+
@code = error_code
|
139
|
+
@error_msg = error_msg
|
140
|
+
super("Facebook error #{error_code}: #{error_msg}")
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tiny_fb_graph
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 25
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Vinh CHUC
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-07-08 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: json
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: rest-client
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 3
|
44
|
+
segments:
|
45
|
+
- 0
|
46
|
+
version: "0"
|
47
|
+
type: :runtime
|
48
|
+
version_requirements: *id002
|
49
|
+
description: "Tiny library for the Facebook Graph API. This library provides several things : methods to handle the OAuth base authentification : generating the login url, retrieving the access_token . And methods, based on the rest-client gem, that allow you to retrieve/post data/files from the Facebook Graph. These methods may raise a custom exception in the API encounters errors."
|
50
|
+
email: vinh.chuc@gmail.com
|
51
|
+
executables: []
|
52
|
+
|
53
|
+
extensions: []
|
54
|
+
|
55
|
+
extra_rdoc_files: []
|
56
|
+
|
57
|
+
files:
|
58
|
+
- lib/tiny_fb_graph.rb
|
59
|
+
has_rdoc: true
|
60
|
+
homepage:
|
61
|
+
licenses:
|
62
|
+
- http://creativecommons.org/licenses/by-nc-sa/3.0/
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
hash: 3
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
version: "0"
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
hash: 3
|
83
|
+
segments:
|
84
|
+
- 0
|
85
|
+
version: "0"
|
86
|
+
requirements: []
|
87
|
+
|
88
|
+
rubyforge_project:
|
89
|
+
rubygems_version: 1.3.7
|
90
|
+
signing_key:
|
91
|
+
specification_version: 3
|
92
|
+
summary: Tiny library for the Facebook Graph API
|
93
|
+
test_files: []
|
94
|
+
|