quiz_api_client 4.13.1 → 4.13.3

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: 9ba12e6decb1a28b9587740a754e454a58c5345d28cf72632b267484bc82eab0
4
- data.tar.gz: 7f7773f75f5f586abf61542bb32b013cba0c25e70feb498d709b6682b6f65f2a
3
+ metadata.gz: ba3c7ef7577c2eb514a0ba86c804c2b3f98ffcbad90bae29e6a0e505ab987bb4
4
+ data.tar.gz: 96a6091a6c5b5811bd7776871b9b0af664f3a330306a614f6b0d17662a54c6f9
5
5
  SHA512:
6
- metadata.gz: 3820e1479d10ed16e76f278a741b359334af9c494fd06d3d4db93f597f7209201a4a43b7492d7c90e5d84187e12f69ca56fb0deff1e404dac91bb95a15c5ef82
7
- data.tar.gz: fdc87d815718c2a0585cbe1bcc50b93c9aadcf0b18e384ab0dd000f9e619e83e89a881b70db478732e1f44d949eabcfbb5f4b4b99d9de4e9462caa36b699b6ac
6
+ metadata.gz: 679f371890c6fa8bbcde89b60f7eaa8bee1e379c666a7fd7e53b6869f69f9e337b45b715a2d469f2fd9f8b2e31aac6b6d7575fddee1e419c1a0b37c87bbb3922
7
+ data.tar.gz: ebb6c033080207b63e7d3da0d0668057e9b845c95cf43b1ffd70ec8c3da2639c2ae237ef6a1cc36db0a08244d1b92e5c1e69097aecd1c53805e4b7ed7b40009e
@@ -31,7 +31,7 @@ module QuizApiClient
31
31
  def get(path, all: false, query: {})
32
32
  return make_request :get, url_for(path), query: query unless all
33
33
 
34
- make_paginated_request :get, url_for(path), query: query
34
+ make_paginated_request :get, url_for(path), query: sanitized_query(query, all)
35
35
  end
36
36
 
37
37
  def post(path, body = {})
@@ -52,6 +52,14 @@ module QuizApiClient
52
52
 
53
53
  private
54
54
 
55
+ def sanitized_query(query, all)
56
+ query.delete(:page) if query.key?(:page) && query[:page].nil? || all
57
+
58
+ query.delete(:per_page) if query.key?(:per_page) && query[:per_page].nil?
59
+
60
+ query
61
+ end
62
+
55
63
  def initialize_logger(log_level)
56
64
  HTTParty::Logger.add_formatter('quiz_api_client_json_formatter', QuizApiClient::JSONFormatter)
