tyler_koala 1.2.0beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. data/.autotest +12 -0
  2. data/.gitignore +5 -0
  3. data/.travis.yml +9 -0
  4. data/CHANGELOG +185 -0
  5. data/Gemfile +11 -0
  6. data/LICENSE +22 -0
  7. data/Manifest +39 -0
  8. data/Rakefile +16 -0
  9. data/autotest/discover.rb +1 -0
  10. data/koala.gemspec +50 -0
  11. data/lib/koala.rb +119 -0
  12. data/lib/koala/batch_operation.rb +74 -0
  13. data/lib/koala/graph_api.rb +281 -0
  14. data/lib/koala/graph_batch_api.rb +87 -0
  15. data/lib/koala/graph_collection.rb +54 -0
  16. data/lib/koala/http_service.rb +161 -0
  17. data/lib/koala/oauth.rb +181 -0
  18. data/lib/koala/realtime_updates.rb +89 -0
  19. data/lib/koala/rest_api.rb +95 -0
  20. data/lib/koala/test_users.rb +102 -0
  21. data/lib/koala/uploadable_io.rb +180 -0
  22. data/lib/koala/utils.rb +7 -0
  23. data/readme.md +160 -0
  24. data/spec/cases/api_base_spec.rb +101 -0
  25. data/spec/cases/error_spec.rb +30 -0
  26. data/spec/cases/graph_and_rest_api_spec.rb +48 -0
  27. data/spec/cases/graph_api_batch_spec.rb +600 -0
  28. data/spec/cases/graph_api_spec.rb +42 -0
  29. data/spec/cases/http_service_spec.rb +420 -0
  30. data/spec/cases/koala_spec.rb +21 -0
  31. data/spec/cases/oauth_spec.rb +428 -0
  32. data/spec/cases/realtime_updates_spec.rb +198 -0
  33. data/spec/cases/rest_api_spec.rb +41 -0
  34. data/spec/cases/test_users_spec.rb +281 -0
  35. data/spec/cases/uploadable_io_spec.rb +206 -0
  36. data/spec/cases/utils_spec.rb +8 -0
  37. data/spec/fixtures/beach.jpg +0 -0
  38. data/spec/fixtures/cat.m4v +0 -0
  39. data/spec/fixtures/facebook_data.yml +61 -0
  40. data/spec/fixtures/mock_facebook_responses.yml +439 -0
  41. data/spec/spec_helper.rb +43 -0
  42. data/spec/support/graph_api_shared_examples.rb +502 -0
  43. data/spec/support/json_testing_fix.rb +42 -0
  44. data/spec/support/koala_test.rb +163 -0
  45. data/spec/support/mock_http_service.rb +98 -0
  46. data/spec/support/ordered_hash.rb +205 -0
  47. data/spec/support/rest_api_shared_examples.rb +285 -0
  48. data/spec/support/uploadable_io_shared_examples.rb +70 -0
  49. metadata +221 -0
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Koala::Facebook::GraphAPI" do
4
+ describe "class consolidation" do
5
+ before :each do
6
+ Koala::Utils.stub(:deprecate) # avoid actual messages to stderr
7
+ end
8
+
9
+ it "still allows you to instantiate a GraphAndRestAPI object" do
10
+ api = Koala::Facebook::GraphAPI.new("token").should be_a(Koala::Facebook::GraphAPI)
11
+ end
12
+
13
+ it "ultimately creates an API object" do
14
+ api = Koala::Facebook::GraphAPI.new("token").should be_a(Koala::Facebook::API)
15
+ end
16
+
17
+ it "fires a depreciation warning" do
18
+ Koala::Utils.should_receive(:deprecate)
19
+ api = Koala::Facebook::GraphAPI.new("token")
20
+ end
21
+ end
22
+
23
+ context "with an access token" do
24
+ before :each do
25
+ @api = Koala::Facebook::API.new(@token)
26
+ end
27
+
28
+ it_should_behave_like "Koala GraphAPI"
29
+ it_should_behave_like "Koala GraphAPI with an access token"
30
+ it_should_behave_like "Koala GraphAPI with GraphCollection"
31
+ end
32
+
33
+ context "without an access token" do
34
+ before :each do
35
+ @api = Koala::Facebook::API.new
36
+ end
37
+
38
+ it_should_behave_like "Koala GraphAPI"
39
+ it_should_behave_like "Koala GraphAPI without an access token"
40
+ it_should_behave_like "Koala GraphAPI with GraphCollection"
41
+ end
42
+ end
@@ -0,0 +1,420 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Koala::HTTPService" do
4
+ it "has a faraday_middleware accessor" do
5
+ Koala::HTTPService.methods.map(&:to_sym).should include(:faraday_middleware)
6
+ Koala::HTTPService.methods.map(&:to_sym).should include(:faraday_middleware=)
7
+ end
8
+
9
+ it "has an faraday_options accessor" do
10
+ Koala::HTTPService.should respond_to(:faraday_options)
11
+ Koala::HTTPService.should respond_to(:faraday_options=)
12
+ end
13
+
14
+ it "sets faraday_options to {} by default" do
15
+ Koala::HTTPService.faraday_options.should == {}
16
+ end
17
+
18
+ describe "DEFAULT_MIDDLEWARE" do
19
+ before :each do
20
+ @builder = stub("Faraday connection builder")
21
+ @builder.stub(:request)
22
+ @builder.stub(:adapter)
23
+ end
24
+
25
+ it "is defined" do
26
+ Koala::HTTPService.const_defined?("DEFAULT_MIDDLEWARE").should be_true
27
+ end
28
+
29
+ it "adds multipart" do
30
+ @builder.should_receive(:request).with(:multipart)
31
+ Koala::HTTPService::DEFAULT_MIDDLEWARE.call(@builder)
32
+ end
33
+
34
+ it "adds url_encoded" do
35
+ @builder.should_receive(:request).with(:url_encoded)
36
+ Koala::HTTPService::DEFAULT_MIDDLEWARE.call(@builder)
37
+ end
38
+
39
+ it "uses the default adapter" do
40
+ adapter = :testing_now
41
+ Faraday.stub(:default_adapter).and_return(adapter)
42
+ @builder.should_receive(:adapter).with(adapter)
43
+ Koala::HTTPService::DEFAULT_MIDDLEWARE.call(@builder)
44
+ end
45
+ end
46
+
47
+ describe "server" do
48
+ describe "with no options" do
49
+ it "returns the REST server if options[:rest_api]" do
50
+ Koala::HTTPService.server(:rest_api => true).should =~ Regexp.new(Koala::Facebook::REST_SERVER)
51
+ end
52
+
53
+ it "returns the graph server if !options[:rest_api]" do
54
+ Koala::HTTPService.server(:rest_api => false).should =~ Regexp.new(Koala::Facebook::GRAPH_SERVER)
55
+ Koala::HTTPService.server({}).should =~ Regexp.new(Koala::Facebook::GRAPH_SERVER)
56
+ end
57
+ end
58
+
59
+ describe "with options[:beta]" do
60
+ before :each do
61
+ @options = {:beta => true}
62
+ end
63
+
64
+ it "returns the beta REST server if options[:rest_api]" do
65
+ server = Koala::HTTPService.server(@options.merge(:rest_api => true))
66
+ server.should =~ Regexp.new("beta.#{Koala::Facebook::REST_SERVER}")
67
+ end
68
+
69
+ it "returns the beta rest server if !options[:rest_api]" do
70
+ server = Koala::HTTPService.server(@options)
71
+ server.should =~ Regexp.new("beta.#{Koala::Facebook::GRAPH_SERVER}")
72
+ end
73
+ end
74
+
75
+ describe "with options[:video]" do
76
+ before :each do
77
+ @options = {:video => true}
78
+ end
79
+
80
+ it "should return the REST video server if options[:rest_api]" do
81
+ server = Koala::HTTPService.server(@options.merge(:rest_api => true))
82
+ server.should =~ Regexp.new(Koala::Facebook::REST_SERVER.gsub(/\.facebook/, "-video.facebook"))
83
+ end
84
+
85
+ it "should return the graph video server if !options[:rest_api]" do
86
+ server = Koala::HTTPService.server(@options)
87
+ server.should =~ Regexp.new(Koala::Facebook::GRAPH_SERVER.gsub(/\.facebook/, "-video.facebook"))
88
+ end
89
+ end
90
+ end
91
+
92
+ describe "#encode_params" do
93
+ it "should return an empty string if param_hash evaluates to false" do
94
+ Koala::HTTPService.encode_params(nil).should == ''
95
+ end
96
+
97
+ it "should convert values to JSON if the value is not a String" do
98
+ val = 'json_value'
99
+ not_a_string = 'not_a_string'
100
+ not_a_string.stub(:is_a?).and_return(false)
101
+ MultiJson.should_receive(:encode).with(not_a_string).and_return(val)
102
+
103
+ string = "hi"
104
+
105
+ args = {
106
+ not_a_string => not_a_string,
107
+ string => string
108
+ }
109
+
110
+ result = Koala::HTTPService.encode_params(args)
111
+ result.split('&').find do |key_and_val|
112
+ key_and_val.match("#{not_a_string}=#{val}")
113
+ end.should be_true
114
+ end
115
+
116
+ it "should escape all values" do
117
+ args = Hash[*(1..4).map {|i| [i.to_s, "Value #{i}($"]}.flatten]
118
+
119
+ result = Koala::HTTPService.encode_params(args)
120
+ result.split('&').each do |key_val|
121
+ key, val = key_val.split('=')
122
+ val.should == CGI.escape(args[key])
123
+ end
124
+ end
125
+
126
+ it "should convert all keys to Strings" do
127
+ args = Hash[*(1..4).map {|i| [i, "val#{i}"]}.flatten]
128
+
129
+ result = Koala::HTTPService.encode_params(args)
130
+ result.split('&').each do |key_val|
131
+ key, val = key_val.split('=')
132
+ key.should == args.find{|key_val_arr| key_val_arr.last == val}.first.to_s
133
+ end
134
+ end
135
+ end
136
+
137
+ describe "#make_request" do
138
+ before :each do
139
+ # Setup stubs for make_request to execute without exceptions
140
+ @mock_body = stub('Typhoeus response body')
141
+ @mock_headers_hash = stub({:value => "headers hash"})
142
+ @mock_http_response = stub("Faraday Response", :status => 200, :headers => @mock_headers_hash, :body => @mock_body)
143
+
144
+ @mock_connection = stub("Faraday connection")
145
+ @mock_connection.stub(:get).and_return(@mock_http_response)
146
+ @mock_connection.stub(:post).and_return(@mock_http_response)
147
+ Faraday.stub(:new).and_return(@mock_connection)
148
+ end
149
+
150
+ describe "creating the Faraday connection" do
151
+ it "creates a Faraday connection using the server" do
152
+ server = "foo"
153
+ Koala::HTTPService.stub(:server).and_return(server)
154
+ Faraday.should_receive(:new).with(server, anything).and_return(@mock_connection)
155
+ Koala::HTTPService.make_request("anything", {}, "anything")
156
+ end
157
+
158
+ it "merges Koala::HTTPService.faraday_options into the request params" do
159
+ http_options = {:a => 2, :c => "3"}
160
+ Koala::HTTPService.faraday_options = http_options
161
+ Faraday.should_receive(:new).with(anything, hash_including(http_options)).and_return(@mock_connection)
162
+ Koala::HTTPService.make_request("anything", {}, "get")
163
+ end
164
+
165
+ it "merges any provided options into the request params" do
166
+ options = {:a => 2, :c => "3"}
167
+ Faraday.should_receive(:new).with(anything, hash_including(options)).and_return(@mock_connection)
168
+ Koala::HTTPService.make_request("anything", {}, "get", options)
169
+ end
170
+
171
+ it "overrides Koala::HTTPService.faraday_options with any provided options for the request params" do
172
+ options = {:a => 2, :c => "3"}
173
+ http_options = {:a => :a}
174
+ Koala::HTTPService.stub(:faraday_options).and_return(http_options)
175
+
176
+ Faraday.should_receive(:new).with(anything, hash_including(http_options.merge(options))).and_return(@mock_connection)
177
+ Koala::HTTPService.make_request("anything", {}, "get", options)
178
+ end
179
+
180
+ it "forces use_ssl to true if an access token is present" do
181
+ options = {:use_ssl => false}
182
+ Koala::HTTPService.stub(:faraday_options).and_return(:use_ssl => false)
183
+ Faraday.should_receive(:new).with(anything, hash_including(:use_ssl => true)).and_return(@mock_connection)
184
+ Koala::HTTPService.make_request("anything", {"access_token" => "foo"}, "get", options)
185
+ end
186
+
187
+ it "calls server with the composite options" do
188
+ options = {:a => 2, :c => "3"}
189
+ http_options = {:a => :a}
190
+ Koala::HTTPService.stub(:faraday_options).and_return(http_options)
191
+ Koala::HTTPService.should_receive(:server).with(hash_including(http_options.merge(options))).and_return("foo")
192
+ Koala::HTTPService.make_request("anything", {}, "get", options)
193
+ end
194
+
195
+ it "uses the default builder block if HTTPService.faraday_middleware block is not defined" do
196
+ Koala::HTTPService.stub(:faraday_middleware).and_return(nil)
197
+ Faraday.should_receive(:new).with(anything, anything, &Koala::HTTPService::DEFAULT_MIDDLEWARE).and_return(@mock_connection)
198
+ Koala::HTTPService.make_request("anything", {}, "get")
199
+ end
200
+
201
+ it "uses the defined HTTPService.faraday_middleware block if defined" do
202
+ block = Proc.new { }
203
+ Koala::HTTPService.should_receive(:faraday_middleware).and_return(block)
204
+ Faraday.should_receive(:new).with(anything, anything, &block).and_return(@mock_connection)
205
+ Koala::HTTPService.make_request("anything", {}, "get")
206
+ end
207
+ end
208
+
209
+ it "makes a POST request if the verb isn't get" do
210
+ @mock_connection.should_receive(:post).and_return(@mock_http_response)
211
+ Koala::HTTPService.make_request("anything", {}, "anything")
212
+ end
213
+
214
+ it "includes the verb in the body if the verb isn't get" do
215
+ verb = "eat"
216
+ @mock_connection.should_receive(:post).with(anything, hash_including("method" => verb)).and_return(@mock_http_response)
217
+ Koala::HTTPService.make_request("anything", {}, verb)
218
+ end
219
+
220
+ it "makes a GET request if the verb is get" do
221
+ @mock_connection.should_receive(:get).and_return(@mock_http_response)
222
+ Koala::HTTPService.make_request("anything", {}, "get")
223
+ end
224
+
225
+ describe "for GETs" do
226
+ it "submits the arguments in the body" do
227
+ # technically this is done for all requests, but you don't send GET requests with files
228
+ args = {"a" => :b, "c" => 3}
229
+ Faraday.should_receive(:new).with(anything, hash_including(:params => args)).and_return(@mock_connection)
230
+ Koala::HTTPService.make_request("anything", args, "get")
231
+ end
232
+
233
+ it "submits nothing to the body" do
234
+ # technically this is done for all requests, but you don't send GET requests with files
235
+ args = {"a" => :b, "c" => 3}
236
+ @mock_connection.should_receive(:get).with(anything, {}).and_return(@mock_http_response)
237
+ Koala::HTTPService.make_request("anything", args, "get")
238
+ end
239
+ end
240
+
241
+ describe "for POSTs" do
242
+ it "submits the arguments in the body" do
243
+ # technically this is done for all requests, but you don't send GET requests with files
244
+ args = {"a" => :b, "c" => 3}
245
+ @mock_connection.should_receive(:post).with(anything, hash_including(args)).and_return(@mock_http_response)
246
+ Koala::HTTPService.make_request("anything", args, "post")
247
+ end
248
+
249
+ it "turns any UploadableIOs to UploadIOs" do
250
+ # technically this is done for all requests, but you don't send GET requests with files
251
+ upload_io = stub("UploadIO")
252
+ u = Koala::UploadableIO.new("/path/to/stuff", "img/jpg")
253
+ u.stub(:to_upload_io).and_return(upload_io)
254
+ @mock_connection.should_receive(:post).with(anything, hash_including("source" => upload_io)).and_return(@mock_http_response)
255
+ Koala::HTTPService.make_request("anything", {:source => u}, "post")
256
+ end
257
+ end
258
+ end
259
+
260
+ describe "deprecated options" do
261
+ before :each do
262
+ Koala::HTTPService.stub(:faraday_options).and_return({})
263
+ end
264
+
265
+ {
266
+ :timeout => :timeout,
267
+ :always_use_ssl => :use_ssl,
268
+ :proxy => :proxy
269
+ }.each_pair do |deprecated_method, parameter|
270
+ describe "##{deprecated_method}" do
271
+ context "read" do
272
+ it "reads faraday_options[:#{parameter}]" do
273
+ value = "foo"
274
+ Koala::HTTPService.faraday_options[parameter] = value
275
+ Koala::HTTPService.send(deprecated_method).should == value
276
+ end
277
+
278
+ it "generates a deprecation warning" do
279
+ Koala::Utils.should_receive(:deprecate)
280
+ Koala::HTTPService.send(deprecated_method)
281
+ end
282
+ end
283
+
284
+ context "write" do
285
+ it "writes to faraday_options[:#{parameter}]" do
286
+ Koala::HTTPService.faraday_options[parameter] = nil
287
+ value = "foo"
288
+ Koala::HTTPService.send(:"#{deprecated_method}=", value)
289
+ Koala::HTTPService.faraday_options[parameter].should == value
290
+ end
291
+
292
+ it "generates a deprecation warning" do
293
+ Koala::Utils.should_receive(:deprecate)
294
+ Koala::HTTPService.send(:"#{deprecated_method}=", 2)
295
+ end
296
+ end
297
+ end
298
+ end
299
+
300
+ # ssl options
301
+ [:ca_path, :ca_file, :verify_mode].each do |deprecated_method|
302
+ describe "##{deprecated_method}" do
303
+ context "read" do
304
+ it "reads faraday_options[:ssl][:#{deprecated_method}] if faraday_options[:ssl]" do
305
+ value = "foo"
306
+ Koala::HTTPService.faraday_options[:ssl] = {deprecated_method => value}
307
+ Koala::HTTPService.send(deprecated_method).should == value
308
+ end
309
+
310
+ it "returns nil if faraday_options[:ssl] is not defined" do
311
+ Koala::HTTPService.send(deprecated_method).should be_nil
312
+ end
313
+
314
+ it "generates a deprecation warning" do
315
+ Koala::Utils.should_receive(:deprecate)
316
+ Koala::HTTPService.send(deprecated_method)
317
+ end
318
+ end
319
+
320
+ context "write" do
321
+ it "defines faraday_options[:ssl] if not defined" do
322
+ Koala::HTTPService.faraday_options[:ssl] = nil
323
+ value = "foo"
324
+ Koala::HTTPService.send(:"#{deprecated_method}=", value)
325
+ Koala::HTTPService.faraday_options[:ssl].should
326
+ end
327
+
328
+ it "writes to faraday_options[:ssl][:#{deprecated_method}]" do
329
+ value = "foo"
330
+ Koala::HTTPService.send(:"#{deprecated_method}=", value)
331
+ Koala::HTTPService.faraday_options[:ssl].should
332
+ Koala::HTTPService.faraday_options[:ssl][deprecated_method].should == value
333
+ end
334
+
335
+ it "does not redefine faraday_options[:ssl] if already defined" do
336
+ hash = {:a => 2}
337
+ Koala::HTTPService.faraday_options[:ssl] = hash
338
+ Koala::HTTPService.send(:"#{deprecated_method}=", 3)
339
+ Koala::HTTPService.faraday_options[:ssl].should include(hash)
340
+ end
341
+
342
+ it "generates a deprecation warning" do
343
+ Koala::Utils.should_receive(:deprecate)
344
+ Koala::HTTPService.send(:"#{deprecated_method}=", 2)
345
+ end
346
+ end
347
+ end
348
+ end
349
+
350
+ describe "per-request options" do
351
+ before :each do
352
+ # Setup stubs for make_request to execute without exceptions
353
+ @mock_body = stub('Typhoeus response body')
354
+ @mock_headers_hash = stub({:value => "headers hash"})
355
+ @mock_http_response = stub("Faraday Response", :status => 200, :headers => @mock_headers_hash, :body => @mock_body)
356
+
357
+ @mock_connection = stub("Faraday connection")
358
+ @mock_connection.stub(:get).and_return(@mock_http_response)
359
+ @mock_connection.stub(:post).and_return(@mock_http_response)
360
+ Faraday.stub(:new).and_return(@mock_connection)
361
+ end
362
+
363
+ describe ":typhoeus_options" do
364
+ it "merges any typhoeus_options into options" do
365
+ typhoeus_options = {:a => 2}
366
+ Faraday.should_receive(:new).with(anything, hash_including(typhoeus_options)).and_return(@mock_connection)
367
+ Koala::HTTPService.make_request("anything", {}, "get", :typhoeus_options => typhoeus_options)
368
+ end
369
+
370
+ it "deletes the typhoeus_options key" do
371
+ typhoeus_options = {:a => 2}
372
+ Faraday.should_receive(:new).with(anything, hash_not_including(:typhoeus_options => typhoeus_options)).and_return(@mock_connection)
373
+ Koala::HTTPService.make_request("anything", {}, "get", :typhoeus_options => typhoeus_options)
374
+ end
375
+ end
376
+
377
+ describe ":ca_path" do
378
+ it "sets any ca_path into options[:ssl]" do
379
+ ca_path = :foo
380
+ Faraday.should_receive(:new).with(anything, hash_including(:ssl => hash_including(:ca_path => ca_path))).and_return(@mock_connection)
381
+ Koala::HTTPService.make_request("anything", {}, "get", :ca_path => ca_path)
382
+ end
383
+
384
+ it "deletes the ca_path key" do
385
+ ca_path = :foo
386
+ Faraday.should_receive(:new).with(anything, hash_not_including(:ca_path => ca_path)).and_return(@mock_connection)
387
+ Koala::HTTPService.make_request("anything", {}, "get", :ca_path => ca_path)
388
+ end
389
+ end
390
+
391
+ describe ":ca_file" do
392
+ it "sets any ca_file into options[:ssl]" do
393
+ ca_file = :foo
394
+ Faraday.should_receive(:new).with(anything, hash_including(:ssl => hash_including(:ca_file => ca_file))).and_return(@mock_connection)
395
+ Koala::HTTPService.make_request("anything", {}, "get", :ca_file => ca_file)
396
+ end
397
+
398
+ it "deletes the ca_file key" do
399
+ ca_file = :foo
400
+ Faraday.should_receive(:new).with(anything, hash_not_including(:ca_file => ca_file)).and_return(@mock_connection)
401
+ Koala::HTTPService.make_request("anything", {}, "get", :ca_file => ca_file)
402
+ end
403
+ end
404
+
405
+ describe ":verify_mode" do
406
+ it "sets any verify_mode into options[:ssl]" do
407
+ verify_mode = :foo
408
+ Faraday.should_receive(:new).with(anything, hash_including(:ssl => hash_including(:verify_mode => verify_mode))).and_return(@mock_connection)
409
+ Koala::HTTPService.make_request("anything", {}, "get", :verify_mode => verify_mode)
410
+ end
411
+
412
+ it "deletes the verify_mode key" do
413
+ verify_mode = :foo
414
+ Faraday.should_receive(:new).with(anything, hash_not_including(:verify_mode => verify_mode)).and_return(@mock_connection)
415
+ Koala::HTTPService.make_request("anything", {}, "get", :verify_mode => verify_mode)
416
+ end
417
+ end
418
+ end
419
+ end
420
+ end