api_me 0.4.1 → 0.5.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
  SHA1:
3
- metadata.gz: d6be2bfc501d6e34780bb7d71cc7f9504110bbe5
4
- data.tar.gz: 0cdf554cf51fc1c08cc960b9aa03d9869a631ecc
3
+ metadata.gz: 02a356aba53a5aadcbbccb6dd04d7e4e5cc3cb51
4
+ data.tar.gz: c544bba077a0144161f4bf585e6d77a39cfb9d79
5
5
  SHA512:
6
- metadata.gz: af00b5703f9677aa28ae0e7ae5422440931e246d90ef0f3b2ea0166d4e85b53e2c259e22568e77fa5c65bac992b2d118bb0742a616dd916f30ab08bb8c9ce5d2
7
- data.tar.gz: d46be75bc55cf3ab8fa89f8c33af26c9a139098b680153a62b750a05f15130fe61802cde9955ca8731f0a046d77b548050090ecd5c58936deea169ae0cc432b0
6
+ metadata.gz: 03ff2f340a021a3d81363cda80165971cb42dccd0375ea80142f42ab08ac6fff4b265cd733b76c9e816f8295c5a4eec3540c844d4c29ececf598368a91fe5f48
7
+ data.tar.gz: 9eda9a733e53c50b7dcde5df28d7771eec648bd55ce21f35fd0167432e3e9a44f19ec7eb4ed238cb5df9f73c37dba6baf3c747fdccf40d00c83bcb53cd99faab
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.1.4
1
+ 2.1.5
data/.travis.yml CHANGED
@@ -1,6 +1,4 @@
1
1
  language: ruby
2
2
  script: bundle exec rspec spec
3
3
  rvm:
4
- - 1.9.3
5
- - 2.0.0
6
4
  - 2.1.0
data/Gemfile CHANGED
@@ -3,6 +3,7 @@ source 'https://rubygems.org'
3
3
  gemspec
4
4
 
5
5
  group :test do
6
+ gem 'pry'
6
7
  gem 'activerecord'
7
8
  gem 'activesupport'
8
9
  gem 'actionpack' # action_controller, action_view
data/api_me.gemspec CHANGED
@@ -23,6 +23,7 @@ Gem::Specification.new do |s|
23
23
  s.add_runtime_dependency 'pundit', '~> 1.0'
24
24
  s.add_runtime_dependency 'active_model_serializers', '~> 0.8.0'
25
25
  s.add_runtime_dependency 'search_object', '~> 1.0'
26
+ s.add_runtime_dependency 'kaminari', '~> 0.16.3'
26
27
 
27
28
  s.add_development_dependency 'combustion', '~> 0.5.1'
28
29
  s.add_development_dependency 'rspec-rails', '~> 3'
data/lib/api_me.rb CHANGED
@@ -1,9 +1,11 @@
1
1
  require 'active_support/concern'
2
2
  require 'pundit'
3
3
  require 'search_object'
4
+ require 'kaminari'
4
5
 
5
6
  require 'api_me/version'
6
7
  require 'api_me/base_filter'
8
+ require 'api_me/pagination'
7
9
 
8
10
  module ApiMe
9
11
  extend ActiveSupport::Concern
@@ -65,12 +67,13 @@ module ApiMe
65
67
  def index
66
68
  ids_filter_hash = params[:ids] ? { ids: params[:ids] } : {}
67
69
  @scoped_objects = policy_scope(resource_scope)
