quiz_api_client 4.24.0 → 4.26.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e7e14132240ede40616dabc2feac9400fe84262ba29cf2d2aacbdedddcded259
4
- data.tar.gz: 745e10dfc3a5e4917ad9e0000a66022c31cd16fc4f8dfb7c386eb9a09846267d
3
+ metadata.gz: 182251d8d198f02d1428d74caf6b3216ca7f5a919a6b2eeec7ac2c4c932df208
4
+ data.tar.gz: e8ec9ef5a7b469d01a246417705ffb4cb80bbe82b69786ccd6635a789bc60efe
5
5
  SHA512:
6
- metadata.gz: 2d62d169bf07dad6adfcf0fbc43d850ebaf705cccd9db6bc848d74268501769b98a983fed9028e12d0dbffb67d9e0bcfa73d1f721c5a4f83220c4ee1d1323589
7
- data.tar.gz: 2a4b338ae4b34b16fcfbe36b2e99ed13fec4b8ea9e08e636b830dec0215e9d01300973ded39e25907dcc7457c1961b33b265eb60f2b8adbb55aa000d32156791
6
+ metadata.gz: 1c5ef1655f96677079f022b6f014a244795702708e3ec48d88b16f7982c72a2d35fe3e755d6e509f0dab7e2e8e50ae71145d93dc5384ef304b5a69f48d4561e6
7
+ data.tar.gz: f700f9a028f4ecbcc7fd34866e6690093a4af5b084cfb999f9873269c56f3d970c76ed380a4d53f5895a46517f8a58a623ffdcc693f2d89784f97d0a314d18b1
data/README.md CHANGED
@@ -187,3 +187,37 @@ Then, run `bundle exec rake spec` to run the tests.
187
187
  You can also run `bundle exec rake console` for an interactive prompt that will allow you to experiment.
188
188
 
189
189
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
190
+
191
+ ## Development with Quiz LTI
192
+
193
+ Copy this repo inside the Quiz LTI docker container:
194
+
195
+ ```shell
196
+ docker cp . quiz-lti-web:/quiz_api_client_ruby
197
+ ```
198
+
199
+ Tell Quiz LTI to use the gem, find the line containing `gem 'quiz_api_client'` in `app.rb`, update it to:
200
+
201
+ ```shell
202
+ gem 'quiz_api_client', path: '/quiz_api_client_ruby'
203
+ ```
204
+
205
+ Run bundle install the container:
206
+
207
+ ```shell
208
+ docker exec quiz-lti-web bundle install
209
+ ```
210
+
211
+ Restart the container:
212
+
213
+ ```shell
214
+ docker restart quiz-lti-web
215
+ ```
216
+
217
+ Your changes should be reflected in the container!
218
+
219
+ If you update the code afterwards you'll need to copy and restart the container:
220
+
221
+ ```shell
222
+ docker cp . quiz-lti-web:gems/quiz_api_client_ruby && docker restart quiz-lti-web
223
+ ```
@@ -2,7 +2,8 @@ module QuizApiClient
2
2
  class Config
3
3
  DEFAULT_ALLOWABLE_RESPONSE_CODES = [401, 422].freeze
4
4
  DEFAULT_PROTOCOL = 'https'.freeze
5
- ERROR_HANDLERS = %i[sentry_raven].freeze
5
+ DEFAULT_USER_AGENT = "QuizApiClient/#{QuizApiClient::VERSION} (Ruby)".freeze
6
+ ERROR_HANDLERS = %i[sentry_raven sentry_rails].freeze
6
7
  METRICS_HANDLERS = %i[inststatsd].freeze
7
8
 
8
9
  class InvalidErrorHandler < StandardError; end
@@ -10,7 +11,7 @@ module QuizApiClient
10
11
  class InvalidMetricsNamespace < StandardError; end
11
12
 
12
13
  attr_reader :error_handler, :metrics_handler, :metrics_namespace
