facebook_graph_api 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/facebook_graph_api.rb +53 -10
- metadata +2 -2
data/lib/facebook_graph_api.rb
CHANGED
@@ -5,8 +5,10 @@ class FacebookGraphAPI
|
|
5
5
|
@access_token = nil
|
6
6
|
@feed = nil
|
7
7
|
@account_info = nil
|
8
|
+
@debug = nil
|
8
9
|
|
9
|
-
def initialize(access_token)
|
10
|
+
def initialize(access_token, debug = false)
|
11
|
+
@debug = debug
|
10
12
|
@graph_url = 'https://graph.facebook.com'
|
11
13
|
@access_token = access_token
|
12
14
|
if @access_token.blank? && !Rails.nil?
|
@@ -14,10 +16,12 @@ class FacebookGraphAPI
|
|
14
16
|
end
|
15
17
|
end
|
16
18
|
|
19
|
+
# Get the basic account information from facebook
|
17
20
|
def get_account_info
|
18
21
|
@account_info ||= get_request('/me')
|
19
22
|
end
|
20
23
|
|
24
|
+
# Get the users feed
|
21
25
|
def get_feed
|
22
26
|
@feed ||= get_request('/me/feed')
|
23
27
|
# I have no idea why sometimes it uses the data index and sometimes it doesn't....
|
@@ -28,10 +32,16 @@ class FacebookGraphAPI
|
|
28
32
|
end
|
29
33
|
end
|
30
34
|
|
35
|
+
# Post a message to the users wall
|
36
|
+
# message: text of the message to be posted. Links will not be converted to click able links. Use post_link to post a clickable link (including video)
|
31
37
|
def post_status(message)
|
32
38
|
post_request('/me/feed', {message: message})
|
33
39
|
end
|
34
40
|
|
41
|
+
# Post a picture to the users wall
|
42
|
+
# picture_path: the actual file path to the image (no urls)
|
43
|
+
# message: the message to be attached to the image if any. Can be null or empty.
|
44
|
+
# post_to_feed: true (by default) if you want the picture to be posted to the users wall, false if you want it hidden. This isn't 100% tested.
|
35
45
|
def post_picture(picture_path, message, post_to_feed=true)
|
36
46
|
File.open(picture_path.to_s.gsub('%20', '\ ').gsub('(', '\(').gsub(')', '\)'), 'rb') do |binary_image|
|
37
47
|
if post_to_feed
|
@@ -42,26 +52,44 @@ class FacebookGraphAPI
|
|
42
52
|
end
|
43
53
|
end
|
44
54
|
|
55
|
+
# Set an already existing image to be the users cover image
|
56
|
+
# picture_id: the facebook id of the image to use as the users cover image. This currently doesn't allow for an offset, but this will be available in the next version.
|
45
57
|
def set_as_cover_image(picture_id)
|
46
58
|
post_request('/me', {cover: picture_id, no_feed_story: 'true'})
|
47
59
|
end
|
48
60
|
|
61
|
+
# Post a clickable link to a users feed (works for youtube videos as well)
|
62
|
+
# link: the link (beginning with http:// or https://) that will be displayed in the users feed
|
63
|
+
# message: the message to be posted with the link on the feed page
|
49
64
|
def post_link(link, message)
|
50
65
|
post_request('/me/feed', {link: link, message: message})
|
51
66
|
end
|
52
67
|
|
68
|
+
# Post a reply to a status that is already existing on facebook
|
69
|
+
# status_id: the facebook id of the status to reply to
|
70
|
+
# message: the message to use in the reply
|
53
71
|
def reply_to_status(status_id, message)
|
54
72
|
post_request('/' + status_id + '/comments', {message: message})
|
55
73
|
end
|
56
74
|
|
75
|
+
# Delete a status
|
76
|
+
# status_id: the facebook id of the status to delete
|
57
77
|
def delete_status(status_id)
|
58
78
|
delete_request('/' + status_id)
|
59
79
|
end
|
60
80
|
|
81
|
+
# Get statuses from facebook page/account
|
61
82
|
def get_statuses
|
62
83
|
@statuses ||= get_request('/me/statuses')
|
84
|
+
# I have no idea why sometimes it uses the data index and sometimes it doesn't....
|
85
|
+
begin
|
86
|
+
return @statuses['data']
|
87
|
+
rescue
|
88
|
+
return @statuses
|
89
|
+
end
|
63
90
|
end
|
64
91
|
|
92
|
+
# Get the insights of a page (**this doesn't work for profiles**)
|
65
93
|
def get_insights
|
66
94
|
@insights ||= get_request('/me/insights')
|
67
95
|
begin
|
@@ -71,32 +99,44 @@ class FacebookGraphAPI
|
|
71
99
|
end
|
72
100
|
end
|
73
101
|
|
74
|
-
#
|
75
|
-
# without creating a new facebook api class
|
102
|
+
# Get any fql request
|
103
|
+
# Fql cannot be cached since it might be used more than once to gather different data without creating a new facebook api class
|
76
104
|
def get_fql(fql)
|
77
105
|
get_request('/fql?q=' + CGI.escape(fql))['data']
|
78
106
|
end
|
79
107
|
|
108
|
+
# Get pages associated with the account
|
80
109
|
def get_pages
|
81
110
|
@page ||= get_request('/me/accounts')['data']
|
82
111
|
end
|
83
112
|
|
84
113
|
private
|
114
|
+
# Execute a get request against the facebook endpoint requested
|
115
|
+
# end_point: anything in the url after the graph.facebook.com i.e. /me/accounts
|
85
116
|
def get_request(end_point)
|
86
117
|
execute_request(end_point, 'get')
|
87
118
|
end
|
88
119
|
|
120
|
+
# Send data to the facebook endpoint requested
|
121
|
+
# end_point: anything in the url after the graph.facebook.com i.e. /me/accounts
|
122
|
+
# parameters: the information to be sent to facebook in a hash
|
89
123
|
def post_request(end_point, parameters)
|
90
124
|
execute_request(end_point, 'post', parameters)
|
91
125
|
end
|
92
126
|
|
127
|
+
# Delete end point from facebook
|
93
128
|
def delete_request(end_point)
|
94
129
|
execute_request(end_point, 'delete')
|
95
130
|
end
|
96
131
|
|
132
|
+
# The actual request send to facebook
|
133
|
+
# end_point: anything in the url after the graph.facebook.com i.e. /me/accounts
|
134
|
+
# method: the HTTP method used to send or receive information from facebook
|
135
|
+
# parameters: the information to be send to facebook in a hash
|
97
136
|
def execute_request(end_point, method, parameters = {})
|
137
|
+
agent = Mechanize.new
|
138
|
+
response = nil
|
98
139
|
begin
|
99
|
-
agent = Mechanize.new
|
100
140
|
if end_point.include? '?'
|
101
141
|
url = @graph_url + end_point + '&access_token=' + @access_token
|
102
142
|
else
|
@@ -109,7 +149,7 @@ class FacebookGraphAPI
|
|
109
149
|
elsif method == 'post'
|
110
150
|
response = agent.post(url, parameters)
|
111
151
|
end
|
112
|
-
if !Rails.nil?
|
152
|
+
if @debug && !Rails.nil?
|
113
153
|
# Echo the response if we are in development mode
|
114
154
|
if Rails.env.development?
|
115
155
|
Rails.logger.info 'FB: ' + method.capitalize + end_point
|
@@ -119,12 +159,15 @@ class FacebookGraphAPI
|
|
119
159
|
return (JSON.parse response.body)
|
120
160
|
rescue Exception => e
|
121
161
|
if !Rails.nil?
|
122
|
-
|
123
|
-
Rails.logger.info 'FB: Error executing ' + method + ' request for "' + end_point + '"'
|
124
|
-
else
|
125
|
-
Rails.logger.info 'FB: Error executing ' + method + ' request for "' + end_point + '" with parameters: ' + parameters.inspect
|
126
|
-
end
|
162
|
+
Rails.logger.info e.page.body
|
127
163
|
Rails.logger.info 'Raw exception: ' + e.message
|
164
|
+
if @debug
|
165
|
+
if !(method == 'post')
|
166
|
+
Rails.logger.info 'FB: Error executing ' + method + ' request for "' + end_point + '"'
|
167
|
+
else
|
168
|
+
Rails.logger.info 'FB: Error executing ' + method + ' request for "' + end_point + '" with parameters: ' + parameters.inspect
|
169
|
+
end
|
170
|
+
end
|
128
171
|
end
|
129
172
|
return []
|
130
173
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: facebook_graph_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-06-
|
12
|
+
date: 2012-06-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: mechanize
|