How to use Koala Facebook Graph API?

Posted by reko on Stack Overflow See other posts from Stack Overflow or by reko
Published on 2012-02-27T22:39:34Z Indexed on 2012/03/18 17:57 UTC
Read the original article Hit count: 820

I am a Rails newbie. I want to use Koala's Graph API.

In my controller

@graph = Koala::Facebook::API.new('myFacebookAccessToken')
@hello = @graph.get_object("my.Name")

When I do this, I get something like this

{
"id"=>"123456", 
"name"=>"First Middle Last", 
"first_name"=>"First", 
"middle_name"=>"Middle", 
"last_name"=>"Last", 
"link"=>"http://www.facebook.com/MyName", 
"username"=>"my.name", 
"birthday"=>"12/12/1212", 
"hometown"=>{"id"=>"115200305133358163", "name"=>"City, State"}, "location"=>{"id"=>"1054648928202133335", "name"=>"City, State"}, 
"bio"=>"This is my awesome Bio.", 
"quotes"=>"I am the master of my fate; I am the captain of my soul. - William Ernest Henley\r\n\r\n"Don't go around saying the world owes you a living. The world owes you nothing. It was here first.\" - Mark Twain", 
"work"=>[{"employer"=>{"id"=>"100751133333", "name"=>"Company1"}, "position"=>{"id"=>"105763693332790962", "name"=>"Position1"}, "start_date"=>"2010-08", "end_date"=>"2011-07"}],
"sports"=>[{"id"=>"104019549633137", "name"=>"Sport1"}, {"id"=>"103992339636529", "name"=>"Sport2"}], 
"favorite_teams"=>[{"id"=>"105467226133353743", "name"=>"Fav1"}, {"id"=>"19031343444432369133", "name"=>"Fav2"}, {"id"=>"98027790139333", "name"=>"Fav3"}, {"id"=>"104055132963393331", "name"=>"Fav4"}, {"id"=>"191744431437533310", "name"=>"Fav5"}], 
"favorite_athletes"=>[{"id"=>"10836600585799922", "name"=>"Fava1"}, {"id"=>"18995689436787722", "name"=>"Fava2"}, {"id"=>"11156342219404022", "name"=>"Fava4"}, {"id"=>"11169998212279347", "name"=>"Fava5"}, {"id"=>"122326564475039", "name"=>"Fava6"}], 
"inspirational_people"=>[{"id"=>"16383141733798", "name"=>"Fava7"}, {"id"=>"113529011990793335", "name"=>"fava8"}, {"id"=>"112032333138809855566", "name"=>"Fava9"}, {"id"=>"10810367588423324", "name"=>"Fava10"}], 
"education"=>[{"school"=>{"id"=>"13478880321332322233663", "name"=>"School1"}, "type"=>"High School", "with"=>[{"id"=>"1401052755", "name"=>"Friend1"}]}, {"school"=>{"id"=>"11482777188037224", "name"=>"School2"}, "year"=>{"id"=>"138383069535219", "name"=>"2005"}, "type"=>"High School"}, {"school"=>{"id"=>"10604484633093514", "name"=>"School3"}, "year"=>{"id"=>"142963519060927", "name"=>"2010"}, "concentration"=>[{"id"=>"10407695629335773", "name"=>"c1"}], "type"=>"College"}, {"school"=>{"id"=>"22030497466330708", "name"=>"School4"}, "degree"=>{"id"=>"19233130157477979", "name"=>"c3"}, "year"=>{"id"=>"201638419856163", "name"=>"2011"}, "type"=>"Graduate School"}], 
"gender"=>"male", 
"interested_in"=>["female"], 
"relationship_status"=>"Single", 
"religion"=>"Religion1", 
"political"=>"Political1", 
"email"=>"[email protected]", 
"timezone"=>-8, 
"locale"=>"en_US", 
"languages"=>[{"id"=>"10605952233759137", "name"=>"English"}, {"id"=>"10337617475934611", "name"=>"L2"}, {"id"=>"11296944428713061", "name"=>"L3"}], 
"verified"=>true,
"updated_time"=>"2012-02-24T04:18:05+0000"
}

How do I show this entire hash in the view in a good format?

This is what I did from what ever I learnt.. In my view

<% @hello.each do |key, value| %>
<li><%=h "#{key.to_s} : #{value.to_s}" %></li>
<% end %>

This will get the entire thing converted to a list... It works awesome if its just one key.. but how to work with multiple keys and show only the information... something like when it outputs hometown : City, State rather than something like

hometown : {"id"=>"115200305133358163", "name"=>"City, State"}

Also for education if I just say education[school][name] to display list of schools attended?

The error i get is can't convert String into Integer

I also tried to do this in my controller, but I get the same error..

@fav_teams = @hello["favorite_teams"]["name"]

Also, how can I save all these to the database.. something like just the list of all schools.. not their id no's?

Update:

The way I plan to save to my database is.. lets say for a user model, i want to save to database as :facebook_id, :facebook_name, :facebook_firstname, ...., :facebook_hometown .. here I only want to save name... when it comes to education.. I want to save.. school, concentration and type.. I have no idea on how to achieve this..

Looking forward for help! thanks!

© Stack Overflow or respective owner

Related posts about ruby

Related posts about ruby-on-rails-3