koala 1.1.0rc2 → 1.1.0rc3

Sign up to get free protection for your applications and to get access to all the features.
data/readme.md CHANGED
@@ -2,8 +2,8 @@ Koala
2
2
  ====
3
3
  [Koala](http://github.com/arsduo/koala) is a new Facebook library for Ruby, supporting the Graph API (including the batch requests and photo uploads), the REST API, realtime updates, test users, and OAuth validation. We wrote Koala with four goals:
4
4
 
5
- * Lightweight: Koala should be as light and simple as Facebook’s own new libraries, providing API accessors and returning simple JSON. (We clock in, with comments, just over 750 lines of code.)
6
- * Fast: Koala should, out of the box, be quick. In addition to supporting the vanilla Ruby networking libraries, it natively supports Typhoeus, our preferred gem for making fast HTTP requests. Of course, That brings us to our next topic:
5
+ * Lightweight: Koala should be as light and simple as Facebook’s own new libraries, providing API accessors and returning simple JSON. (We clock in, with comments, at just over 750 lines of code.)
6
+ * Fast: Koala should, out of the box, be quick. In addition to supporting the vanilla Ruby networking libraries, it natively supports Typhoeus, our preferred gem for making fast HTTP requests. Of course, that brings us to our next topic:
7
7
  * Flexible: Koala should be useful to everyone, regardless of their current configuration. (We have no dependencies beyond the JSON gem. Koala also has a built-in mechanism for using whichever HTTP library you prefer to make requests against the graph.)
8
8
  * Tested: Koala should have complete test coverage, so you can rely on it. (Our complete test coverage can be run against either mocked responses or the live Facebook servers.)
9
9
 
@@ -47,7 +47,7 @@ When retrieving data that returns an array of results (for example, when calling
47
47
  # This is useful for paging across multiple requests
48
48
  next_path, next_args = feed.next_page_params
49
49
 
50
- # You can use those params to easily get the next (or prevous) page
50
+ # You can use those params to easily get the next (or previous) page
51
51
  page = graph.get_page(feed.next_page_params)
52
52
 
53
53
  You can make multiple calls at once using Facebook's batch API:
@@ -144,12 +144,12 @@ Testing
144
144
  Unit tests are provided for all of Koala's methods. By default, these tests run against mock responses and hence are ready out of the box:
145
145
 
146
146
  # From anywhere in the project directory:
147
- rake spec
147
+ bundle exec rake spec
148
148
 
149
149
 
150
150
  You can also run live tests against Facebook's servers:
151
151
 
152
152
  # Again from anywhere in the project directory:
153
- LIVE=true rake spec
153
+ LIVE=true bundle exec rake spec
154
154
 
155
- Important Note: to run the live tests, you have to provide some of your own data in spec/fixtures/facebook_data.yml: a valid OAuth access token with publish\_stream, read\_stream, and user\_photos permissions and an OAuth code that can be used to generate an access token. You can get thisdata at the OAuth Playground; if you want to use your own app, remember to swap out the app ID, secret, and other values. (The file also provides valid values for other tests, which you're welcome to swap out for data specific to your own application.)
155
+ Important Note: to run the live tests, you have to provide some of your own data in spec/fixtures/facebook_data.yml: a valid OAuth access token with publish\_stream, read\_stream, and user\_photos permissions and an OAuth code that can be used to generate an access token. You can get this data at the OAuth Playground; if you want to use your own app, remember to swap out the app ID, secret, and other values. (The file also provides valid values for other tests, which you're welcome to swap out for data specific to your own application.)
@@ -52,7 +52,7 @@ describe "Koala::Facebook::API" do
52
52
  Koala.stub(:make_request).and_return(response)
53
53
 
54
54
  json_body = mock('JSON body')
55
- JSON.stub(:parse).and_return([json_body])
55
+ MultiJson.stub(:decode).and_return([json_body])
56
56
 
57
57
  @service.api('anything').should == json_body
58
58
  end
@@ -66,7 +66,7 @@ describe "Koala::Facebook::API" do
66
66
 
67
67
  @service.api('anything', {}, "get") do |arg|
68
68
  yield_test.pass
69
- arg.should == JSON.parse(body)
69
+ arg.should == MultiJson.decode(body)
70
70
  end
71
71
  end
72
72
 
@@ -225,7 +225,7 @@ describe "Koala::Facebook::GraphAPI in batch mode" do
225
225
 
226
226
  it "includes the method" do
227
227
  params = Koala::Facebook::BatchOperation.new(@args).to_batch_params(@args[:access_token])
228
- params[:method].should == @args[:method].to_sym
228
+ params[:method].should == @args[:method].to_s
229
229
  end
230
230
 
231
231
  it "works with nil http_options" do
@@ -257,89 +257,45 @@ describe "Koala::Facebook::GraphAPI in batch mode" do
257
257
  file_ids = batch_op.files.find_all {|k, v| v == @uploadable_io}.map {|k, v| k}
258
258
  params = batch_op.to_batch_params(nil)
259
259
  params[:attached_files].should == file_ids.join(",")
260
- end
260
+ end
261
261
  end
262
262
  end
263
-
263
+
264
264
  end
265
-
265
+
266
266
  describe "GraphAPI batch interface" do
267
- it "sets the batch_mode flag to false outside batch mode" do
268
- Koala::Facebook::GraphAPI.batch_mode?.should be_false
269
- end
270
-
271
- it "sets the batch_mode flag inside batch mode" do
272
- Koala::Facebook::GraphAPI.batch do
273
- Koala::Facebook::GraphAPI.batch_mode?.should be_true
274
- end
275
- end
276
-
277
- it "throws an error if you try to access the batch_calls queue outside a batch block" do
278
- expect { Koala::Facebook::GraphAPI.batch_calls << BatchOperation.new(:access_token => "2") }.to raise_exception(Koala::KoalaError)
279
- end
280
267
 
281
- it "clears the batch queue between requests" do
282
- Koala.stub(:make_request).and_return(Koala::Response.new(200, "[]", {}))
283
- Koala::Facebook::GraphAPI.batch { @api.get_object("me") }
284
- Koala.should_receive(:make_request).once.and_return(Koala::Response.new(200, "[]", {}))
285
- Koala::Facebook::GraphAPI.batch { @api.get_object("me") }
286
- end
287
-
288
268
  it "returns nothing for a batch operation" do
289
269
  Koala.stub(:make_request).and_return(Koala::Response.new(200, "[]", {}))
290
- Koala::Facebook::GraphAPI.batch do
291
- @api.get_object("me").should be_nil
292
- end
293
- end
294
-
295
- it "creates a BatchObject when making a GraphAPI request in batch mode" do
296
- Koala.should_receive(:make_request).once.and_return(Koala::Response.new(200, "[]", {}))
297
-
298
- args = {:a => :b}
299
- method = "post"
300
- http_options = {:option => true}
301
- url = "/a"
302
- access_token = "token"
303
- post_processing = lambda {}
304
- op = Koala::Facebook::BatchOperation.new(:access_token => access_token, :method => :get, :url => "/")
305
- Koala::Facebook::BatchOperation.should_receive(:new).with(
306
- :url => url,
307
- :args => args,
308
- :method => method,
309
- :access_token => access_token,
310
- :http_options => http_options,
311
- :post_processing => post_processing
312
- ).and_return(op)
313
-
314
- Koala::Facebook::GraphAPI.batch do
315
- Koala::Facebook::GraphAPI.new(access_token).graph_call(url, args, method, http_options, &post_processing)
270
+ @api.batch do |batch_api|
271
+ batch_api.get_object('me').should be_nil
316
272
  end
317
273
  end
318
274
 
319
- describe "#batch_api" do
275
+ describe "#batch" do
320
276
  before :each do
321
277
  @fake_response = Koala::Response.new(200, "[]", {})
322
278
  Koala.stub(:make_request).and_return(@fake_response)
323
279
  end
324
-
280
+
325
281
  describe "making the request" do
326
282
  context "with no calls" do
327
283
  it "does not make any requests if batch_calls is empty" do
328
284
  Koala.should_not_receive(:make_request)
329
- Koala::Facebook::GraphAPI.batch {}
285
+ @api.batch {|batch_api|}
330
286
  end
331
287
 
332
288
  it "returns []" do
333
- Koala::Facebook::GraphAPI.batch {}.should == []
289
+ @api.batch {|batch_api|}.should == []
334
290
  end
335
291
  end
336
-
292
+
337
293
  it "includes the first operation's access token as the main one in the args" do
338
294
  access_token = "foo"
339
295
  Koala.should_receive(:make_request).with(anything, hash_including("access_token" => access_token), anything, anything).and_return(@fake_response)
340
- Koala::Facebook::GraphAPI.batch do
341
- Koala::Facebook::GraphAPI.new(access_token).get_object('me')
342
- Koala::Facebook::GraphAPI.new("bar").get_object('me')
296
+ Koala::Facebook::GraphAPI.new(access_token).batch do |batch_api|
297
+ batch_api.get_object('me')
298
+ batch_api.get_object('me', {}, {'access_token' => 'bar'})
343
299
  end
344
300
  end
345
301
 
@@ -348,155 +304,180 @@ describe "Koala::Facebook::GraphAPI in batch mode" do
348
304
  op = Koala::Facebook::BatchOperation.new(:access_token => access_token, :method => :get, :url => "/")
349
305
  op.stub(:to_batch_params).and_return({:a => 2})
350
306
  Koala::Facebook::BatchOperation.stub(:new).and_return(op)
351
-
307
+
352
308
  # two requests should generate two batch operations
353
- expected = [op.to_batch_params(access_token), op.to_batch_params(access_token)].to_json
309
+ expected = MultiJson.encode([op.to_batch_params(access_token), op.to_batch_params(access_token)])
354
310
  Koala.should_receive(:make_request).with(anything, hash_including("batch" => expected), anything, anything).and_return(@fake_response)
355
- Koala::Facebook::GraphAPI.batch do
356
- Koala::Facebook::GraphAPI.new(access_token).get_object('me')
357
- Koala::Facebook::GraphAPI.new(access_token).get_object('me')
358
- end
311
+ Koala::Facebook::GraphAPI.new(access_token).batch do |batch_api|
312
+ batch_api.get_object('me')
313
+ batch_api.get_object('me')
314
+ end
359
315
  end
360
-
316
+
361
317
  it "adds any files from the batch operations to the arguments" do
362
- # stub the batch operation
318
+ # stub the batch operation
363
319
  # we test above to ensure that files are properly assimilated into the BatchOperation instance
364
320
  # right now, we want to make sure that batch_api handles them properly
365
321
  @key = "file0_0"
366
322
  @uploadable_io = stub("UploadableIO")
367
323
  batch_op = stub("Koala Batch Operation", :files => {@key => @uploadable_io}, :to_batch_params => {}, :access_token => "foo")
368
324
  Koala::Facebook::BatchOperation.stub(:new).and_return(batch_op)
369
-
370
- Koala.should_receive(:make_request).with(anything, hash_including(@key => @uploadable_io), anything, anything).and_return(@fake_response)
371
- Koala::Facebook::GraphAPI.batch do
372
- Koala::Facebook::GraphAPI.new("bar").put_picture("path/to/file", "image/jpeg")
373
- end
325
+
326
+ Koala.should_receive(:make_request).with(anything, hash_including(@key => @uploadable_io), anything, anything).and_return(@fake_response)
327
+ Koala::Facebook::GraphAPI.new("bar").batch do |batch_api|
328
+ batch_api.put_picture("path/to/file", "image/jpeg")
329
+ end
374
330
  end
375
-
331
+
376
332
  it "preserves operation order" do
377
333
  access_token = "bar"
378
334
  # two requests should generate two batch operations
379
- Koala.should_receive(:make_request) do |url, args, method, options|
335
+ Koala.should_receive(:make_request) do |url, args, method, options|
380
336
  # test the batch operations to make sure they appear in the right order
381
337
  (args ||= {})["batch"].should =~ /.*me\/farglebarg.*otheruser\/bababa/
382
338
  @fake_response
383
339
  end
384
- Koala::Facebook::GraphAPI.batch do
385
- Koala::Facebook::GraphAPI.new(access_token).get_connections('me', "farglebarg")
386
- Koala::Facebook::GraphAPI.new(access_token).get_connections('otheruser', "bababa")
340
+ Koala::Facebook::GraphAPI.new(access_token).batch do |batch_api|
341
+ batch_api.get_connections('me', "farglebarg")
342
+ batch_api.get_connections('otheruser', "bababa")
387
343
  end
388
344
  end
389
-
345
+
390
346
  it "makes a POST request" do
391
347
  Koala.should_receive(:make_request).with(anything, anything, "post", anything).and_return(@fake_response)
392
- Koala::Facebook::GraphAPI.batch do
393
- Koala::Facebook::GraphAPI.new("foo").get_object('me')
348
+ Koala::Facebook::GraphAPI.new("foo").batch do |batch_api|
349
+ batch_api.get_object('me')
394
350
  end
395
351
  end
396
-
352
+
397
353
  it "makes a request to /" do
398
354
  Koala.should_receive(:make_request).with("/", anything, anything, anything).and_return(@fake_response)
399
- Koala::Facebook::GraphAPI.batch do
400
- Koala::Facebook::GraphAPI.new("foo").get_object('me')
355
+ Koala::Facebook::GraphAPI.new("foo").batch do |batch_api|
356
+ batch_api.get_object('me')
401
357
  end
402
358
  end
403
-
359
+
404
360
  it "includes any http options specified at the top level" do
405
361
  http_options = {"a" => "baz"}
406
362
  Koala.should_receive(:make_request).with(anything, anything, anything, hash_including(http_options)).and_return(@fake_response)
407
- Koala::Facebook::GraphAPI.batch(http_options) do
408
- Koala::Facebook::GraphAPI.new("foo").get_object('me')
363
+ Koala::Facebook::GraphAPI.new("foo").batch(http_options) do |batch_api|
364
+ batch_api.get_object('me')
409
365
  end
410
366
  end
411
367
  end
412
-
368
+
413
369
  describe "processing the request" do
414
370
  it "throws an error if the response is not 200" do
415
371
  Koala.stub(:make_request).and_return(Koala::Response.new(500, "[]", {}))
416
- expect { Koala::Facebook::GraphAPI.batch do
417
- Koala::Facebook::GraphAPI.new("foo").get_object('me')
372
+ expect { Koala::Facebook::GraphAPI.new("foo").batch do |batch_api|
373
+ batch_api.get_object('me')
418
374
  end }.to raise_exception(Koala::Facebook::APIError)
419
375
  end
420
-
376
+
421
377
  it "throws an error if the response is a Batch API-style error" do
422
378
  Koala.stub(:make_request).and_return(Koala::Response.new(200, '{"error":190,"error_description":"Error validating access token."}', {}))
423
- expect { Koala::Facebook::GraphAPI.batch do
424
- Koala::Facebook::GraphAPI.new("foo").get_object('me')
425
- end }.to raise_exception(Koala::Facebook::APIError)
379
+ expect { Koala::Facebook::GraphAPI.new("foo").batch do |batch_api|
380
+ batch_api.get_object('me')
381
+ end }.to raise_exception(Koala::Facebook::APIError)
426
382
  end
427
-
383
+
428
384
  it "returns the result status if http_component is status" do
429
385
  Koala.stub(:make_request).and_return(Koala::Response.new(200, '[{"code":203,"headers":[{"name":"Content-Type","value":"text/javascript; charset=UTF-8"}],"body":"{\"id\":\"1234\"}"}]', {}))
430
- result = Koala::Facebook::GraphAPI.batch do
431
- @api.get_object("koppel", {}, :http_component => :status)
386
+ result = @api.batch do |batch_api|
387
+ batch_api.get_object("koppel", {}, :http_component => :status)
432
388
  end
433
389
  result[0].should == 203
434
390
  end
435
391
 
436
392
  it "returns the result headers as a hash if http_component is headers" do
437
393
  Koala.stub(:make_request).and_return(Koala::Response.new(200, '[{"code":203,"headers":[{"name":"Content-Type","value":"text/javascript; charset=UTF-8"}],"body":"{\"id\":\"1234\"}"}]', {}))
438
- result = Koala::Facebook::GraphAPI.batch do
439
- @api.get_object("koppel", {}, :http_component => :headers)
394
+ result = @api.batch do |batch_api|
395
+ batch_api.get_object("koppel", {}, :http_component => :headers)
440
396
  end
441
397
  result[0].should == {"Content-Type" => "text/javascript; charset=UTF-8"}
442
398
  end
443
399
  end
444
-
445
- it "is not available on the GraphAndRestAPI class" do
446
- Koala::Facebook::GraphAndRestAPI.should_not respond_to(:batch)
400
+
401
+ it "is thread safe" do
402
+ # ensure batch operations on one thread don't affect those on another
403
+ thread_one_count = 0
404
+ thread_two_count = 0
405
+ first_count = 20
406
+ second_count = 10
407
+
408
+ Koala.stub(:make_request).and_return(@fake_response)
409
+
410
+ thread1 = Thread.new do
411
+ @api.batch do |batch_api|
412
+ first_count.times {|i| batch_api.get_object("me"); sleep(0.01) }
413
+ thread_one_count = batch_api.batch_calls.count
414
+ end
415
+ end
416
+
417
+ thread2 = Thread.new do
418
+ @api.batch do |batch_api|
419
+ second_count.times {|i| batch_api.get_object("me"); sleep(0.01) }
420
+ thread_two_count = batch_api.batch_calls.count
421
+ end
422
+ end
423
+
424
+ thread1.join
425
+ thread2.join
426
+
427
+ thread_one_count.should == first_count
428
+ thread_two_count.should == second_count
447
429
  end
448
430
  end
449
431
  end
450
-
432
+
451
433
  describe "usage tests" do
452
- it "should be able get two results at once" do
453
- me, koppel = Koala::Facebook::GraphAPI.batch do
454
- @api.get_object('me')
455
- @api.get_object('koppel')
434
+ it "can get two results at once" do
435
+ me, koppel = @api.batch do |batch_api|
436
+ batch_api.get_object('me')
437
+ batch_api.get_object('koppel')
456
438
  end
457
439
  me['id'].should_not be_nil
458
440
  koppel['id'].should_not be_nil
459
441
  end
460
442
 
461
-
462
443
  it "works with GraphAndRestAPI instances" do
463
- me, koppel = Koala::Facebook::GraphAPI.batch do
464
- Koala::Facebook::GraphAndRestAPI.new(@api.access_token).get_object('me')
465
- @api.get_object('koppel')
444
+ me, koppel = Koala::Facebook::GraphAndRestAPI.new(@api.access_token).batch do |batch_api|
445
+ batch_api.get_object('me')
446
+ batch_api.get_object('koppel')
466
447
  end
467
448
  me['id'].should_not be_nil
468
449
  koppel['id'].should_not be_nil
469
450
  end
470
451
 
471
452
  it 'should be able to make mixed calls inside of a batch' do
472
- me, friends = Koala::Facebook::GraphAPI.batch do
473
- @api.get_object('me')
474
- @api.get_connections('me', 'friends')
453
+ me, friends = @api.batch do |batch_api|
454
+ batch_api.get_object('me')
455
+ batch_api.get_connections('me', 'friends')
475
456
  end
476
457
  me['id'].should_not be_nil
477
458
  friends.should be_an(Array)
478
459
  end
479
460
 
480
461
  it 'should be able to make a get_picture call inside of a batch' do
481
- pictures = Koala::Facebook::GraphAPI.batch do
482
- @api.get_picture('me')
462
+ pictures = @api.batch do |batch_api|
463
+ batch_api.get_picture('me')
483
464
  end
484
465
  pictures.first.should_not be_empty
485
466
  end
486
-
467
+
487
468
  it "should handle requests for two different tokens" do
488
- me, insights = Koala::Facebook::GraphAPI.batch do
489
- @api.get_object('me')
490
- @app_api.get_connections(@app_id, 'insights')
469
+ me, insights = @api.batch do |batch_api|
470
+ batch_api.get_object('me')
471
+ batch_api.get_connections(@app_id, 'insights', {}, {"access_token" => @app_api.access_token})
491
472
  end
492
473
  me['id'].should_not be_nil
493
474
  insights.should be_an(Array)
494
475
  end
495
-
476
+
496
477
  it "inserts errors in the appropriate place, without breaking other results" do
497
- failed_insights, koppel = Koala::Facebook::GraphAPI.batch do
498
- @api.get_connections(@app_id, 'insights')
499
- @app_api.get_object("koppel")
478
+ failed_insights, koppel = @api.batch do |batch_api|
479
+ batch_api.get_connections(@app_id, 'insights')
480
+ batch_api.get_object("koppel", {}, {"access_token" => @app_api.access_token})
500
481
  end
501
482
  failed_insights.should be_a(Koala::Facebook::APIError)
502
483
  koppel["id"].should_not be_nil
@@ -505,96 +486,124 @@ describe "Koala::Facebook::GraphAPI in batch mode" do
505
486
  it "handles different request methods" do
506
487
  result = @api.put_wall_post("Hello, world, from the test suite batch API!")
507
488
  wall_post = result["id"]
508
-
509
- wall_post, koppel = Koala::Facebook::GraphAPI.batch do
510
- @api.put_like(wall_post)
511
- @api.delete_object(wall_post)
489
+
490
+ wall_post, koppel = @api.batch do |batch_api|
491
+ batch_api.put_like(wall_post)
492
+ batch_api.delete_object(wall_post)
512
493
  end
513
494
  end
514
-
495
+
515
496
  it "allows FQL" do
516
- result = Koala::Facebook::GraphAPI.batch do
517
- @api.graph_call("method/fql.query", {:query=>"select name from user where uid=4"}, "post")
497
+ result = @api.batch do |batch_api|
498
+ batch_api.graph_call("method/fql.query", {:query=>"select name from user where uid=4"}, "post")
518
499
  end
519
-
500
+
520
501
  fql_result = result[0]
521
502
  fql_result[0].should be_a(Hash)
522
503
  fql_result[0]["name"].should == "Mark Zuckerberg"
523
504
  end
524
-
505
+
525
506
  describe "binary files" do
526
507
  it "posts binary files" do
527
508
  file = File.open(File.join(File.dirname(__FILE__), "..", "fixtures", "beach.jpg"))
528
509
 
529
- result = Koala::Facebook::GraphAPI.batch do
530
- @api.put_picture(file)
510
+ Koala::Facebook::BatchOperation.instance_variable_set(:@identifier, 0)
511
+ result = @api.batch do |batch_api|
512
+ batch_api.put_picture(file)
531
513
  end
532
-
514
+
533
515
  @temporary_object_id = result[0]["id"]
534
516
  @temporary_object_id.should_not be_nil
535
517
  end
536
-
518
+
537
519
  it "posts binary files with multiple requests" do
538
520
  file = File.open(File.join(File.dirname(__FILE__), "..", "fixtures", "beach.jpg"))
539
521
 
540
- results = Koala::Facebook::GraphAPI.batch do
541
- @api.put_picture(file)
542
- @api.put_picture(file, {}, "koppel")
522
+ Koala::Facebook::BatchOperation.instance_variable_set(:@identifier, 0)
523
+ results = @api.batch do |batch_api|
524
+ batch_api.put_picture(file)
525
+ batch_api.put_picture(file, {}, "koppel")
543
526
  end
544
527
  results[0]["id"].should_not be_nil
545
528
  results[1]["id"].should_not be_nil
546
529
  end
547
530
  end
548
-
531
+
549
532
  describe "relating requests" do
550
533
  it "allows you create relationships between requests without omit_response_on_success" do
551
- results = Koala::Facebook::GraphAPI.batch do
552
- @api.get_connections("me", "friends", {:limit => 5}, :batch_args => {:name => "get-friends"})
553
- @api.get_objects("{result=get-friends:$.data.*.id}")
534
+ results = @api.batch do |batch_api|
535
+ batch_api.get_connections("me", "friends", {:limit => 5}, :batch_args => {:name => "get-friends"})
536
+ batch_api.get_objects("{result=get-friends:$.data.*.id}")
554
537
  end
555
-
538
+
556
539
  results[0].should be_nil
557
540
  results[1].should be_an(Hash)
558
541
  end
559
-
542
+
560
543
  it "allows you create relationships between requests with omit_response_on_success" do
561
- results = Koala::Facebook::GraphAPI.batch do
562
- @api.get_connections("me", "friends", {:limit => 5}, :batch_args => {:name => "get-friends", :omit_response_on_success => false})
563
- @api.get_objects("{result=get-friends:$.data.*.id}")
544
+ results = @api.batch do |batch_api|
545
+ batch_api.get_connections("me", "friends", {:limit => 5}, :batch_args => {:name => "get-friends", :omit_response_on_success => false})
546
+ batch_api.get_objects("{result=get-friends:$.data.*.id}")
564
547
  end
565
-
548
+
566
549
  results[0].should be_an(Array)
567
550
  results[1].should be_an(Hash)
568
551
  end
569
-
552
+
570
553
  it "allows you to create dependencies" do
571
- me, koppel = Koala::Facebook::GraphAPI.batch do
572
- @api.get_object("me", {}, :batch_args => {:name => "getme"})
573
- @api.get_object("koppel", {}, :batch_args => {:depends_on => "getme"})
554
+ me, koppel = @api.batch do |batch_api|
555
+ batch_api.get_object("me", {}, :batch_args => {:name => "getme"})
556
+ batch_api.get_object("koppel", {}, :batch_args => {:depends_on => "getme"})
574
557
  end
575
-
558
+
576
559
  me.should be_nil # gotcha! it's omitted because it's a successfully-executed dependency
577
560
  koppel["id"].should_not be_nil
578
561
  end
579
-
562
+
580
563
  it "properly handles dependencies that fail" do
581
- data, koppel = Koala::Facebook::GraphAPI.batch do
582
- @api.get_connections(@app_id, 'insights', {}, :batch_args => {:name => "getdata"})
583
- @api.get_object("koppel", {}, :batch_args => {:depends_on => "getdata"})
564
+ data, koppel = @api.batch do |batch_api|
565
+ batch_api.get_connections(@app_id, 'insights', {}, :batch_args => {:name => "getdata"})
566
+ batch_api.get_object("koppel", {}, :batch_args => {:depends_on => "getdata"})
584
567
  end
585
568
 
586
569
  data.should be_a(Koala::Facebook::APIError)
587
570
  koppel.should be_nil
588
571
  end
589
-
572
+
590
573
  it "throws an error for badly-constructed request relationships" do
591
- expect {
592
- Koala::Facebook::GraphAPI.batch do
593
- @api.get_connections("me", "friends", {:limit => 5})
594
- @api.get_objects("{result=i-dont-exist:$.data.*.id}")
574
+ expect {
575
+ @api.batch do |batch_api|
576
+ batch_api.get_connections("me", "friends", {:limit => 5})
577
+ batch_api.get_objects("{result=i-dont-exist:$.data.*.id}")
595
578
  end
596
579
  }.to raise_exception(Koala::Facebook::APIError)
597
580
  end
598
581
  end
599
582
  end
583
+
584
+ describe "new interface" do
585
+ it "includes a deprecation warning on GraphAPI" do
586
+ begin
587
+ Koala::Facebook::GraphAPI.batch do
588
+ end
589
+ rescue NoMethodError => @err
590
+ end
591
+
592
+ # verify the message points people to the wiki page
593
+ @err.should
594
+ @err.message.should =~ /https\:\/\/github.com\/arsduo\/koala\/wiki\/Batch-requests/
595
+ end
596
+
597
+ it "includes a deprecation warning on GraphAndRESTAPI" do
598
+ begin
599
+ Koala::Facebook::GraphAndRestAPI.batch do
600
+ end
601
+ rescue NoMethodError => @err
602
+ end
603
+
604
+ # verify the message points people to the wiki page
605
+ @err.should
606
+ @err.message.should =~ /https\:\/\/github.com\/arsduo\/koala\/wiki\/Batch-requests/
607
+ end
608
+ end
600
609
  end