13
- attr_writer :protocol, :allowable_response_codes
14
+ attr_writer :protocol, :allowable_response_codes, :user_agent
14
15
  attr_accessor :consumer_key, :consumer_request_id, :host, :shared_secret
15
16
 
16
17
  def initialize
@@ -39,6 +40,10 @@ module QuizApiClient
39
40
  @allowable_response_codes || DEFAULT_ALLOWABLE_RESPONSE_CODES
40
41
  end
41
42
 
43
+ def user_agent
44
+ @user_agent || DEFAULT_USER_AGENT
45
+ end
46
+
42
47
  private
43
48
 
44
49
  def validate_error_handler!(handler)
@@ -123,7 +123,8 @@ module QuizApiClient
123
123
  'Authorization' => jwt,
124
124
  'AuthType' => 'Signature',
125
125
  'Accept' => 'application/json',
126
- 'Content-Type' => 'application/json'
126
+ 'Content-Type' => 'application/json',
127
+ 'User-Agent' => config.user_agent
127
128
  }
128
129
  }
129
130
 
@@ -48,6 +48,10 @@ module QuizApiClient
48
48
  when :sentry_raven
49
49
  require 'sentry-raven'
50
50
  Raven.extra_context(context)
51
+ when :sentry_rails
52
+ require 'sentry-ruby'
53
+ require 'sentry-rails'
54
+ Sentry.set_extras(context)
51
55
  end
52
56
  end
53
57
 
@@ -1,3 +1,3 @@
1
1
  module QuizApiClient
2
- VERSION = '4.24.0'.freeze
2
+ VERSION = '4.26.0'.freeze
3
3
  end
data/spec/config_spec.rb CHANGED
@@ -30,6 +30,18 @@ describe QuizApiClient::Config do
30
30
  end
31
31
  end
32
32
 
33
+ describe '#user_agent' do
34
+ it 'returns the default when not set' do
35
+ config = described_class.new
36
+ expect(config.user_agent).to eq QuizApiClient::Config::DEFAULT_USER_AGENT
37
+ end
38
+
39
+ it 'returns the configured value when set' do
40
+ config = described_class.new { |c| c.user_agent = 'canvas-lms/2026.05.0' }
41
+ expect(config.user_agent).to eq 'canvas-lms/2026.05.0'
42
+ end
43
+ end
44
+
33
45
  describe '#setup_metrics' do
34
46
  let(:valid_handler) { :inststatsd }
35
47
  let(:invalid_handler) { :invalid_handler }
@@ -188,6 +188,7 @@ describe QuizApiClient::HttpClient do
188
188
  'AuthType' => 'Signature',
189
189
  'Accept' => 'application/json',
190
190
  'Content-Type' => 'application/json',
191
+ 'User-Agent' => QuizApiClient::Config::DEFAULT_USER_AGENT,
191
192
  'X-Consumer-Request-Id' => 'hi'
192
193
  )
193
194
  end
@@ -360,6 +361,162 @@ describe QuizApiClient::HttpClient do
360
361
  expect { client.get('/') }.to raise_error(QuizApiClient::HttpClient::RequestFailed)
361
362
  end
362
363
 
