rails_api_kit 1.0.1 → 1.0.2

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: 3c6f2c6b595530d2b1c63ad4a4e582c83e2555e04bc26c4d5e5c0e31e879fd34
4
- data.tar.gz: 4c1700476844577653e7311cf315c1eaeca1c7eb82b1a33cd0dedd646e330311
3
+ metadata.gz: 398056ff21cfe533837881bebaf65d87fff8c5abba87d5cdf8a9c708d55c90ef
4
+ data.tar.gz: 0b9de9c9f63c4312b898a03d5b8b142a57f82f7a353588f656a0b3cc8da2d290
5
5
  SHA512:
6
- metadata.gz: 1c998192b4f161fb2cb4cd7f87253f94feb9a83e05b1e30e0f70a9e533130e2f0864da68205f9babbac0c8c8b26319606ef3567008ca25499e00b51f40566c0f
7
- data.tar.gz: 05c8ffa12577ce6980e98750ff4e6050276c6b4cd5d4a7aaef2e2a24c693f470e39263106b0c96b28541fdd6dfe8959a15454553f61dd5a54a6088ff427efb70
6
+ metadata.gz: d965e94656b77eec9b146898305aed13c67aa744c589e101065f87a828433e1a99e9677145d79285b0e630d7c26d31c0f1e0c63365b281f84007477e235a1afe
7
+ data.tar.gz: 45b408f991bbf95bc9341396249e2d12900a906cc0359120a6e1877607c4c41f1898fb79862db405fc13a874087ddca3aecafee5014beabf40b6421d841ad079
data/README.md CHANGED
@@ -292,7 +292,7 @@ This gem leverages these excellent libraries:
292
292
 
293
293
  ## Contributing
294
294
 
295
- Bug reports and pull requests are welcome on GitHub at https://github.com/iakbudak/api_kit
295
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ismailakbudak/rails_api_kit
296
296
 
297
297
  This project follows the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
298
298
 
@@ -49,7 +49,9 @@ module ApiKit
49
49
  *api_path_parameters.keys.map(&:to_s)
50
50
  ).as_json.with_indifferent_access
51
51
 
52
- original_params[:page] = original_params[:page].dup || {}
52
+ # A blank `page=` passed `api_pagination_params` as "no page"; links build a fresh one.
53
+ page = original_params[:page]
54
+ original_params[:page] = page.is_a?(Hash) ? page.dup : {}
53
55
  original_url = "?"
54
56
 
55
57
  pagination.each do |page_name, number|
@@ -132,7 +134,16 @@ module ApiKit
132
134
  #
133
135
  # @return [Array] with the offset, limit and the current page number
134
136
  def api_pagination_params
135
- pagination = params[:page].try(:slice, :number, :size) || {}
137
+ page = params[:page]
138
+ # `page=` names no page: the defaults, like a missing `page`.
139
+ page = nil if page.is_a?(String) && page.blank?
140
+ # `page=2` (or a JSON array / number) is not `page[number]=2`: a client error like
141
+ # `params.expect`, not a 500 from #slice.
142
+ unless page.nil? || page.is_a?(ActionController::Parameters) || page.is_a?(Hash)
143
+ raise ActionController::ParameterMissing, :page
144
+ end
145
+
146
+ pagination = page.try(:slice, :number, :size) || {}
136
147
  per_page = api_page_size(pagination)
137
148
  num = [ 1, pagination[:number].to_f.to_i ].max
138
149
 
@@ -1,3 +1,3 @@
1
1
  module ApiKit
2
- VERSION = "1.0.1"
2
+ VERSION = "1.0.2"
3
3
  end
@@ -40,11 +40,18 @@ RSpec.describe UsersController, type: :request do
40
40
  let(:user) { users.last }
41
41
 
42
42
  context 'returns filtered users' do
43
+ # FFaker names are random, so filter on a token that no other user
44
+ # can match by accident
45
+ let(:name_prefix) { 'Filterable' }
46
+ let(:filterable_name) { "#{name_prefix}#{third_user.id}" }
47
+
43
48
  let(:params) do
