Here’s code. Can anyone find why?
This program is to show photos from one’s friend’s albums.
I think this program uses facebook api 3times, so there might be important point.
But i dont have any idea about things instead of this code.
graph = Koala::Facebook::API.new(session[:access_token])
freegraph = Koala::Facebook::API.new
friends = graph.get_object("me/friends")
friendsIds = Array.new
friends.each do |f| friendsIds << f["id"] end
fsinfo = freegraph.get_objects(friendsIds)
@realFriends = Array.new
fsinfo.each do |f|
if f[1]["gender"].present? && (f[1]["gender"] != "male")
@realFriends << f[1]
end
end
rFids = @realFriends.map do |rF| rF["id"] end
albums = graph.get_object("albums?ids="+rFids.join(","))
album_ids = Array.new
albums.each do |user|
album_ids += user[1]["data"].map do |a| a["id"] end
end
randAlbumIds = Array.new
20.times do
randAlbumIds << album_ids.at(rand(album_ids.count))
end
imgList = graph.get_object("photos?ids="+randAlbumIds.join(","))
imgObjs = Array.new
imgList.each do |img|
imgObjs += img[1]["data"]
end
if params[:tags].present?
@photos = imgObjs.select do |i| i["tags"].present? end
else
@photos = imgObjs
end
Facebook pulls are going to be slow, that’s a given. You don’t want to do HTTP calls inside of the Rails HTTP request — also a given. So, you’ll want to move this to a background process or thread.
But I’m more concerned about your code… You wrote
you’re looking for an array of female friend ideas. So I would do this:
This will return an array of friend ids who are not male. The map will return [nil, 3435, 656], and to get rid of the nils we call compact.
It’s readable, and will be faster in execution and reduce garbage collection time.
Can’t help myself. More!
Should just be
(All this is just Ruby awesomeness)