Module: Koala::Facebook::GraphAPIMethods
- Included in:
- API
- Defined in:
- lib/koala/api/graph_api_methods.rb
Overview
Methods used to interact with the Facebook Graph API.
See github.com/arsduo/koala/wiki/Graph-API for a general introduction to Koala and the Graph API.
The Graph API is made up of the objects in Facebook (e.g., people, pages, events, photos, etc.) and the connections between them (e.g., friends, photo tags, event RSVPs, etc.). Koala provides access to those objects types in a generic way. For example, given an OAuth access token, this will fetch the profile of the active user and the list of the user’s friends:
You can see a list of all of the objects and connections supported by the API at developers.facebook.com/docs/reference/api/.
You can obtain an access token via OAuth or by using the Facebook JavaScript SDK. If you’re using the JavaScript SDK, you can use the OAuth#get_user_from_cookie method to get the OAuth access token for the active user from the cookie provided by Facebook. See the Koala and Facebook documentation for more information.
Instance Method Summary collapse
-
#batch(http_options = {}, &block) ⇒ Object
Execute a set of Graph API calls as a batch.
-
#debug_token(input_token, &block) ⇒ Object
Get an access token information The access token used to instantiate the API object needs to be the app access token or a valid User Access Token from a developer of the app.
-
#delete_connections(id, connection_name, args = {}, options = {}, &block) ⇒ Object
Delete an object’s connection (for instance, unliking the object).
-
#delete_like(id, options = {}, &block) ⇒ Object
Unlike a given object.
-
#delete_object(id, options = {}, &block) ⇒ Object
Delete an object from the Graph if you have appropriate permissions.
-
#get_connection(id, connection_name, args = {}, options = {}, &block) ⇒ Koala::Facebook::API::GraphCollection
(also: #get_connections)
Fetch information about a given connection (e.g. type of activity – feed, events, photos, etc.) for a specific user.
-
#get_object(id, args = {}, options = {}, &block) ⇒ Object
Get information about a Facebook object.
-
#get_object_metadata(id, &block) ⇒ Object
Get the metadata of a Facebook object, including its type.
-
#get_objects(ids, args = {}, options = {}, &block) ⇒ Object
Get information about multiple Facebook objects in one call.
-
#get_page(params, &block) ⇒ Object
Certain calls such as #get_connections return an array of results which you can page through forwards and backwards (to see more feed stories, search results, etc.).
-
#get_page_access_token(id, args = {}, options = {}, &block) ⇒ Object
Get a page’s access token, allowing you to act as the page.
-
#get_picture(object, args = {}, options = {}, &block) ⇒ Object
Fetches a photo url.
-
#get_picture_data(object, args = {}, options = {}, &block) ⇒ Object
Fetches a photo data hash.
- #get_user_picture_data(*args, &block) ⇒ Object
-
#put_comment(id, message, options = {}, &block) ⇒ Object
Comment on a given object.
-
#put_connections(id, connection_name, args = {}, options = {}, &block) ⇒ Object
Write an object to the Graph for a specific user.
-
#put_like(id, options = {}, &block) ⇒ Object
Like a given object.
-
#put_object(parent_object, connection_name, args = {}, options = {}, &block) ⇒ Object
Write an object to the Graph for a specific user.
-
#put_picture(*picture_args, &block) ⇒ Object
Upload a photo.
-
#put_video(*video_args, &block) ⇒ Object
Upload a video.
-
#put_wall_post(message, attachment = {}, target_id = "me", options = {}, &block) ⇒ Object
Write directly to the user’s wall.
-
#search(search_terms, args = {}, options = {}, &block) ⇒ Koala::Facebook::API::GraphCollection
Search for a given query among visible Facebook objects.
-
#set_app_restrictions(app_id, restrictions_hash, args = {}, options = {}, &block) ⇒ Object
App restrictions require you to JSON-encode the restriction value.
Instance Method Details
#batch(http_options = {}, &block) ⇒ Object
Execute a set of Graph API calls as a batch. See batch request documentation for more information and examples.
458 459 460 461 462 463 464 465 466 |
# File 'lib/koala/api/graph_api_methods.rb', line 458 def batch( = {}, &block) batch_client = GraphBatchAPI.new(self) if block yield batch_client batch_client.execute() else batch_client end end |
#debug_token(input_token, &block) ⇒ Object
Get an access token information The access token used to instantiate the API object needs to be the app access token or a valid User Access Token from a developer of the app. See developers.facebook.com/docs/howtos/login/debugging-access-tokens/#step1
389 390 391 392 393 |
# File 'lib/koala/api/graph_api_methods.rb', line 389 def debug_token(input_token, &block) access_token_info = graph_call("debug_token", {:input_token => input_token}) block ? block.call(access_token_info) : access_token_info end |
#delete_connections(id, connection_name, args = {}, options = {}, &block) ⇒ Object
to access connections like /user_id/CONNECTION/other_user_id, simply pass “CONNECTION/other_user_id” as the connection_name
Delete an object’s connection (for instance, unliking the object).
175 176 177 178 179 |
# File 'lib/koala/api/graph_api_methods.rb', line 175 def delete_connections(id, connection_name, args = {}, = {}, &block) # Deletes a given connection raise AuthenticationError.new(nil, nil, "Delete requires an access token") unless access_token graph_call("#{id}/#{connection_name}", args, "delete", , &block) end |
#delete_like(id, options = {}, &block) ⇒ Object
Unlike a given object. Convenience method equivalent to delete_connection(id, “likes”).
336 337 338 339 340 |
# File 'lib/koala/api/graph_api_methods.rb', line 336 def delete_like(id, = {}, &block) # Unlikes a given object for the logged-in user raise AuthenticationError.new(nil, nil, "Unliking requires an access token") unless access_token graph_call("#{id}/likes", {}, "delete", , &block) end |
#delete_object(id, options = {}, &block) ⇒ Object
Delete an object from the Graph if you have appropriate permissions.
107 108 109 110 111 |
# File 'lib/koala/api/graph_api_methods.rb', line 107 def delete_object(id, = {}, &block) # Deletes the object with the given ID from the graph. raise AuthenticationError.new(nil, nil, "Delete requires an access token") unless access_token graph_call(id, {}, "delete", , &block) end |
#get_connection(id, connection_name, args = {}, options = {}, &block) ⇒ Koala::Facebook::API::GraphCollection Also known as: get_connections
to access connections like /user_id/CONNECTION/other_user_id, simply pass “CONNECTION/other_user_id” as the connection_name
Fetch information about a given connection (e.g. type of activity – feed, events, photos, etc.) for a specific user. See Facebook’s documentation for a complete list of connections.
127 128 129 130 |
# File 'lib/koala/api/graph_api_methods.rb', line 127 def get_connection(id, connection_name, args = {}, = {}, &block) # Fetches the connections for given object. graph_call("#{id}/#{connection_name}", args, "get", , &block) end |
#get_object(id, args = {}, options = {}, &block) ⇒ Object
Get information about a Facebook object.
54 55 56 57 |
# File 'lib/koala/api/graph_api_methods.rb', line 54 def get_object(id, args = {}, = {}, &block) # Fetches the given object from the graph. graph_call(id, args, "get", , &block) end |
#get_object_metadata(id, &block) ⇒ Object
Get the metadata of a Facebook object, including its type.
68 69 70 71 |
# File 'lib/koala/api/graph_api_methods.rb', line 68 def (id, &block) result = graph_call(id, {"metadata" => "1"}, "get", {}, &block) result["metadata"] end |
#get_objects(ids, args = {}, options = {}, &block) ⇒ Object
Get information about multiple Facebook objects in one call.
83 84 85 86 87 88 |
# File 'lib/koala/api/graph_api_methods.rb', line 83 def get_objects(ids, args = {}, = {}, &block) # Fetches all of the given objects from the graph. # If any of the IDs are invalid, they'll raise an exception. return [] if ids.empty? graph_call("", args.merge("ids" => ids.respond_to?(:join) ? ids.join(",") : ids), "get", , &block) end |
#get_page(params, &block) ⇒ Object
You’ll rarely need to use this method unless you’re using Sinatra or another non-Rails framework (see GraphCollection for more information).
Certain calls such as #get_connections return an array of results which you can page through forwards and backwards (to see more feed stories, search results, etc.). Those methods use get_page to request another set of results from Facebook.
420 421 422 |
# File 'lib/koala/api/graph_api_methods.rb', line 420 def get_page(params, &block) graph_call(*params, &block) end |
#get_page_access_token(id, args = {}, options = {}, &block) ⇒ Object
Get a page’s access token, allowing you to act as the page. Convenience method for @api.get_object(page_id, :fields => “access_token”).
372 373 374 375 376 377 378 |
# File 'lib/koala/api/graph_api_methods.rb', line 372 def get_page_access_token(id, args = {}, = {}, &block) access_token = get_object(id, args.merge(:fields => "access_token"), ) do |result| result ? result["access_token"] : nil end block ? block.call(access_token) : access_token end |
#get_picture(object, args = {}, options = {}, &block) ⇒ Object
to delete photos or videos, use delete_object(id)
Fetches a photo url. Note that this method returns the picture url, not the full API response. For the hash containing the full metadata for the photo, use #get_user_picture_data instead.
193 194 195 196 197 198 199 200 201 |
# File 'lib/koala/api/graph_api_methods.rb', line 193 def get_picture(object, args = {}, = {}, &block) Koala::Utils.deprecate("API#get_picture will be removed in a future version. Please use API#get_picture_data, which returns a hash including the url.") get_user_picture_data(object, args, ) do |result| # Try to extract the URL result = result.fetch('data', {})['url'] if result.respond_to?(:fetch) block ? block.call(result) : result end end |
#get_picture_data(object, args = {}, options = {}, &block) ⇒ Object
Fetches a photo data hash.
210 211 212 213 214 215 216 217 |
# File 'lib/koala/api/graph_api_methods.rb', line 210 def get_picture_data(object, args = {}, = {}, &block) # The default response for a Graph API query like GET /me/picture is to # return a 302 redirect. This is a surprising difference from the # common return type, so we add the `redirect: false` parameter to get # a RESTful API response instead. args = args.merge(:redirect => false) graph_call("#{object}/picture", args, "get", , &block) end |
#get_user_picture_data(*args, &block) ⇒ Object
219 220 221 222 |
# File 'lib/koala/api/graph_api_methods.rb', line 219 def get_user_picture_data(*args, &block) Koala::Utils.deprecate("API#get_user_picture_data is deprecated and will be removed in a future version. Please use API#get_picture_data, which has the same signature.") get_picture_data(*args, &block) end |
#put_comment(id, message, options = {}, &block) ⇒ Object
Comment on a given object. Convenience method equivalent to put_connection(id, “comments”).
To delete comments, use delete_object(comment_id). To get comments, use get_connections(object, “likes”).
308 309 310 311 |
# File 'lib/koala/api/graph_api_methods.rb', line 308 def put_comment(id, , = {}, &block) # Writes the given comment on the given post. put_connections(id, "comments", {:message => }, , &block) end |
#put_connections(id, connection_name, args = {}, options = {}, &block) ⇒ Object
to access connections like /user_id/CONNECTION/other_user_id, simply pass “CONNECTION/other_user_id” as the connection_name
Write an object to the Graph for a specific user. See Facebook’s documentation for all the supported writeable objects. It is important to note that objects take the singular form, i.e. “event” when using put_connections.
Most write operations require extended permissions. For example, publishing wall posts requires the “publish_stream” permission. See developers.facebook.com/docs/authentication/ for details about extended permissions.
157 158 159 160 161 162 |
# File 'lib/koala/api/graph_api_methods.rb', line 157 def put_connections(id, connection_name, args = {}, = {}, &block) # Posts a certain connection raise AuthenticationError.new(nil, nil, "Write operations require an access token") unless access_token graph_call("#{id}/#{connection_name}", args, "post", , &block) end |
#put_like(id, options = {}, &block) ⇒ Object
Like a given object. Convenience method equivalent to put_connections(id, “likes”).
To get a list of a user’s or object’s likes, use get_connections(id, “likes”).
323 324 325 326 |
# File 'lib/koala/api/graph_api_methods.rb', line 323 def put_like(id, = {}, &block) # Likes the given post. put_connections(id, "likes", {}, , &block) end |
#put_object(parent_object, connection_name, args = {}, options = {}, &block) ⇒ Object
put_object is (for historical reasons) the same as put_connections. Please use put_connections; in a future version of Koala (2.0?), put_object will issue a POST directly to an individual object, not to a connection.
Write an object to the Graph for a specific user.
96 97 98 |
# File 'lib/koala/api/graph_api_methods.rb', line 96 def put_object(parent_object, connection_name, args = {}, = {}, &block) put_connections(parent_object, connection_name, args, , &block) end |
#put_picture(*picture_args, &block) ⇒ Object
to access the media after upload, you’ll need the user_photos or user_videos permission as appropriate.
Upload a photo.
This can be called in multiple ways:
put_picture(file, [content_type], ...)
put_picture(path_to_file, [content_type], ...)
put_picture(picture_url, ...)
You can also pass in uploaded files directly from Rails or Sinatra. See the Koala wiki for more information.
248 249 250 |
# File 'lib/koala/api/graph_api_methods.rb', line 248 def put_picture(*picture_args, &block) put_connections(*parse_media_args(picture_args, "photos"), &block) end |
#put_video(*video_args, &block) ⇒ Object
Upload a video. Functions exactly the same as put_picture (URLs supported as of Facebook API version 2.3).
255 256 257 258 259 |
# File 'lib/koala/api/graph_api_methods.rb', line 255 def put_video(*video_args, &block) args = parse_media_args(video_args, "videos") args.last[:video] = true put_connections(*args, &block) end |
#put_wall_post(message, attachment = {}, target_id = "me", options = {}, &block) ⇒ Object
Write directly to the user’s wall. Convenience method equivalent to put_connections(id, “feed”).
To get wall posts, use get_connections(user, “feed”) To delete a wall post, use delete_object(post_id)
288 289 290 291 292 293 294 |
# File 'lib/koala/api/graph_api_methods.rb', line 288 def put_wall_post(, = {}, target_id = "me", = {}, &block) if properties = .delete(:properties) || .delete("properties") properties = JSON.dump(properties) if properties.is_a?(Hash) || properties.is_a?(Array) ["properties"] = properties end put_connections(target_id, "feed", .merge({:message => }), , &block) end |
#search(search_terms, args = {}, options = {}, &block) ⇒ Koala::Facebook::API::GraphCollection
Search for a given query among visible Facebook objects. See Facebook documentation for more information.
351 352 353 354 355 356 |
# File 'lib/koala/api/graph_api_methods.rb', line 351 def search(search_terms, args = {}, = {}, &block) # Normally we wouldn't enforce Facebook API behavior, but the API fails with cryptic error # messages if you fail to include a type term. For a convenience method, that is valuable. raise ArgumentError, "type must be includedin args when searching" unless args[:type] || args["type"] graph_call("search", args.merge("q" => search_terms), "get", , &block) end |
#set_app_restrictions(app_id, restrictions_hash, args = {}, options = {}, &block) ⇒ Object
App restrictions require you to JSON-encode the restriction value. This is neither obvious nor intuitive, so this convenience method is provided.
404 405 406 |
# File 'lib/koala/api/graph_api_methods.rb', line 404 def set_app_restrictions(app_id, restrictions_hash, args = {}, = {}, &block) graph_call(app_id, args.merge(:restrictions => JSON.dump(restrictions_hash)), "post", , &block) end |