koala 2.4.0 → 3.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/test.yml +32 -0
  3. data/Gemfile +5 -3
  4. data/ISSUE_TEMPLATE +25 -0
  5. data/PULL_REQUEST_TEMPLATE +11 -0
  6. data/changelog.md +161 -4
  7. data/code_of_conduct.md +64 -12
  8. data/koala.gemspec +5 -1
  9. data/lib/koala/api/batch_operation.rb +3 -6
  10. data/lib/koala/api/{graph_api.rb → graph_api_methods.rb} +29 -104
  11. data/lib/koala/api/graph_batch_api.rb +112 -65
  12. data/lib/koala/api/graph_collection.rb +19 -12
  13. data/lib/koala/api/graph_error_checker.rb +4 -3
  14. data/lib/koala/api.rb +65 -26
  15. data/lib/koala/configuration.rb +56 -0
  16. data/lib/koala/errors.rb +22 -2
  17. data/lib/koala/http_service/request.rb +133 -0
  18. data/lib/koala/http_service/response.rb +6 -4
  19. data/lib/koala/http_service/uploadable_io.rb +0 -5
  20. data/lib/koala/http_service.rb +29 -76
  21. data/lib/koala/oauth.rb +8 -8
  22. data/lib/koala/realtime_updates.rb +26 -21
  23. data/lib/koala/test_users.rb +9 -8
  24. data/lib/koala/version.rb +1 -1
  25. data/lib/koala.rb +7 -9
  26. data/readme.md +83 -109
  27. data/spec/cases/api_spec.rb +176 -69
  28. data/spec/cases/configuration_spec.rb +11 -0
  29. data/spec/cases/error_spec.rb +16 -3
  30. data/spec/cases/graph_api_batch_spec.rb +75 -44
  31. data/spec/cases/graph_api_spec.rb +15 -29
  32. data/spec/cases/graph_collection_spec.rb +47 -34
  33. data/spec/cases/graph_error_checker_spec.rb +31 -2
  34. data/spec/cases/http_service/request_spec.rb +250 -0
  35. data/spec/cases/http_service/response_spec.rb +24 -0
  36. data/spec/cases/http_service_spec.rb +126 -286
  37. data/spec/cases/koala_spec.rb +7 -5
  38. data/spec/cases/oauth_spec.rb +41 -2
  39. data/spec/cases/realtime_updates_spec.rb +51 -13
  40. data/spec/cases/test_users_spec.rb +56 -2
  41. data/spec/cases/uploadable_io_spec.rb +31 -31
  42. data/spec/fixtures/cat.m4v +0 -0
  43. data/spec/fixtures/facebook_data.yml +4 -6
  44. data/spec/fixtures/mock_facebook_responses.yml +41 -78
  45. data/spec/fixtures/vcr_cassettes/app_test_accounts.yml +97 -0
  46. data/spec/integration/graph_collection_spec.rb +8 -5
  47. data/spec/spec_helper.rb +2 -2
  48. data/spec/support/graph_api_shared_examples.rb +152 -337
  49. data/spec/support/koala_test.rb +11 -13
  50. data/spec/support/mock_http_service.rb +11 -14
  51. data/spec/support/uploadable_io_shared_examples.rb +4 -4
  52. metadata +47 -48
  53. data/.autotest +0 -12
  54. data/.travis.yml +0 -17
  55. data/Guardfile +0 -6
  56. data/autotest/discover.rb +0 -1
  57. data/lib/koala/api/rest_api.rb +0 -135
  58. data/lib/koala/http_service/multipart_request.rb +0 -41
  59. data/spec/cases/multipart_request_spec.rb +0 -65
  60. data/spec/support/rest_api_shared_examples.rb +0 -168
