jsonapi.rb 1.0.0 → 1.1.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: 2625c5af20d211c8e480235ec9587d648143eda2f76ffc2a5add9bdcc5d5e1d4
4
- data.tar.gz: e685d519946126bd51b3741b0980edab95d7da4f3bbb9ceca8cb248142eedd65
3
+ metadata.gz: 7f77c3754e421d90a87ea1b4e29d0a93684491c7b856358544e7d284e016f1e2
4
+ data.tar.gz: b2fc4ffb3a75a0dde01133af68e2993cc96ee4404adc5d7ffae49c97d15b0532
5
5
  SHA512:
6
- metadata.gz: fe25509464222844a3511f5cb768b85e3b078939822abe88889bf8f21df91fd8460d52b81d98dadd3e74ae831b7d0c66242353007a44f631560728029d6db231
7
- data.tar.gz: 789b5919fcc2b8092e9db62ddad3fc420d152b6e9a5542f2daba561a7233d5320e4a672b85bb7640bce31408a2e60788ee1b3ca65e503b766e8d3bfe0b599ee5
6
+ metadata.gz: 36eaed17ff71ecb1dbf2d78cdf80cde3d3f310a1540c62972c675430a5800cc555fc6b2cc00bd83044b5734782e7c8dea94cd9f8dd50efe29d1abfed5cb6f6d1
7
+ data.tar.gz: 3f6cd094ffad9b139329da5628ff68e53bccef6eba51b73f720fba402dccd9f84eae8fa3a9572f78b50dd33ad1cbd49a1f2d5816450fe58855fa2cf2b509078b
@@ -8,7 +8,7 @@ GIT
8
8
  PATH
9
9
  remote: .
10
10
  specs:
11
- jsonapi.rb (1.0.0)
11
+ jsonapi.rb (1.1.0)
12
12
  fast_jsonapi (~> 1.5)
13
13
  ransack (~> 2.1)
14
14
 
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # JSONAPI.rb :electric_plug:
2
2
 
3
+ [![Build Status](https://travis-ci.org/stas/jsonapi.rb.svg?branch=master)](https://travis-ci.org/stas/jsonapi.rb)
4
+
3
5
  So you say you need [JSON:API](https://jsonapi.org/) support in your API...
4
6
 
5
7
  > - hey how did your hackathon go?
@@ -26,19 +26,34 @@ module JSONAPI::Pagination
26
26
  #
27
27
  # @return [Array]
28
28
  def jsonapi_pagination(resources)
29
- links = {
30
- self: request.base_url + request.original_fullpath
31
- }
29
+ links = { self: request.base_url + request.original_fullpath }
30
+ pagination = jsonapi_pagination_meta(resources)
32
31
 
33
- return links unless resources.respond_to?(:many?)
34
-
35
- _, limit, page = jsonapi_pagination_params
32
+ return links if pagination.blank?
36
33
 
37
34
  original_params = params.except(
38
35
  *request.path_parameters.keys.map(&:to_s)).to_unsafe_h
39
36
  original_params[:page] ||= {}
40
37
  original_url = request.base_url + request.path + '?'
41
38
 
39
+ pagination.each do |page_name, number|
40
+ original_params[:page][:number] = number
41
+ links[page_name] = original_url + CGI.unescape(original_params.to_query)
42
+ end
43
+
44
+ links
45
+ end
46
+
47
+ # Generates pagination numbers
48
+ #
49
+ # @return [Hash] with the first, previous, next, current and last page number
50
+ def jsonapi_pagination_meta(resources)
51
+ return {} unless JSONAPI::Rails.is_collection?(resources)
52
+
53
+ _, limit, page = jsonapi_pagination_params
54
+
55
+ numbers = { current: page }
56
+
42
57
  if resources.respond_to?(:unscope)
43
58
  total = resources.unscope(:limit, :offset).count()
44
59
  else
@@ -48,20 +63,16 @@ module JSONAPI::Pagination
48
63
  last_page = [1, (total.to_f / limit).ceil].max
49
64
 
50
65
  if page > 1
51
- original_params[:page][:number] = 1
52
- links[:first] = original_url + CGI.unescape(original_params.to_query)
53
- original_params[:page][:number] = page - 1
54
- links[:prev] = original_url + CGI.unescape(original_params.to_query)
66
+ numbers[:first] = 1
67
+ numbers[:prev] = page - 1
55
68
  end
56
69
 
57
70
  if page < last_page
58
- original_params[:page][:number] = page + 1
59
- links[:next] = original_url + CGI.unescape(original_params.to_query)
60
- original_params[:page][:number] = last_page
61
- links[:last] = original_url + CGI.unescape(original_params.to_query)
71
+ numbers[:next] = page + 1
72
+ numbers[:last] = last_page
62
73
  end
63
74
 
64
- links
75
+ numbers
65
76
  end
66
77
 
67
78
  # Extracts the pagination params
@@ -1,3 +1,3 @@
1
1
  module JSONAPI
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stas Suscov