koala 3.4.0 → 3.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dd1ef11b53dbfd2dc45d096a2b3f26d86aab74927758638b95670be82d5a81b3
4
- data.tar.gz: 4f8694539f46568a268740064fa16f5f9084198f90ccdb2bf66b532d0fd280ae
3
+ metadata.gz: 27591b64021ee915c13d9bcea2763739c2c86ce04d961dd5b540d964515a9905
4
+ data.tar.gz: 4a8c184674f681b193a3686b4eeb0023da0c00765d60be9694af9098014dacfd
5
5
  SHA512:
6
- metadata.gz: aa13fcdb702dacaf5ea61d905f1d4d09e067b8181c9bbe9f1e210d80718eb8015953b770fc79332658bf9a0c5b11e0923a31c85049fde4a5dc566367f5ed63a1
7
- data.tar.gz: cda4d98559e43ce1cb2cfd4c308431020c32443282cf2161230f6cae81bfc7608494181f55cc15e8dfc4386b248a81ec7d2306125487a41b6d4c7c6f4bc84c09
6
+ metadata.gz: 78e4a3d670c3524211d438b8b67713f3f27cc0638aee6660b544b8b3b83a99397546a96ae826c6d613a1b6799184894c816b95008dc6083689f866357ae7ae13
7
+ data.tar.gz: 7a135a41f63d84e7e4f6c68632574eb19140aaf32ea9d9220bb2aaa4c79c50097c7ee1c957f7e9740d6b2c1141b5d24ebffdb47edd510c28bd7590f3cc09bf4f
@@ -10,11 +10,11 @@ jobs:
10
10
  strategy:
11
11
  fail-fast: false
12
12
  matrix:
13
- ruby: [2.7, "3.0", 3.1, head]
13
+ ruby: [2.7, "3.0", 3.1, 3.2, 3.3, head]
14
14
 
15
15
  steps:
16
16
  - name: Checkout repository
17
- uses: actions/checkout@v2
17
+ uses: actions/checkout@v4
18
18
 
19
19
  - name: Set up Ruby
20
20
  uses: ruby/setup-ruby@v1
@@ -25,7 +25,7 @@ jobs:
25
25
  run: bundle install --jobs 4 --retry 3
26
26
 
27
27
  - name: Specs & Coverage
28
- uses: paambaati/codeclimate-action@v3.0.0
28
+ uses: paambaati/codeclimate-action@v6
29
29
  env:
30
30
  CC_TEST_REPORTER_ID: 7af99d9225b4c14640f9ec3cb2e24d2f7103ac49417b0bd989188fb6c25f2909
31
31
  with:
data/changelog.md CHANGED
@@ -15,6 +15,30 @@ Testing improvements:
15
15
 
16
16
  Others:
17
17
 
