Browse files

Refactor get_object_type to get_object_metadata

  • Loading branch information...
1 parent 282f6b8 commit a67a3fb91116d43fbb9b091f4dccf79f72b92ec7 @arsduo committed Jul 8, 2016
View
4 changelog.md
@@ -1,6 +1,10 @@
v2.5.0
======
+New features:
+
+* API#get_object_metadata provides a convenient accessor to an object's graph metadata (thanks, sancopanco!)
+
Documentation improvements:
* Add explicit require to examples in readme.md (thanks, dwkns!)
View
15 lib/koala/api/graph_api.rb
@@ -57,19 +57,18 @@ def get_object(id, args = {}, options = {}, &block)
graph_call(id, args, "get", options, &block)
end
- # Get type of a Facebook object
- #
+ # Get the metadata of a Facebook object, including its type.
+ #
# @param id the object ID (string or number)
#
# @raise [Koala::Facebook::ClientError] if the ID is invalid
# @example
- # get_object_type("442575165800306")=>"page"
- # get_object_type("190822584430113")=>"status"
+ # get_object_metadata("442575165800306")=>{"metadata" => "page", ...}
+ # get_object_metadata("190822584430113")=>{"metadata" => "status", ...}
# @return a string of Facebook object type
- def get_object_type(id)
- res = graph_call("#{id}?metadata=1")
- metadata = res["metadata"]
- metadata["type"]
+ def get_object_metadata(id, &block)
+ result = graph_call(id, {"metadata" => "1"}, "get", {}, &block)
+ result["metadata"]
end
# Get information about multiple Facebook objects in one call.
View
3 spec/fixtures/mock_facebook_responses.yml
@@ -216,6 +216,9 @@ graph_api:
get:
with_token: '{"id": 1, "name": 1, "updated_time": 1}'
no_token: '{"id": 1, "name": 1}'
+ metadata=1:
+ get:
+ with_token: '{"name": "Alex Koppel","metadata": {"fields": [{"name": "id"}],"type": "user","connections": {}},"id": "2905623"}'
/facebook:
no_args:
View
8 spec/support/graph_api_shared_examples.rb
@@ -234,6 +234,14 @@
result = @api.get_objects([KoalaTest.page, KoalaTest.user1])
expect(result.length).to eq(2)
end
+
+ describe "#get_object_metadata" do
+ it "can access an object's metadata" do
+ result = @api.get_object_metadata(KoalaTest.user1)
+ expect(result["type"]).to eq("user")
+ end
+ end
+
it "can access connections from users" do
result = @api.get_connections(KoalaTest.user2, "friends")
expect(result.length).to be > 0

0 comments on commit a67a3fb

Please sign in to comment.