Last Friday I finished off the first few Graph API wrapper objects for the updated Facebook for Force.com Toolkit. The original toolkit had been written to use XML, but as times change the Facebook API had changed to support JSON and OAuth2. (good move in my opinion, btw) With the popularity of Facebook and the Force.com platform still growing at an amazing pace, the toolkit was certainly high on my priority list for updating. I posted a entry the other day showing how the authentication process works in the new toolkit. This time I wanted to share some code as to how the wrapper objects look, and how you can use them in your own applications.

 I have been working through the Graph API to implement the objects listed, and about halfway through. (If anyone wants to help by implementing some of the objects, please let me know – it is a great way to get some recognition in the Force.com community and help your fellow developers!) To give you a flavor of what the toolkit looks like here is a pretty representative example of a wrapper—the FacebookLikes— object:

public class FacebookLikes extends FacebookObject

{

    JSONObject data = new JSONObject();

    public List<LikeInfo> allLikes = new List<LikeInfo>();

    

     

    public FacebookLikes(String clientid, String username, String teststub) 

    {

if(teststub != null)

super.setTestStubResponse(teststub);

super.isAuthenticated(clientid);

data = super.doAPICall(username+'/likes'); 

JSONObject.value p = data.getValue('data');

JSONObject.value[] vals = p.values; 

for(Integer i = 0; i < vals.size(); i++)

{

allLikes.add(new LikeInfo(vals[i].obj));

}

    }

    

    

    public class LikeInfo

    {

JSONObject data;

public String id { get { return data.getValue('id').str; } set; }

public String name { get { return data.getValue('name').str; } set; }

public String category { get { return data.getValue('category').str; } set; }

public LikeInfo(JSONObject obj)

{

data = obj;

}

    }

    

    public static  testmethod void test_Likes()

    {

String stub = '{"data":[{"name":"Mindfulness","category":"Interest","id":"110882252269682"},'+

            '{"name":"Jason Mraz","category":"Musicians","id":"6135205697"},'+

            '{"name":"Emeria","category":"Musicians","id":"90598197142"}]}';

         

FacebookLikes fbl = new FacebookLikes('136520473033312', 'qwall', stub);

System.assertEquals(fbl.allLikes.size(), 3);    

System.assertEquals(fbl.allLikes.get(1).id, '6135205697');

System.assertEquals(fbl.allLikes.get(1).name, 'Jason Mraz');

System.assertEquals(fbl.allLikes.get(1).category, 'Musicians');

    }

}

If you had used the previous version of the toolkit, things, at an API level, look pretty well the same: you have a wrapper class with accessor methods. The big difference is that we now take a JSON string and parse it. The test method gives you an example of what a typical json string looks like from the Facebook Graph API. Speaking of test methods, you may also have noticed that the toolkit allows setting a test stub message. One of the tricky aspects of writing test cases is effectively testing callouts. There are some good examples (here and here) on strategies for testing callout. In this instance, I keep it pretty simple: check if a teststub has been set or not.


The toolkit will also include a samples page, and a simple little test page I put together to help me develop the toolkit. I am a visual person and wanted a way to easily see the JSON responses returned from Facebook. I wanted an easy way to cut and paste the responses for test stubs and also a quick way to 'play' with values. The screenshot below is the result. Hopefully it helps others as they work with the toolkit. 



Testpage-1
 Right now, there is a known bug I need to fix with the JSON Parser which will fail if there is a double quote (") returned in the JSON response. (It should be fixed by the time the updated toolkit is released but just an FYI). In addition to the wrapper objects I have also built a number of social components like recommendations, recent activities, and profile pictures which you can  drop onto your Visualforce pages.  Next on my todo list this week is to jump into HTML5 and some of the touch toolkits/frameworks out there. 
 





tagged , , , , , , , , , , , , , Bookmark the permalink. Trackbacks are closed, but you can post a comment.
  • http://www.forcebrain.com Jason North

    Very cool Quinton! We’ll have to give this a test drive inernally here at ForceBrain.com. We’re building tons of social communities 100% on Force.com and Facebook is definitely in the mix.

  • http://www.linkedin.com/in/jackvolkov Jack

    What are the business requirements and requests Force.com Developers are being given that caused the need for this toolkit?

  • http://facebookwidgets.cerontek.com Joomla Facebook

    Will try it out. Thanks a bunch.
    //Eric