57
65
  @logger = ::Logger.new(
@@ -3,7 +3,7 @@ module QuizApiClient::Services
3
3
  def list(params:, token: nil, all: false)
4
4
  raise 'Bank Id Required' unless params && params[:id]
5
5
 
6
- pagination_params = { page: params.delete(:page), per_page: params.delete(:per_page) }
6
+ pagination_params = { page: params.delete(:page), per_page: params.delete(:per_page) }.compact
7
7
  get_from_quiz_api(params: params, token: token, pagination_params: pagination_params, all: all)
8
8
  end
9
9
 
@@ -23,7 +23,7 @@ module QuizApiClient::Services
23
23
  def list(params:, token: nil, all: false)
24
24
  raise 'Quiz Id Required' unless params && params[:id]
25
25
 
26
- pagination_params = { page: params.delete(:page), per_page: params.delete(:per_page) }
26
+ pagination_params = { page: params.delete(:page), per_page: params.delete(:per_page) }.compact
27
27
  get_from_quiz_api(params: params, token: token, pagination_params: pagination_params, all: all)
28
28
  end
29
29
 
@@ -1,3 +1,3 @@
1
1
  module QuizApiClient
2
- VERSION = '4.13.1'.freeze
2
+ VERSION = '4.13.3'.freeze
3
3
  end
@@ -81,7 +81,17 @@ describe QuizApiClient::HttpClient do
81
81
  path = '/api/quizzes'
82
82
  stub_quiz_api path, headers: { link: link_header(path, 1, 2) }
83
83
  stub_quiz_api path, item: 2, query: { page: 2 }, headers: { link: link_header(path, 2, 2) }
84
- expect(client.get('/api/quizzes', all: true)).to eq [1, 2]
84
+ expect(client.get('/api/quizzes', query: { page: 2 }, all: true)).to eq [1, 2]
85
+ end
86
+
87
+ it 'sanitizies the query param if page is nil' do
88
+ stub_quiz_api '/api/quizzes'
89
+ expect(client.get('/api/quizzes', query: { page: nil }, all: true)).to eq [1]
90
+ end
91
+
92
+ it 'sanitizies the query param if per_page is nil' do
93
+ stub_quiz_api '/api/quizzes'
94
+ expect(client.get('/api/quizzes', query: { per_page: nil }, all: true)).to eq [1]
85
95
  end
86
96
  end
87
97
 
@@ -102,6 +112,20 @@ describe QuizApiClient::HttpClient do
102
112
  stub_quiz_api path, item: 2, query: { my_id: 12, **dynamo_params }
103
113
  expect(client.get('/api/quizzes', query: { my_id: 12 }, all: true)).to eq [1, 2]
104
114
  end
115
+
116
+ it 'retrieves subsequent pages when all is true and sanitizes query if page is nil' do
117
+ path = '/api/quizzes'
118
+ stub_quiz_api path, query: { my_id: 12 }, headers: dynamo_headers
119
+ stub_quiz_api path, item: 2, query: { my_id: 12, **dynamo_params }
120
+ expect(client.get('/api/quizzes', query: { my_id: 12, page: nil }, all: true)).to eq [1, 2]
121
+ end
122
+
123
+ it 'retrieves subsequent pages when all is true and sanitizes query if per_page is nil' do
124
+ path = '/api/quizzes'
125
+ stub_quiz_api path, query: { my_id: 12 }, headers: dynamo_headers
126
+ stub_quiz_api path, item: 2, query: { my_id: 12, **dynamo_params }
127
+ expect(client.get('/api/quizzes', query: { my_id: 12, per_page: nil }, all: true)).to eq [1, 2]
128
+ end
105
129
  end
106
130
  end
107
131
  end
@@ -3,24 +3,53 @@ describe QuizApiClient::Services::BankEntriesService do
3
3
  let(:config) { QuizApiClient::Config.new { |c| c.host = host } }
4
4
  let(:subject) { described_class.new(config) }
5
5
 
6
+ def link_header(host, path, page, last_page)
7
+ link_header = "<https://#{host}#{path}?page=#{last_page}>; rel=\"last\""
8
+ link_header += ", <https://#{host}#{path}?page=#{page + 1}>; rel=\"next\"" if page < last_page
9
+ link_header += ", <https://#{host}#{path}?page=#{page - 1}>; rel=\"prev\"" if page > 1
10
+ link_header += ", <https://#{host}#{path}?page=1>; rel=\"first\"" if page > 1
11
+ { link: link_header }
12
+ end
13
+
14
+ def stub_quiz_api(url, body, query: {}, headers: {}, status: 200)
15
+ stub_request(:get, url)
16
+ .with(query: query)
17
+ .to_return(
18
+ body: body.to_json,
19
+ status: status,
20
+ headers: headers.merge(
21
+ 'Content-Type' => 'application/json'
22
+ )
23
+ )
24
+ end
25
+
6
26
  describe '#list' do
7
27
  let(:params) { { id: 1 } }
8
- let(:stubbed_response) { { 'id' => 1, 'entry' => { 'id' => 1 } } }
9
- let(:expected_url) { "https://#{host}/api/internal_services/banks/#{params[:id]}/bank_entries?page=&per_page=" }
10
- let(:status_code) { 200 }
28
+ let(:path) { "/api/internal_services/banks/#{params[:id]}/bank_entries" }
29
+ let(:expected_url) { "https://#{host}#{path}" }
11
30
 
12
- before do
13
- stub_request(:get, expected_url)
14
- .to_return(
15
- status: status_code,
16
- body: stubbed_response.to_json,
17
- headers: { 'Content-Type' => 'application/json' }
18
- )
31
+ def page(page_num)
32
+ [{ 'id' => page_num, 'entry' => { 'id' => page_num } }]
33
+ end
34
+
35
+ it 'single page' do
36
+ stub_quiz_api(expected_url, page(1), headers: link_header(host, path, 1, 1))
37
+ result = subject.list(params: params, token: 'token', all: true)
38
+ expect(result).to eql([{ 'id' => 1, 'entry' => { 'id' => 1 } }])
19
39
  end
20
40
 
21
- it 'gets from quiz_api/api/banks/{id}/bank_entries and returns the response' do
22
- result = subject.list(params: params, token: 'token')
23
- expect(result.parsed_response).to eql(stubbed_response)
41
+ it 'paginated response' do
42
+ expected_result = [
43
+ { 'id' => 1, 'entry' => { 'id' => 1 } },
44
+ { 'id' => 2, 'entry' => { 'id' => 2 } },
45
+ { 'id' => 3, 'entry' => { 'id' => 3 } }
46
+ ]
47
+
48
+ stub_quiz_api(expected_url, page(1), headers: link_header(host, path, 1, 3))
49
+ stub_quiz_api(expected_url, page(2), query: { page: 2 }, headers: link_header(host, path, 2, 3))
50
+ stub_quiz_api(expected_url, page(3), query: { page: 3 }, headers: link_header(host, path, 3, 3))
51
+ result = subject.list(params: params, token: 'token', all: true)
52
+ expect(result).to eql(expected_result)
24
53
  end
25
54
  end
26
55
  end
@@ -3,24 +3,53 @@ describe QuizApiClient::Services::QuizEntriesService do
3
3
  let(:config) { QuizApiClient::Config.new { |c| c.host = host } }
4
4
  let(:subject) { described_class.new(config) }
5
5
 
6
+ def link_header(host, path, page, last_page)
7
+ link_header = "<https://#{host}#{path}?page=#{last_page}>; rel=\"last\""
8
+ link_header += ", <https://#{host}#{path}?page=#{page + 1}>; rel=\"next\"" if page < last_page
9
+ link_header += ", <https://#{host}#{path}?page=#{page - 1}>; rel=\"prev\"" if page > 1
10
+ link_header += ", <https://#{host}#{path}?page=1>; rel=\"first\"" if page > 1
11
+ { link: link_header }
12
+ end
13
+
14
+ def stub_quiz_api(url, body, query: {}, headers: {}, status: 200)
15
+ stub_request(:get, url)
16
+ .with(query: query)
17
+ .to_return(
18
+ body: body.to_json,
19
+ status: status,
20
+ headers: headers.merge(
21
+ 'Content-Type' => 'application/json'
22
+ )
23
+ )
24
+ end
25
+
6
26
  describe '#list' do
7
27
  let(:params) { { id: 1 } }
8
- let(:stubbed_response) { { 'id' => 1, 'entry' => { 'id' => 1 } } }
9
- let(:expected_url) { "https://#{host}/api/quizzes/#{params[:id]}/quiz_entries?page=&per_page=" }
10
- let(:status_code) { 200 }
28
+ let(:path) { "/api/quizzes/#{params[:id]}/quiz_entries" }
29
+ let(:expected_url) { "https://#{host}#{path}" }
11
30
 
12
- before do
13
- stub_request(:get, expected_url)
14
- .to_return(
15
- status: status_code,
16
- body: stubbed_response.to_json,
17
- headers: { 'Content-Type' => 'application/json' }
18
- )
31
+ def page(page_num)
32
+ [{ 'id' => page_num, 'entry' => { 'id' => page_num } }]
19
33
  end
20
34
 
21
- it 'gets from quiz_api/api/quizzes/{id}/quiz_entries and returns the response' do
22
- result = subject.list(params: params, token: 'token')
23
- expect(result.parsed_response).to eql(stubbed_response)
35
+ it 'single page' do
36
+ stub_quiz_api(expected_url, page(1), headers: link_header(host, path, 1, 1))
37
+ result = subject.list(params: params, token: 'token', all: true)
38
+ expect(result).to eql([{ 'id' => 1, 'entry' => { 'id' => 1 } }])
39
+ end
40
+
41
+ it 'paginated response' do
42
+ expected_result = [
43
+ { 'id' => 1, 'entry' => { 'id' => 1 } },
44
+ { 'id' => 2, 'entry' => { 'id' => 2 } },
45
+ { 'id' => 3, 'entry' => { 'id' => 3 } }
46
+ ]
47
+
48
+ stub_quiz_api(expected_url, page(1), headers: link_header(host, path, 1, 3))
49
+ stub_quiz_api(expected_url, page(2), query: { page: 2 }, headers: link_header(host, path, 2, 3))
50
+ stub_quiz_api(expected_url, page(3), query: { page: 3 }, headers: link_header(host, path, 3, 3))
51
+ result = subject.list(params: params, token: 'token', all: true)
52
+ expect(result).to eql(expected_result)
24
53
  end
25
54
  end
26
55
 
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.13.1
4
+ version: 4.13.3
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: 2023-08-23 00:00:00.000000000 Z
18
+ date: 2023-09-26 00:00:00.000000000 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: httparty