18
+ v3.6.0 (2024-06-27)
19
+ ==========
20
+
21
+ Updated features:
22
+
23
+ * Add fbtrace_id, x-fb-rev, x-fb-debug to error messages and error class ([#668](https://github.com/arsduo/koala/pull/686))
24
+ * Handles the invalid JSON response from Facebook when the request's http_options[:http_component] is set to ':response' ([#689](https://github.com/arsduo/koala/pull/689))
25
+
26
+ Internal improvements:
27
+
28
+ * Require base64 for ruby 3.4 support ([#688](https://github.com/arsduo/koala/pull/688))
29
+
30
+ Testing improvements:
31
+
32
+ * Fix CI for ruby 3.4 ([#688](https://github.com/arsduo/koala/pull/688))
33
+ * Add latest rubies to CI ([#687](https://github.com/arsduo/koala/pull/687))
34
+ * Bump GHA action plugins to avoid deprecation warnings ([#689](https://github.com/arsduo/koala/pull/689))
35
+
36
+ v3.5.0 (2023-08-23)
37
+ ======
38
+
39
+ Internal improvements:
40
+ * Raise ClientError instead of NoMethodError when body is 'null' ([#673](https://github.com/arsduo/koala/issues/673))
41
+
18
42
  v3.4.0 (2023-01-05)
19
43
  ======
20
44
 
data/koala.gemspec CHANGED
@@ -28,4 +28,5 @@ Gem::Specification.new do |gem|
28
28
  gem.add_runtime_dependency("addressable")
29
29
  gem.add_runtime_dependency("json", ">= 1.8")
30
30
  gem.add_runtime_dependency("rexml")
31
+ gem.add_runtime_dependency("base64")
31
32
  end
@@ -53,7 +53,16 @@ module Koala
53
53
  end
54
54
 
55
55
  original_api.graph_call("/", args, "post", http_options) do |response|
56
- raise bad_response if response.nil?
56
+ raise bad_response('Facebook returned an empty body') if response.nil?
57
+
58
+ # when http_component is set we receive Koala::Http_service response object
59
+ # from graph_call so this needs to be parsed
60
+ # as generate_results method handles only JSON response
61
+ if http_options[:http_component] && http_options[:http_component] == :response
62
+ response = json_body(response.body)
63
+
64
+ raise bad_response('Facebook returned an invalid body') unless response.is_a?(Array)
65
+ end
57
66
 
58
67
  batch_results += generate_results(response, batch)
59
68
  end
@@ -81,9 +90,9 @@ module Koala
81
90
  end
82
91
  end
83
92
 
84
- def bad_response
93
+ def bad_response(message)
85
94
  # Facebook sometimes reportedly returns an empty body at times
86
- BadFacebookResponse.new(200, "", "Facebook returned an empty body")
95
+ BadFacebookResponse.new(200, '', message)
87
96
  end
88
97
 
89
98
  def result_from_response(response, options)
@@ -123,14 +132,17 @@ module Koala
123
132
  JSON.dump calls
124
133
  end
125
134
 
126
- def json_body(response)
127
- # quirks_mode is needed because Facebook sometimes returns a raw true or false value --
128
- # in Ruby 2.4 we can drop that.
129
- JSON.parse(response.fetch("body"), quirks_mode: true)
135
+ def json_body(body)
136
+ return if body.nil?
137
+
138
+ JSON.parse(body)
139
+ rescue JSON::ParserError => e
140
+ Koala::Utils.logger.error("#{e.class}: #{e.message} while parsing #{body}")
141
+ nil
130
142
  end
131
143
 
132
144
  def desired_component(component:, response:, headers:)
133
- result = Koala::HTTPService::Response.new(response['status'], response['body'], headers)
145
+ result = Koala::HTTPService::Response.new(response['code'], response['body'], headers)
134
146
 
135
147
  # Get the HTTP component they want
136
148
  case component
@@ -138,6 +150,7 @@ module Koala
138
150
  # facebook returns the headers as an array of k/v pairs, but we want a regular hash
139
151
  when :headers then headers
140
152
  # (see note in regular api method about JSON parsing)
153
+ when :response then result
141
154
  else GraphCollection.evaluate(result, original_api)
142
155
  end
143
156
  end
@@ -61,7 +61,8 @@ module Koala
61
61
  # Normally, we start with the response body. If it isn't valid JSON, we start with an empty
62
62
  # hash and fill it with error data.
63
63
  @response_hash ||= begin
64
- JSON.parse(body)
64
+ parsed_body = JSON.parse(body)
65
+ parsed_body.is_a?(Hash) ? parsed_body : {}
65
66
  rescue JSON::ParserError
66
67
  {}
67
68
  end
data/lib/koala/errors.rb CHANGED
@@ -22,6 +22,7 @@ module Koala
22
22
  :fb_error_user_msg,
23
23
  :fb_error_user_title,
24
24
  :fb_error_trace_id,
25
+ :fb_error_debug_trace_id,
25
26
  :fb_error_debug,
26
27
  :fb_error_rev,
27
28
  :fb_buc_usage,
@@ -65,8 +66,9 @@ module Koala
65
66
  self.fb_error_message = error_info["message"]
66
67
  self.fb_error_user_msg = error_info["error_user_msg"]
67
68
  self.fb_error_user_title = error_info["error_user_title"]
69
+ self.fb_error_trace_id = error_info["fbtrace_id"]
68
70
 
69
- self.fb_error_trace_id = error_info["x-fb-trace-id"]
71
+ self.fb_error_debug_trace_id = error_info["x-fb-trace-id"]
70
72
  self.fb_error_debug = error_info["x-fb-debug"]
71
73
  self.fb_error_rev = error_info["x-fb-rev"]
72
74
  self.fb_buc_usage = json_parse_for(error_info, "x-business-use-case-usage")
@@ -74,7 +76,7 @@ module Koala
74
76
  self.fb_app_usage = json_parse_for(error_info, "x-app-usage")
75
77
 
76
78
  error_array = []
77
- %w(type code error_subcode message error_user_title error_user_msg x-fb-trace-id).each do |key|
79
+ %w(type code error_subcode message error_user_title error_user_msg fbtrace_id x-fb-trace-id x-fb-debug x-fb-rev).each do |key|
78
80
  error_array << "#{key}: #{error_info[key]}" if error_info[key]
79
81
  end
80
82
 
data/lib/koala/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Koala
2
- VERSION = "3.4.0"
2
+ VERSION = "3.6.0"
3
3
  end
@@ -34,7 +34,8 @@ describe Koala::Facebook::APIError do
34
34
  'error_subcode' => 'subcode',
35
35
  'error_user_msg' => 'error user message',
36
36
  'error_user_title' => 'error user title',
37
- 'x-fb-trace-id' => 'fb trace id',
37
+ 'fbtrace_id' => 'fb trace id',
38
+ 'x-fb-trace-id' => 'x-fb trace id',
38
39
  'x-fb-debug' => 'fb debug token',
39
40
  'x-fb-rev' => 'fb revision',
40
41
  'x-business-use-case-usage' => BUC_USAGE_JSON,
@@ -52,6 +53,7 @@ describe Koala::Facebook::APIError do
52
53
  :fb_error_user_msg => 'error user message',
53
54
  :fb_error_user_title => 'error user title',
54
55
  :fb_error_trace_id => 'fb trace id',
56
+ :fb_error_debug_trace_id => 'x-fb trace id',
55
57
  :fb_error_debug => 'fb debug token',
56
58
  :fb_error_rev => 'fb revision',
57
59
  :fb_buc_usage => JSON.parse(BUC_USAGE_JSON),
@@ -64,7 +66,7 @@ describe Koala::Facebook::APIError do
64
66
  end
65
67
 
66
68
  it "sets the error message appropriately" do
67
- expect(error.message).to eq("type: type, code: 1, error_subcode: subcode, message: message, error_user_title: error user title, error_user_msg: error user message, x-fb-trace-id: fb trace id [HTTP 400]")
69
+ expect(error.message).to eq("type: type, code: 1, error_subcode: subcode, message: message, error_user_title: error user title, error_user_msg: error user message, fbtrace_id: fb trace id, x-fb-trace-id: x-fb trace id, x-fb-debug: fb debug token, x-fb-rev: fb revision [HTTP 400]")
68
70
  end
69
71
  end
70
72
 
@@ -383,28 +383,85 @@ describe "Koala::Facebook::GraphAPI in batch mode" do
383
383
  end
384
384
  end
385
385
 
386
- describe "processing the request" do
387
- it "returns the result headers as a hash if http_component is headers" do
386
+ describe 'processing the request' do
387
+ let(:response_status) { 203 }
388
+ let(:response_body) { '{\"id\":\"1234\"}'.gsub('\\', '') }
389
+ let(:response_headers) { { 'Content-Type' => 'text/javascript; charset=UTF-8' } }
390
+
391
+ it 'returns the result headers as a hash if http_component is headers' do
388
392
  allow(Koala).to receive(:make_request).and_return(Koala::HTTPService::Response.new(200, '[{"code":203,"headers":[{"name":"Content-Type","value":"text/javascript; charset=UTF-8"}],"body":"{\"id\":\"1234\"}"}]', {}))
389
393
  result = @api.batch do |batch_api|
390
394
  batch_api.get_object(KoalaTest.user1, {}, :http_component => :headers)
391
395
  end
392
- expect(result[0]).to eq({"Content-Type" => "text/javascript; charset=UTF-8"})
396
+ expect(response_headers).to eq(result[0])
397
+ end
398
+
399
+ it 'returns the complete response if http_component is response' do
400
+ allow(Koala).to receive(:make_request).and_return(Koala::HTTPService::Response.new(200, '[{"code":203,"headers":[{"name":"Content-Type","value":"text/javascript; charset=UTF-8"}],"body":"{\"id\":\"1234\"}"}]', {}))
401
+ result = @api.batch do |batch_api|
402
+ batch_api.get_object(KoalaTest.user1, {}, :http_component => :response)
403
+ end
404
+
405
+ expect(response_status).to eq(result[0].status)
406
+ expect(response_body).to eq(result[0].body)
407
+ expect(response_headers).to eq(result[0].headers)
393
408
  end
394
409
 
395
- describe "if it errors" do
396
- it "raises an APIError if the response is not 200" do
397
- allow(Koala).to receive(:make_request).and_return(Koala::HTTPService::Response.new(500, "[]", {}))
410
+ describe 'if it errors' do
411
+ it 'raises an APIError if the response is not 200' do
412
+ allow(Koala).to receive(:make_request).and_return(Koala::HTTPService::Response.new(500, '[]', {}))
398
413
  expect {
399
- Koala::Facebook::API.new("foo").batch {|batch_api| batch_api.get_object('me') }
414
+ Koala::Facebook::API.new('foo').batch { |batch_api| batch_api.get_object('me') }
400
415
  }.to raise_exception(Koala::Facebook::APIError)
401
416
  end
402
417
 
403
- it "raises a BadFacebookResponse if the body is empty" do
404
- allow(Koala).to receive(:make_request).and_return(Koala::HTTPService::Response.new(200, "", {}))
418
+ it 'raises a BadFacebookResponse if the body is empty' do
419
+ allow(Koala).to receive(:make_request).and_return(Koala::HTTPService::Response.new(200, '', {}))
405
420
  expect {
406
- Koala::Facebook::API.new("foo").batch {|batch_api| batch_api.get_object('me') }
407
- }.to raise_exception(Koala::Facebook::BadFacebookResponse)
421
+ Koala::Facebook::API.new('foo').batch { |batch_api| batch_api.get_object('me') }
422
+ }.to raise_exception(Koala::Facebook::BadFacebookResponse, /Facebook returned an empty body \[HTTP 200\]/)
423
+ end
424
+
425
+ describe 'handle invalid body errors' do
426
+ describe 'with http_component set to :response' do
427
+ it 'raises a BadFacebookResponse if the body is non-empty, non-array' do
428
+ allow(Koala).to receive(:make_request).and_return(Koala::HTTPService::Response.new(200, '200', {}))
429
+ expect {
430
+ Koala::Facebook::API.new('foo').batch(http_component: :response) do |batch_api|
431
+ batch_api.get_object('me')
432
+ end
433
+ }.to raise_exception(Koala::Facebook::BadFacebookResponse, /Facebook returned an invalid body \[HTTP 200\]/)
434
+ end
435
+
436
+ it 'raises a BadFacebookResponse if the body is invalid JSON' do
437
+ allow(Koala).to receive(:make_request).and_return(Koala::HTTPService::Response.new(200, '{"\"id\":\1234\"}"}', {}))
438
+ expect {
439
+ Koala::Facebook::API.new('foo').batch(http_component: :response) do |batch_api|
440
+ batch_api.get_object('me')
441
+ end
442
+ }.to raise_exception(Koala::Facebook::BadFacebookResponse, /Facebook returned an invalid body \[HTTP 200\]/)
443
+ end
444
+ end
445
+
446
+ %i[headers status].each do |component|
447
+ describe "with http_component set to #{component}" do
448
+ it 'should not raise a BadFacebookResponse if the body is non-empty, non-array' do
449
+ allow(Koala).to receive(:make_request).and_return(Koala::HTTPService::Response.new(200, '200', {}))
450
+ expect {
451
+ Koala::Facebook::API.new('foo').batch(http_component: component) { |batch_api| batch_api.get_object('me') }
452
+ }.not_to raise_exception(Koala::Facebook::BadFacebookResponse, /Facebook returned an invalid body \[HTTP 200\]/)
453
+ end
454
+
455
+ it 'should not raise a BadFacebookResponse if the body is invalid JSON' do
456
+ allow(Koala).to receive(:make_request).and_return(Koala::HTTPService::Response.new(200, '{"\"id\":\1234\"}"}', {}))
457
+ expect {
458
+ Koala::Facebook::API.new('foo').batch(http_component: component) do |batch_api|
459
+ batch_api.get_object('me')
460
+ end
461
+ }.not_to raise_exception(Koala::Facebook::BadFacebookResponse, /Facebook returned an invalid body \[HTTP 200\]/)
462
+ end
463
+ end
464
+ end
408
465
  end
409
466
 
410
467
  context "with error info" do
@@ -63,6 +63,12 @@ module Koala
63
63
  expect(error.response_body).to eq(body)
64
64
  end
65
65
 
66
+ it "returns a ClientError if the body is null" do
67
+ body.replace("null")
68
+ expect(error).to be_a(ClientError)
69
+ expect(error.response_body).to eq(body)
70
+ end
71
+
66
72
  it "adds error data from the body" do
67
73
  error_data = {
68
74
  "type" => "FB error type",
@@ -70,10 +76,12 @@ module Koala
70
76
  "error_subcode" => "FB error subcode",
71
77
  "message" => "An error occurred!",
72
78
  "error_user_msg" => "A user msg",
73
- "error_user_title" => "usr title"
79
+ "error_user_title" => "usr title",
80
+ "fbtrace_id" => "fbtrace_id"
74
81
  }
75
82
  body.replace({"error" => error_data}.to_json)
76
83
 
84
+ expect(error.fb_error_trace_id).to eq(error_data["fbtrace_id"])
77
85
  expect(error.fb_error_type).to eq(error_data["type"])
78
86
  expect(error.fb_error_code).to eq(error_data["code"])
79
87
  expect(error.fb_error_subcode).to eq(error_data["error_subcode"])
@@ -91,7 +99,7 @@ module Koala
91
99
  "x-ad-account-usage" => { 'c' => 3, 'd' => 4 }.to_json,
92
100
  "x-app-usage" => { 'e' => 5, 'f' => 6 }.to_json
93
101
  )
94
- expect(error.fb_error_trace_id).to eq(headers["x-fb-trace-id"])
102
+ expect(error.fb_error_debug_trace_id).to eq(headers["x-fb-trace-id"])
95
103
  expect(error.fb_error_debug).to eq(headers["x-fb-debug"])
96
104
  expect(error.fb_error_rev).to eq(headers["x-fb-rev"])
97
105
  expect(error.fb_buc_usage).to eq({ 'a' => 1, 'b' => 2 })
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: koala
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.4.0
4
+ version: 3.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Koppel
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-01-05 00:00:00.000000000 Z
11
+ date: 2024-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: base64
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  description: Koala is a lightweight, flexible Ruby SDK for Facebook. It allows read/write
84
98
  access to the social graph via the Graph and REST APIs, as well as support for realtime
85
99
  updates and OAuth and Facebook Connect authentication. Koala is fully tested and
@@ -158,7 +172,7 @@ homepage: http://github.com/arsduo/koala
158
172
  licenses:
159
173
  - MIT
160
174
  metadata: {}
161
- post_install_message:
175
+ post_install_message:
162
176
  rdoc_options:
163
177
  - "--line-numbers"
164
178
  - "--inline-source"
@@ -177,8 +191,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
177
191
  - !ruby/object:Gem::Version
178
192
  version: '0'
179
193
  requirements: []
180
- rubygems_version: 3.2.32
181
- signing_key:
194
+ rubygems_version: 3.3.26
195
+ signing_key:
182
196
  specification_version: 4
183
197
  summary: A lightweight, flexible library for Facebook with support for the Graph API,
184
198
  the REST API, realtime updates, and OAuth authentication.