49
+ third_user.update(first_name: filterable_name)
50
+
44
51
  {
45
52
  filter: {
46
53
  last_name_or_first_name_cont_any: (
47
- "#{third_user.first_name[0..5]}%,#{self.class.name}"
54
+ "#{name_prefix},#{self.class.name}"
48
55
  )
49
56
  }
50
57
  }
@@ -56,6 +63,27 @@ RSpec.describe UsersController, type: :request do
56
63
  expect(response_json['data'][0]['id']).to eql(third_user.id)
57
64
  end
58
65
 
66
+ # `cont` escapes LIKE wildcards, `matches` passes them through
67
+ context 'with a wildcard' do
68
+ let(:params) do
69
+ third_user.update(first_name: filterable_name)
70
+
71
+ {
72
+ filter: {
73
+ last_name_or_first_name_matches_any: (
74
+ "#{name_prefix}%,#{self.class.name}"
75
+ )
76
+ }
77
+ }
78
+ end
79
+
80
+ it do
81
+ expect(response).to have_http_status(:ok)
82
+ expect(response_json['data'].size).to eq(1)
83
+ expect(response_json['data'][0]['id']).to eql(third_user.id)
84
+ end
85
+ end
86
+
59
87
  context 'with a comma' do
60
88
  let(:params) do
61
89
  third_user.update(first_name: third_user.first_name + ',')
@@ -332,4 +332,50 @@ RSpec.describe UsersController, type: :request do
332
332
  end
333
333
  end
334
334
  end
335
+
336
+ describe 'GET /users with a scalar page' do
337
+ # `page=2` is not `page[number]=2`: ParameterMissing, a client error, not a 500 from
338
+ # String#slice. This controller has no ApiKit::Errors, so Rails answers 400; with it, 422.
339
+ [ 'page=2', 'page[]=2' ].each do |query|
340
+ it "answers #{query} with a client error" do
341
+ get("#{users_path}?#{query}", headers: api_headers)
342
+
343
+ expect(response).to have_http_status(:bad_request)
344
+ end
345
+ end
346
+ end
347
+
348
+ describe 'GET /users with a blank page' do
349
+ # `page=` names no page: the defaults, like a missing `page`.
350
+ [ 'page=', 'page=%20' ].each do |query|
351
+ it "answers #{query} with the first page" do
352
+ get("#{users_path}?#{query}", headers: api_headers)
353
+
354
+ expect(response).to have_http_status(:ok)
355
+ expect(response_json.dig('meta', 'pagination', 'current')).to eq(1)
356
+ end
357
+ end
358
+ end
359
+
360
+ describe '#api_pagination_params with a typed non-hash page' do
361
+ # A JSON body reaches it typed: an array or a number is malformed too, never a #slice error.
362
+ let(:controller_class) do
363
+ Class.new do
364
+ include ApiKit::Pagination
365
+
366
+ attr_reader :params
367
+
368
+ def initialize(params)
369
+ @params = ActionController::Parameters.new(params)
370
+ end
371
+ end
372
+ end
373
+
374
+ [ [], 2 ].each do |page|
375
+ it "raises ParameterMissing for #{page.inspect}" do
376
+ expect { controller_class.new(page: page).send(:api_pagination_params) }
377
+ .to raise_error(ActionController::ParameterMissing)
378
+ end
379
+ end
380
+ end
335
381
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_api_kit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ismail Akbudak
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-09-25 00:00:00.000000000 Z
11
+ date: 2026-11-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ransack
@@ -247,7 +247,7 @@ files:
247
247
  - spec/sparse_fields_type_key_spec.rb
248
248
  - spec/spec_helper.rb
249
249
  - spec/support/api_kit_rspec.rb
250
- homepage: https://github.com/iakbudak/api_kit
250
+ homepage: https://github.com/ismailakbudak/rails_api_kit
251
251
  licenses:
252
252
  - MIT
253
253
  metadata: {}
@@ -266,7 +266,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
266
266
  - !ruby/object:Gem::Version
267
267
  version: '0'
268
268
  requirements: []
269
- rubygems_version: 3.5.22
269
+ rubygems_version: 3.4.19
270
270
  signing_key:
271
271
  specification_version: 4
272
272
  summary: A lightweight toolkit for building JSON:API compliant APIs with Rails.