68
- @filter_objects = filter_klass.new(
70
+ @filter_object = filter_klass.new(
69
71
  scope: @scoped_objects,
70
72
  filters: (filter_params || {}).merge(ids_filter_hash)
71
73
  )
74
+ @pagination_object = ApiMe::Pagination.new(scope: @filter_object.results, page_params: params[:page])
72
75
 
73
- render json: @filter_objects.results, each_serializer: serializer_klass
76
+ render json: @pagination_object.results, each_serializer: serializer_klass, meta: { page: @pagination_object.page_meta }
74
77
  end
75
78
 
76
79
  def show
@@ -0,0 +1,55 @@
1
+ module ApiMe
2
+ class Pagination
3
+ attr_accessor :page_size, :page_offset, :scope
4
+
5
+ def initialize(scope:, page_params:)
6
+ self.scope = scope
7
+
8
+ if page_params
9
+ self.page_size = page_params[:size]
10
+ self.page_offset = page_params[:offset]
11
+ end
12
+ end
13
+
14
+ def results
15
+ paging? ? page.per.scope : scope
16
+ end
17
+
18
+ def page_meta
19
+ return Hash.new unless paging?
20
+
21
+ {
22
+ size: page_size.nil? ? default_page_size : page_size,
23
+ offset: page_offset,
24
+ record_count: scope.size,
25
+ total_records: scope.total_count,
26
+ total_pages: scope.total_pages
27
+ }
28
+ end
29
+
30
+ protected
31
+
32
+ def page
33
+ self.scope = scope.page(self.page_offset ? page_offset : 1)
34
+ self
35
+ end
36
+
37
+ def per
38
+ if page_size
39
+ self.scope = scope.per(page_size)
40
+ end
41
+
42
+ self
43
+ end
44
+
45
+ private
46
+
47
+ def default_page_size
48
+ Kaminari.config.default_per_page
49
+ end
50
+
51
+ def paging?
52
+ page_size || page_offset
53
+ end
54
+ end
55
+ end
@@ -1,3 +1,3 @@
1
1
  module ApiMe
2
- VERSION = '0.4.1'
2
+ VERSION = '0.5.0'
3
3
  end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe 'Users API' do
3
+ describe 'Posts API' do
4
4
  it 'sends the list of posts using the default filter' do
5
5
  posts = [
6
6
  Post.create(name: 'test'),
@@ -15,6 +15,111 @@ describe 'Users API' do
15
15
  expect(json['posts'].length).to eq(posts.count)
16
16
  end
17
17
 
18
+ it 'is paging with page offset using default page size and restricting size' do
19
+ 40.times do
20
+ Post.create(name: 'page item')
21
+ end
22
+
23
+ get '/api/v1/posts?page%5Boffset%5D=1'
24
+ json = JSON.parse(last_response.body)
25
+ expect(json['posts'].length).to eq(25)
26
+
27
+ end
28
+
29
+ it 'is page offset of 2 working for size of 40 default page size of 25' do
30
+ 40.times do
31
+ Post.create(name: 'page item')
32
+ end
33
+
34
+ get '/api/v1/posts?page%5Boffset%5D=2'
35
+ json = JSON.parse(last_response.body)
36
+ expect(json['posts'].length).to eq(15)
37
+
38
+ end
39
+
40
+ it 'is page offset of -1 working for size of 40 default page size of 25' do
41
+ #min page offset is 1, anything less gets converted to 1 and results in page size records
42
+ 40.times do
43
+ Post.create(name: 'page item')
44
+ end
45
+
46
+ get '/api/v1/posts?page%5Boffset%5D=-1'
47
+ json = JSON.parse(last_response.body)
48
+ expect(json['posts'].length).to eq(25)
49
+
50
+ end
51
+
52
+ it 'is page offset of 3 working for size of 40 default page size of 25' do
53
+ #overflow on page offset will result in 0 records
54
+ 40.times do
55
+ Post.create(name: 'page item')
56
+ end
57
+
58
+ get '/api/v1/posts?page%5Boffset%5D=3'
59
+ json = JSON.parse(last_response.body)
60
+ expect(json['posts'].length).to eq(0)
61
+
62
+ end
63
+
64
+ it 'is page size of 10 working for default offset of 1' do
65
+ #overflow on page offset will result in 0 records
66
+ 40.times do
67
+ Post.create(name: 'page item')
68
+ end
69
+
70
+ get '/api/v1/posts?page%5Bsize%5D=10'
71
+ json = JSON.parse(last_response.body)
72
+ expect(json['posts'].length).to eq(10)
73
+
74
+ end
75
+
76
+ it 'is page size working when the value is negative' do
77
+ #overflow on page offset will result in 0 records
78
+ 40.times do
79
+ Post.create(name: 'page item')
80
+ end
81
+
82
+ get '/api/v1/posts?page%5Bsize%5D=-10'
83
+ json = JSON.parse(last_response.body)
84
+ expect(json['posts'].length).to eq(25)
85
+
86
+ end
87
+
88
+ it 'is page size working when the value is higher than the total record count' do
89
+ #overflow on page offset will result in 0 records
90
+ 40.times do
91
+ Post.create(name: 'page item')
92
+ end
93
+
94
+ get '/api/v1/posts?page%5Bsize%5D=100'
95
+ json = JSON.parse(last_response.body)
96
+ expect(json['posts'].length).to eq(40)
97
+ end
98
+
99
+ it 'is the result all records when no page paramas are sent' do
100
+ #overflow on page offset will result in 0 records
101
+ 100.times do
102
+ Post.create(name: 'page item')
103
+ end
104
+
105
+ get '/api/v1/posts'
106
+ json = JSON.parse(last_response.body)
107
+ expect(json['posts'].length).to eq(100)
108
+
109
+ end
110
+
111
+ it 'restricts page_size when max_per_page config is less' do
112
+ 100.times do
113
+ Post.create(name: 'page item')
114
+ end
115
+
116
+ Kaminari.config.max_per_page = 10
117
+
118
+ get '/api/v1/posts?page%5Bsize%5D=100'
119
+ json = JSON.parse(last_response.body)
120
+ expect(json['posts'].length).to eq(10)
121
+ end
122
+
18
123
  it 'sends posts filtered by ids' do
19
124
  all_posts = [
20
125
  Post.create(name: 'test'),
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_me
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Clopton
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-07-13 00:00:00.000000000 Z
12
+ date: 2016-02-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -81,6 +81,20 @@ dependencies:
81
81
  - - "~>"
82
82
  - !ruby/object:Gem::Version
83
83
  version: '1.0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: kaminari
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: 0.16.3
91
+ type: :runtime
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: 0.16.3
84
98
  - !ruby/object:Gem::Dependency
85
99
  name: combustion
86
100
  requirement: !ruby/object:Gem::Requirement
@@ -171,6 +185,7 @@ files:
171
185
  - config.ru
172
186
  - lib/api_me.rb
173
187
  - lib/api_me/base_filter.rb
188
+ - lib/api_me/pagination.rb
174
189
  - lib/api_me/version.rb
175
190
  - lib/generators/api_me/controller/USAGE
176
191
  - lib/generators/api_me/controller/controller_generator.rb