Beautiful Architecture

(avery) #1

Example 6-4 shows what the code behind the landing page of http://fettermansbooks.com
might look like without any Facebook integration.


EXAMPLE 6-4. Example book site logic


$books_user_id = establish_booksite_userid($_REQUEST);
$book_infos = user_get_likely_books($books_user_id);
display_books($book_infos);


This user_get_likely_books function operates entirely from the data that the book application
controls, possibly using clever relevance techniques to guess at a user’s interests.


However, imagine Facebook makes available two simple remote-procedure call (RPC) methods
for users on sites outside its walls:



  • friends.get()

  • users.getInfo($users, $fields)


With these, and a mapping from http://fettermansbooks.com’s user identifiers to Facebook’s,
we can add social context to any content on http://fettermansbooks.com. Consider this new
flow for Facebook users in Example 6-5.


EXAMPLE 6-5. Book site logic with social context


$books_user_id = establish_booksite_userid($_REQUEST);
$facebook_client = establish_facebook_session($_REQUEST,$books_user_id);


if ($facebook_client) {
$facebook_friend_uids = $facebook_client->api_client->friends_get();
foreach($facebook_friend_uids as $facebook_friend) {
$book_site_friends[$facebook_friend]
= books_user_id_from_facebook_id ($facebook_friend);
}
$book_site_friend_names = $facebook->api_client->
users_getInfo($facebook_friend_uids, 'name');


foreach($book_site_friends as $fb_id => $booksite_id) {
$friend_books = user_get_reviewed_books($booksite_id);
print "


". $book_site_friend_names[$fb_id]. "'s likely picks:
";
display_books($friend_books);
}
}


The bolded parts of this example are where the book application harnesses the data of the
Facebook Platform. If we could figure out the code behind the function
establish_facebook_session, this architecture would make available much more data in order
to turn this book-aware application into a fully user-aware application.


Let’s examine how Facebook’s API enables this. First, we’ll check out a simple technical
walkthrough of the web service wrapping Facebook data, created through use of appropriate
metadata by a flexible code generator called Thrift. Developers can use these techniques


118 CHAPTER SIX

Free download pdf