364
+ it 'handles an Net::ReadTimeout error' do
365
+ stub_request(:get, url).to_raise(Net::ReadTimeout)
366
+ expect_metrics_calls(client.config, :get, 0)
367
+ expect_raise_error_call(client.config, :get, url_for_path(path), nil, kind_of(Net::ReadTimeout))
368
+ expect { client.get('/') }.to raise_error(QuizApiClient::HttpClient::RequestFailed)
369
+ end
370
+ end
371
+ context 'with :sentry_rails error handler' do
372
+ let(:error_handler) { :sentry_rails }
373
+ let(:path) { '/' }
374
+ let(:url) { "http://api.quiz.docker#{path}" }
375
+ let(:error_context) do
376
+ {
377
+ quiz_api_client: {
378
+ request: {
379
+ method: :get,
380
+ url: url
381
+ }
382
+ }
383
+ }
384
+ end
385
+
386
+ before do
387
+ client.config.error_handler = error_handler
388
+ end
389
+
390
+ context 'with non-success responses' do
391
+ let(:body) { '[2]' }
392
+ let(:code) { 404 }
393
+ let(:path) { '/api/quizzes' }
394
+ let(:context_url) { url }
395
+ let(:method) { :get }
396
+ let(:error_context) do
397
+ {
398
+ quiz_api_client: {
399
+ request: {
400
+ method: method,
401
+ url: context_url
402
+ },
403
+ response: {
404
+ body: body,
405
+ code: code
406
+ }
407
+ }
408
+ }
409
+ end
410
+ let(:mock_response) do
411
+ instance_double(
412
+ 'HTTParty::Response',
413
+ success?: false,
414
+ body: body,
415
+ code: code
416
+ )
417
+ end
418
+ let(:success_response) do
419
+ # rubocop:disable Metrics/LineLength
420
+ instance_double(
421
+ 'HTTParty::Response',
422
+ body: body,
423
+ code: 200,
424
+ parsed_response: [1],
425
+ headers: {
426
+ 'link' => '<http://api.quiz.docker/api/quizzes?page=2>; rel="last", <http://api.quiz.docker/api/quizzes?page=2>; rel="next"',
427
+ 'content-type' => ['application/json']
428
+ }
429
+ )
430
+ # rubocop:enable Metrics/LineLength
431
+ end
432
+
433
+ context ':get request' do
434
+ it 'raises error' do
435
+ expect(client).to receive(:successful_response?).with(mock_response).and_return(false)
436
+ expect(client.class).to receive(:get).and_return(mock_response)
437
+ expect_metrics_calls(client.config, method, code)
438
+ expect_raise_error_call(client.config, method, url_for_path(path), mock_response, nil)
439
+ expect { client.get(path, all: false, query: { sort: 'alpha' }) }
440
+ .to raise_error(QuizApiClient::HttpClient::RequestFailed)
441
+ end
442
+ end
443
+
444
+ context ':post request' do
445
+ let(:method) { :post }
446
+
447
+ it 'raises error' do
448
+ expect(client).to receive(:successful_response?).with(mock_response).and_return(false)
449
+ expect(client.class).to receive(:post).and_return(mock_response)
450
+ expect_metrics_calls(client.config, method, code)
451
+ expect_raise_error_call(client.config, method, url_for_path(path), mock_response, nil)
452
+ expect { client.post(path) }.to raise_error(QuizApiClient::HttpClient::RequestFailed)
453
+ end
454
+ end
455
+
456
+ context ':put request' do
457
+ let(:method) { :put }
458
+
459
+ it 'raises error' do
460
+ expect(client).to receive(:successful_response?).with(mock_response).and_return(false)
461
+ expect(client.class).to receive(:put).and_return(mock_response)
462
+ expect_metrics_calls(client.config, method, code)
463
+ expect_raise_error_call(client.config, method, url_for_path(path), mock_response, nil)
464
+ expect { client.put(path) }.to raise_error(QuizApiClient::HttpClient::RequestFailed)
465
+ end
466
+ end
467
+
468
+ context ':patch request' do
469
+ let(:method) { :patch }
470
+
471
+ it 'raises error' do
472
+ expect(client).to receive(:successful_response?).with(mock_response).and_return(false)
473
+ expect(client.class).to receive(:patch).and_return(mock_response)
474
+ expect_metrics_calls(client.config, method, code)
475
+ expect_raise_error_call(client.config, method, url_for_path(path), mock_response, nil)
476
+ expect { client.patch(path) }.to raise_error(QuizApiClient::HttpClient::RequestFailed)
477
+ end
478
+ end
479
+
480
+ context ':delete request' do
481
+ let(:method) { :delete }
482
+
483
+ it 'raises error' do
484
+ expect(client).to receive(:successful_response?).with(mock_response).and_return(false)
485
+ expect(client.class).to receive(:delete).and_return(mock_response)
486
+ expect_metrics_calls(client.config, method, code)
487
+ expect_raise_error_call(client.config, method, url_for_path(path), mock_response, nil)
488
+ expect { client.delete(path) }.to raise_error(QuizApiClient::HttpClient::RequestFailed)
489
+ end
490
+ end
491
+
492
+ context 'with pagination' do
493
+ let(:context_url) { "#{url}?page=2" }
494
+
495
+ it 'raises error if one of the linked pages does not return 200 response' do
496
+ stub_quiz_api path, headers: { link: link_header(path, 1, 2) }
497
+ stub_quiz_api path, item: 2, query: { page: 2 }, headers: { link: link_header(path, 2, 2) }, status: code
498
+ allow(HTTParty::Response).to receive(:new).and_return(success_response, mock_response)
499
+ expect(client).to receive(:successful_response?).and_return(true, false).twice
500
+ expect_raise_error_call(client.config, method, context_url, mock_response, nil)
501
+ expect { client.get('/api/quizzes', all: true) }.to raise_error(QuizApiClient::HttpClient::RequestFailed)
502
+ end
503
+ end
504
+ end
505
+
506
+ it 'handles an HTTParty::Error error' do
507
+ stub_request(:get, url).to_raise(HTTParty::Error)
508
+ expect_metrics_calls(client.config, :get, 0)
509
+ expect_raise_error_call(client.config, :get, url_for_path(path), nil, kind_of(HTTParty::Error))
510
+ expect { client.get('/') }.to raise_error(QuizApiClient::HttpClient::RequestFailed)
511
+ end
512
+
513
+ it 'handles an Errno::ECONNREFUSED error' do
514
+ stub_request(:get, url).to_raise(Errno::ECONNREFUSED)
515
+ expect_metrics_calls(client.config, :get, 0)
516
+ expect_raise_error_call(client.config, :get, url_for_path(path), nil, kind_of(Errno::ECONNREFUSED))
517
+ expect { client.get('/') }.to raise_error(QuizApiClient::HttpClient::RequestFailed)
518
+ end
519
+
363
520
  it 'handles an Net::ReadTimeout error' do