@@ -0,0 +1,250 @@
1
+ require "spec_helper"
2
+
3
+ module Koala
4
+ module HTTPService
5
+ RSpec.describe Request do
6
+ let(:path) { "/foo" }
7
+ let(:args) { {"a" => 2, "b" => 3, "access_token" => "a_token"} }
8
+ let(:verb) { "get" }
9
+ let(:options) { {an: :option} }
10
+ let(:request) { Request.new(path: path, args: args, verb: verb, options: options) }
11
+
12
+ shared_context post: true do
13
+ let(:verb) { "post" }
14
+ end
15
+
16
+ shared_context delete: true do
17
+ let(:verb) { "delete" }
18
+ end
19
+
20
+ shared_context put: true do
21
+ let(:verb) { "put" }
22
+ end
23
+
24
+ it "raises an ArgumentError if no verb is supplied" do
25
+ expect { Request.new(path: path) }.to raise_exception(ArgumentError)
26
+ end
27
+
28
+ it "raises an ArgumentError if no path is supplied" do
29
+ expect { Request.new(verb: verb) }.to raise_exception(ArgumentError)
30
+ end
31
+
32
+ describe "#verb" do
33
+ it "returns get for get" do
34
+ expect(Request.new(path: path, verb: "get").verb).to eq("get")
35
+ end
36
+
37
+ it "returns post for post" do
38
+ expect(Request.new(path: path, verb: "post").verb).to eq("post")
39
+ end
40
+
41
+ it "returns post for put" do
42
+ expect(Request.new(path: path, verb: "put").verb).to eq("post")
43
+ end
44
+
45
+ it "returns post for delete" do
46
+ expect(Request.new(path: path, verb: "delete").verb).to eq("post")
47
+ end
48
+ end
49
+
50
+ describe "#path" do
51
+ context "if there's no API version" do
52
+ it "returns the path" do
53
+ expect(request.path).to eq(path)
54
+ end
55
+ end
56
+
57
+ context "if there's an API version" do
58
+ shared_examples_for :including_the_version do
59
+ it "prefixes the version appropriately when the path starts with /" do
60
+ expect(Request.new(path: "/foo", verb: "get", options: options).path).to eq("/#{version}/foo")
61
+ end
62
+
63
+ it "prefixes the version appropriately when the path doesn't start with /" do
64
+ expect(Request.new(path: "foo", verb: "get", options: options).path).to eq("/#{version}/foo")
65
+ end
66
+ end
67
+
68
+ context "set by Koala.config" do
69
+ let(:version) { "v2.7" }
70
+
71
+ before :each do
72
+ Koala.configure do |config|
73
+ config.api_version = version
74
+ end
75
+ end
76
+
77
+ it_should_behave_like :including_the_version
78
+ end
79
+
80
+ context "set in options" do
81
+ let(:version) { "v2.8" }
82
+
83
+ let(:options) { {api_version: version} }
84
+
85
+ it_should_behave_like :including_the_version
86
+ end
87
+
88
+ context "for versions without a ." do
89
+ let(:version) { "v21" }
90
+
91
+ let(:options) { {api_version: version} }
92
+
93
+ it_should_behave_like :including_the_version
94
+ end
95
+ end
96
+ end
97
+
98
+ describe "#post_args" do
99
+ it "returns {} for get" do
100
+ expect(request.post_args).to eq({})
101
+ end
102
+
103
+ it "returns args for post", :post do
104
+ expect(request.post_args).to eq(args)
105
+ end
106
+
107
+ it "returns args + method: delete for delete", :delete do
108
+ expect(request.post_args).to eq(args.merge(method: "delete"))
109
+ end
110
+
111
+ it "returns args + method: put for put", :put do
112
+ expect(request.post_args).to eq(args.merge(method: "put"))
113
+ end
114
+
115
+ it "turns any UploadableIOs to UploadIOs" do
116
+ # technically this is done for all requests, but you don't send GET requests with files
117
+ upload_io = double("UploadIO")
118
+ u = Koala::HTTPService::UploadableIO.new("/path/to/stuff", "img/jpg")
119
+ allow(u).to receive(:to_upload_io).and_return(upload_io)
120
+ request = Request.new(path: path, verb: "post", args: {an_upload: u})
121
+ expect(request.post_args[:an_upload]).to eq(upload_io)
122
+ end
123
+ end
124
+
125
+ describe "#json?" do
126
+ it "returns false if the option for JSON isn't set" do
127
+ expect(request).not_to be_json
128
+ end
129
+
130
+ context "if JSON is set" do
131
+ let(:options) { {format: :json} }
132
+
133
+ it "returns true if it is" do
134
+ expect(request).to be_json
135
+ end
136
+ end
137
+ end
138
+
139
+ describe "#options" do
140
+ it "returns the options provided" do
141
+ expect(request.options).to include(options)
142
+ end
143
+
144
+ it "merges in any global options" do
145
+ global_options = {some: :opts}
146
+ HTTPService.http_options = global_options
147
+ results = request.options
148
+ HTTPService.http_options = {}
149
+ expect(results).to include(global_options)
150
+ end
151
+
152
+ it "overwrites any global options with equivalent ones" do
153
+ options = {foo: :bar}
154
+ HTTPService.http_options = {foo: 2}
155
+ results = Request.new(path: path, args: args, verb: verb, options: options).options
156
+ HTTPService.http_options = {}
157
+ expect(results).to include(options)
158
+ end
159
+
160
+ describe "get parameters" do
161
+ it "includes the parameters for a get " do
162
+ expect(request.options[:params]).to eq(args)
163
+ end
164
+
165
+ it "returns {} for post", :post do
166
+ expect(request.options[:params]).to eq({})
167
+ end
168
+
169
+ it "returns {} for delete", :delete do
170
+ expect(request.options[:params]).to eq({})
171
+ end
172
+
173
+ it "returns {} for put", :put do
174
+ expect(request.options[:params]).to eq({})
175
+ end
176
+ end
177
+
178
+ describe "ssl options" do
179
+ it "includes the default SSL options even if there's no access token" do
180
+ request_options = Request.new(path: path, args: args.delete_if {|k, _v| k == "access_token"}, verb: verb, options: options).options
181
+ expect(request_options).to include(use_ssl: true, ssl: {verify: true})
182
+ end
183
+
184
+ context "if there is an access_token" do
185
+ it "includes the default SSL options" do
186
+ expect(request.options).to include(use_ssl: true, ssl: {verify: true})
187
+ end
188
+
189
+ it "incorporates any provided SSL options" do
190
+ new_ssl_options = {an: :option2}
191
+ request_options = Request.new(path: path, args: args, verb: verb, options: options.merge(ssl: new_ssl_options)).options
192
+ expect(request_options[:ssl]).to include(new_ssl_options)
193
+ end
194
+
195
+ it "overrides default SSL options with what's provided" do
196
+ new_ssl_options = {use_ssl: false, ssl:{verify: :dunno}}
197
+ request_options = Request.new(path: path, args: args, verb: verb, options: options.merge(new_ssl_options)).options
198
+ expect(request_options).to include(new_ssl_options)
199
+ end
200
+ end
201
+ end
202
+
203
+ describe "server" do
204
+ describe "with no options" do
205
+ it "returns the graph server by default" do
206
+ expect(request.server).to eq("https://graph.facebook.com")
207
+ end
208
+
209
+ it "uses another base server if specified" do
210
+ Koala.config.graph_server = "foo"
211
+ expect(request.server).to eq("https://foo")
212
+ end
213
+
214
+ context "if there is no access token" do
215
+ let(:args) { {"a" => "b"} }
216
+
217
+ it "uses https" do
218
+ expect(request.server).to eq("https://graph.facebook.com")
219
+ end
220
+ end
221
+
222
+ context "if options[:use_ssl] is false" do
223
+ let(:options) { {use_ssl: false} }
224
+
225
+ it "uses http" do
226
+ expect(request.server).to eq("http://graph.facebook.com")
227
+ end
228
+ end
229
+
230
+ context "if options[:beta]" do
231
+ let(:options) { {beta: true} }
232
+
233
+ it "returns https if options[:beta]" do
234
+ expect(request.server).to eq("https://graph.beta.facebook.com")
235
+ end
236
+ end
237
+
238
+ context "if options[:video]" do
239
+ let(:options) { {video: true} }
240
+
241
+ it "returns https if options[:video]" do
242
+ expect(request.server).to eq("https://graph-video.facebook.com")
243
+ end
244
+ end
245
+ end
246
+ end
247
+ end
248
+ end
249
+ end
250
+ end
@@ -0,0 +1,24 @@
1
+ require "spec_helper"
2
+
3
+ module Koala::HTTPService
4
+ RSpec.describe Response do
5
+ describe "#data" do
6
+ it "returns nothing if the body is empty" do
7
+ expect(Response.new(200, "", {}).data).to be_nil
8
+ end
9
+
10
+ it "parses the JSON if there's a body" do
11
+ result = {"foo" => "bar"}
12
+ expect(Response.new(200, result.to_json, {}).data).to eq(result)
13
+ end
14
+
15
+ it "allows raw values" do
16
+ expect(Response.new(200, "true", {}).data).to be true
17
+ end
18
+
19
+ it "raises a ParserError if it's invalid JSON" do
20
+ expect { Response.new(200, "{", {}).data }.to raise_exception(JSON::ParserError)
21
+ end
22
+ end
23
+ end
24
+ end