Skip to content
arsduo edited this page Jun 6, 2011 · 11 revisions

Here are some Batch Request examples taken directly from the test cases -- more formal documentation coming soon!

Make sure to read Facebook's documentation, it's very useful.

it "should be able get two results at once" do
  me, koppel = Koala::Facebook::GraphAPI.batch do
    @api.get_object('me')
    @api.get_object('koppel')
  end
  me['id'].should_not be_nil
  koppel['id'].should_not be_nil
end

  
it "works with GraphAndRestAPI instances" do
  me, koppel = Koala::Facebook::GraphAPI.batch do
    Koala::Facebook::GraphAndRestAPI.new(@api.access_token).get_object('me')
    @api.get_object('koppel')
  end
  me['id'].should_not be_nil
  koppel['id'].should_not be_nil
end

it 'should be able to make mixed calls inside of a batch' do
  me, friends = Koala::Facebook::GraphAPI.batch do
    @api.get_object('me')
    @api.get_connections('me', 'friends')
  end
  me['id'].should_not be_nil
  friends.should be_an(Array)
end

it 'should be able to make a get_picture call inside of a batch' do
  pictures = Koala::Facebook::GraphAPI.batch do
    @api.get_picture('me')
  end
  pictures.first.should_not be_empty
end

it "should handle requests for two different tokens" do
  me, insights = Koala::Facebook::GraphAPI.batch do
    @api.get_object('me')
    @app_api.get_connections(@app_id, 'insights')
  end
  me['id'].should_not be_nil
  insights.should be_an(Array)
end

it "inserts errors in the appropriate place, without breaking other results" do
  failed_insights, koppel = Koala::Facebook::GraphAPI.batch do
    @api.get_connections(@app_id, 'insights')
    @app_api.get_object("koppel")
  end
  failed_insights.should be_a(Koala::Facebook::APIError)
  koppel["id"].should_not be_nil
end

it "handles different request methods" do
  result = @api.put_wall_post("Hello, world, from the test suite batch API!")
  wall_post = result["id"]
  
  wall_post, koppel = Koala::Facebook::GraphAPI.batch do
    @api.put_like(wall_post)
    @api.delete_object(wall_post)
  end
end

it "allows FQL" do
  result = Koala::Facebook::GraphAPI.batch do
    @api.graph_call("method/fql.query", {:query=>"select name from user where uid=4"}, "post")
  end
  
  fql_result = result[0]
  fql_result[0].should be_a(Hash)
  fql_result[0]["name"].should == "Mark Zuckerberg"
end

describe "binary files" do
  it "posts binary files" do
    file = File.open(File.join(File.dirname(__FILE__), "..", "fixtures", "beach.jpg"))

    result = Koala::Facebook::GraphAPI.batch do
      @api.put_picture(file)
    end
    
    @temporary_object_id = result[0]["id"]
    @temporary_object_id.should_not be_nil
  end
  
  it "posts binary files with multiple requests" do
    file = File.open(File.join(File.dirname(__FILE__), "..", "fixtures", "beach.jpg"))

    results = Koala::Facebook::GraphAPI.batch do
      @api.put_picture(file)
      @api.put_picture(file, {}, "koppel")
    end
    results[0]["id"].should_not be_nil
    results[1]["id"].should_not be_nil
  end
end

describe "relating requests" do
  it "allows you create relationships between requests without omit_response_on_success" do
    results = Koala::Facebook::GraphAPI.batch do
      @api.get_connections("me", "friends", {:limit => 5}, :batch_args => {:name => "get-friends"})
      @api.get_objects("{result=get-friends:$.data.*.id}")
    end
  
    results[0].should be_nil
    results[1].should be_an(Hash)
  end
  
  it "allows you create relationships between requests with omit_response_on_success" do
    results = Koala::Facebook::GraphAPI.batch do
      @api.get_connections("me", "friends", {:limit => 5}, :batch_args => {:name => "get-friends", :omit_response_on_success => false})
      @api.get_objects("{result=get-friends:$.data.*.id}")
    end
  
    results[0].should be_an(Array)
    results[1].should be_an(Hash)
  end
  
  it "allows you to create dependencies" do
    me, koppel = Koala::Facebook::GraphAPI.batch do
      @api.get_object("me", {}, :batch_args => {:name => "getme"})
      @api.get_object("koppel", {}, :batch_args => {:depends_on => "getme"})
    end
    
    me.should be_nil # gotcha!  it's omitted because it's a successfully-executed dependency
    koppel["id"].should_not be_nil
  end
  
  it "properly handles dependencies that fail" do
    data, koppel = Koala::Facebook::GraphAPI.batch do
      @api.get_connections(@app_id, 'insights', {}, :batch_args => {:name => "getdata"})
      @api.get_object("koppel", {}, :batch_args => {:depends_on => "getdata"})
    end

    data.should be_a(Koala::Facebook::APIError)
    koppel.should be_nil
  end
  
  it "throws an error for badly-constructed request relationships" do
    expect { 
      Koala::Facebook::GraphAPI.batch do
        @api.get_connections("me", "friends", {:limit => 5})
        @api.get_objects("{result=i-dont-exist:$.data.*.id}")
      end
    }.to raise_exception(Koala::Facebook::APIError)
  end
end