364
521
  stub_request(:get, url).to_raise(Net::ReadTimeout)
365
522
  expect_metrics_calls(client.config, :get, 0)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quiz_api_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.24.0
4
+ version: 4.26.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Slaughter
@@ -15,7 +15,7 @@ authors:
15
15
  autorequire:
16
16
  bindir: exe
17
17
  cert_chain: []
18
- date: 2025-12-02 00:00:00.000000000 Z
18
+ date: 2026-05-08 00:00:00.000000000 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: httparty
@@ -115,6 +115,20 @@ dependencies:
115
115
  - - "~>"
116
116
  - !ruby/object:Gem::Version
117
117
  version: 0.74.0
118
+ - !ruby/object:Gem::Dependency
119
+ name: sentry-rails
120
+ requirement: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '6.5'
125
+ type: :development
126
+ prerelease: false
127
+ version_requirements: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '6.5'
118
132
  - !ruby/object:Gem::Dependency
119
133
  name: sentry-raven
120
134
  requirement: !ruby/object:Gem::Requirement
@@ -129,6 +143,20 @@ dependencies:
129
143
  - - "~>"
130
144
  - !ruby/object:Gem::Version
131
145
  version: '3.1'
146
+ - !ruby/object:Gem::Dependency
147
+ name: sentry-ruby
148
+ requirement: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '6.5'
153
+ type: :development
154
+ prerelease: false
155
+ version_requirements: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '6.5'
132
160
  - !ruby/object:Gem::Dependency
133
161
  name: simplecov
134
162
  requirement: !ruby/object:Gem::Requirement