Paging through Koala::GraphCollection

Go To StackoverFlow.com

0

I'm writing a Rails function that (should)

  1. Import all of a users '/me/music'
  2. Look up each page in more detail in groups of 50 (FB batch API)

Import I've got working just fine. And batch works with me restricting the loop to the first 50, but I'm not sure how to rerun the loop with an offset. This is my current loop:

Koala::Facebook::BatchOperation.instance_variable_set(:@identifier, 0)
results = @graph.batch do |batch_api|
  @music.each do |artist|
    if(i == 50)
    break
    end
    batch_api.get_object(artist["id"])
    i=i+1
  end
end

Apparently @music[0..50] do |artist| is not valid syntax, so no luck there.

2012-04-04 01:01
by Bessey


3

For googlers, this is how I solved my problem:

artist_ids = music.each do |artist|
  artist["id"]
end
Koala::Facebook::BatchOperation.instance_variable_set(:@identifier, 0)
artist_ids.in_groups_of(50) do |artists|
  i=0
  results = graph.batch do |batch_api|
    for artist in artists do
      # ((Your code))
      i=i+1
    end
  end
  results.each do |artist|
      # ((Your code))
  end
end
2012-04-20 15:40
by Bessey
Although better yet, if your list of artists / pages / objects is coming from a collection, you can specify the fields you want in the initial collection request, rather than getting the list of IDs and then working one by one - Bessey 2012-07-05 14:01
Ads