api_only_pagination 0.1.0 → 0.1.2

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: df8d4b65cd6919886b92cc7aa4321cb56c64acd0656ad54aa5b057b772b94815
4
- data.tar.gz: 82e0979809dcbc13f795a161e5345e52192730d301b7b61ca5612414b0e2ec67
3
+ metadata.gz: f6d061e8e8b177a9162d5d74908e3cc3ba5e2fc2bae27e5d4623c036b31a8690
4
+ data.tar.gz: 6a8de04ac9425d054808fbb3c5d4554c28a3444753229f2a047891ebaa183a63
5
5
  SHA512:
6
- metadata.gz: 8851f8382e803cfc15b291f40feba6c005f4ed94ea46b45ca76a62453f0950ebe5a346529f8a262149537e86eb196f35dc706edfde5e7287033c8bb935204dbb
7
- data.tar.gz: 588b778f574d748a74be48911e6c18c22722a55246088d6e4796e5069314e68b79592f4aa36b2cf75d717807b3a24c74a2deeee93c757896a9d8596ae16c8782
6
+ metadata.gz: e187502965e71964a3224647ffbaa34e13763780c292d8f01a3c2e350632bff1738d314a10a8b9534bf84522f6fdfdf8bad020c6dca122907040f27cc19a692b
7
+ data.tar.gz: d2d059b67a91be138b6aae23d0506c41c5d5c8ee957f5de172e338d5e2c2c53a6786d51f77606e24295f452fd354e7f0a986dd0a28c794032391effbbc4ae9b5
data/Gemfile.lock ADDED
@@ -0,0 +1,123 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ api_only_pagination (0.1.2)
5
+ activerecord (>= 5.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actionpack (6.1.7.3)
11
+ actionview (= 6.1.7.3)
12
+ activesupport (= 6.1.7.3)
13
+ rack (~> 2.0, >= 2.0.9)
14
+ rack-test (>= 0.6.3)
15
+ rails-dom-testing (~> 2.0)
16
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
17
+ actionview (6.1.7.3)
18
+ activesupport (= 6.1.7.3)
19
+ builder (~> 3.1)
20
+ erubi (~> 1.4)
21
+ rails-dom-testing (~> 2.0)
22
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
23
+ activemodel (6.1.7.3)
24
+ activesupport (= 6.1.7.3)
25
+ activerecord (6.1.7.3)
26
+ activemodel (= 6.1.7.3)
27
+ activesupport (= 6.1.7.3)
28
+ activesupport (6.1.7.3)
29
+ concurrent-ruby (~> 1.0, >= 1.0.2)
30
+ i18n (>= 1.6, < 2)
31
+ minitest (>= 5.1)
32
+ tzinfo (~> 2.0)
33
+ zeitwerk (~> 2.3)
34
+ ast (2.4.2)
35
+ builder (3.2.4)
36
+ concurrent-ruby (1.2.2)
37
+ crass (1.0.6)
38
+ diff-lcs (1.5.0)
39
+ erubi (1.12.0)
40
+ i18n (1.13.0)
41
+ concurrent-ruby (~> 1.0)
42
+ json (2.6.1)
43
+ loofah (2.20.0)
44
+ crass (~> 1.0.2)
45
+ nokogiri (>= 1.5.9)
46
+ method_source (1.0.0)
47
+ minitest (5.18.0)
48
+ nokogiri (1.13.10-x86_64-darwin)
49
+ racc (~> 1.4)
50
+ parallel (1.21.0)
51
+ parser (3.2.1.1)
52
+ ast (~> 2.4.1)
53
+ racc (1.6.2)
54
+ rack (2.2.7)
55
+ rack-test (2.1.0)
56
+ rack (>= 1.3)
57
+ rails-dom-testing (2.0.3)
58
+ activesupport (>= 4.2.0)
59
+ nokogiri (>= 1.6)
60
+ rails-html-sanitizer (1.5.0)
61
+ loofah (~> 2.19, >= 2.19.1)
62
+ railties (6.1.7.3)
63
+ actionpack (= 6.1.7.3)
64
+ activesupport (= 6.1.7.3)
65
+ method_source
66
+ rake (>= 12.2)
67
+ thor (~> 1.0)
68
+ rainbow (3.1.1)
69
+ rake (13.0.6)
70
+ regexp_parser (2.3.0)
71
+ rexml (3.2.5)
72
+ rspec (3.10.0)
73
+ rspec-core (~> 3.10.0)
74
+ rspec-expectations (~> 3.10.0)
75
+ rspec-mocks (~> 3.10.0)
76
+ rspec-core (3.10.1)
77
+ rspec-support (~> 3.10.0)
78
+ rspec-expectations (3.10.1)
79
+ diff-lcs (>= 1.2.0, < 2.0)
80
+ rspec-support (~> 3.10.0)
81
+ rspec-mocks (3.10.2)
82
+ diff-lcs (>= 1.2.0, < 2.0)
83
+ rspec-support (~> 3.10.0)
84
+ rspec-rails (4.0.2)
85
+ actionpack (>= 4.2)
86
+ activesupport (>= 4.2)
87
+ railties (>= 4.2)
88
+ rspec-core (~> 3.10)
89
+ rspec-expectations (~> 3.10)
90
+ rspec-mocks (~> 3.10)
91
+ rspec-support (~> 3.10)
92
+ rspec-support (3.10.2)
93
+ rubocop (1.48.1)
94
+ json (~> 2.3)
95
+ parallel (~> 1.10)
96
+ parser (>= 3.2.0.0)
97
+ rainbow (>= 2.2.2, < 4.0)
98
+ regexp_parser (>= 1.8, < 3.0)
99
+ rexml (>= 3.2.5, < 4.0)
100
+ rubocop-ast (>= 1.26.0, < 2.0)
101
+ ruby-progressbar (~> 1.7)
102
+ unicode-display_width (>= 2.4.0, < 3.0)
103
+ rubocop-ast (1.27.0)
104
+ parser (>= 3.2.1.0)
105
+ ruby-progressbar (1.11.0)
106
+ thor (1.2.1)
107
+ tzinfo (2.0.6)
108
+ concurrent-ruby (~> 1.0)
109
+ unicode-display_width (2.4.2)
110
+ zeitwerk (2.6.8)
111
+
112
+ PLATFORMS
113
+ x86_64-darwin-20
114
+
115
+ DEPENDENCIES
116
+ api_only_pagination!
117
+ rake (~> 13.0)
118
+ rspec (~> 3.6)
119
+ rspec-rails (~> 4.0)
120
+ rubocop (~> 1.21)
121
+
122
+ BUNDLED WITH
123
+ 2.4.9
data/README.md CHANGED
@@ -1,24 +1,43 @@
1
1
  # ApiOnlyPagination
2
-
3
- TODO: Delete this and the text below, and describe your gem
4
-
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/api_only_pagination`. To experiment with that code, run `bin/console` for an interactive prompt.
6
-
7
- ## Installation
8
-
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
-
11
- Install the gem and add to the application's Gemfile by executing:
12
-
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
14
-
15
- If bundler is not being used to manage dependencies, install the gem by executing:
16
-
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
18
-
2
+ Lightweight pagination gem that works only with Activerecord for Api only applications
3
+ ## Installation
4
+ To install this gem,
5
+ ```
6
+ $ gem install api_only_pagination
7
+ ```
8
+ or add it to your Gemfile
9
+ ```
10
+ gem 'api_only_pagination'
11
+ ```
19
12
  ## Usage
20
13
 
21
- TODO: Write usage instructions here
14
+ Configure your pagination adding api_only_pagination.rb file in config/initializers unless it will take default configurations.
15
+
16
+ ```
17
+ ApiOnlyPagination.configure do |config|
18
+ config.default_page_size = 3
19
+ config.max_per_page = 100
20
+ config.default_page = 1
21
+ end
22
+ ```
23
+
24
+ Now you can call chain .do_paginate(page, per_page) to your activerecord query.
25
+ ```
26
+ response = User.do_paginate(1,20)
27
+ ```
28
+
29
+ The response will be
30
+
31
+
32
+ {
33
+ "data" => [],
34
+ "per_page" => 3,
35
+ "current_page" => 1,
36
+ "total_pages" => 2,
37
+ "total_records" => 5
38
+ }
39
+
40
+
22
41
 
23
42
  ## Development
24
43
 
@@ -10,15 +10,14 @@ Gem::Specification.new do |spec|
10
10
 
11
11
  spec.summary = "pagination with api response only"
12
12
  spec.description = "pagination with api response only"
13
- # spec.homepage = ""
13
+ spec.homepage = "https://github.com/ashok1/api_only_pagination"
14
14
  spec.license = "MIT"
15
15
  spec.required_ruby_version = ">= 2.6.0"
16
16
 
17
17
  # spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
18
18
 
19
- # spec.metadata["homepage_uri"] = spec.homepage
20
- # spec.metadata["source_code_uri"] = ""
21
- # spec.metadata["changelog_uri"] = ""
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = spec.homepage
22
21
 
23
22
  # Specify which files should be added to the gem when it is released.
24
23
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
@@ -30,7 +29,9 @@ Gem::Specification.new do |spec|
30
29
  spec.bindir = "exe"
31
30
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
31
  spec.require_paths = ["lib"]
33
-
32
+ spec.add_dependency "activerecord", ">= 5.0"
33
+ spec.add_development_dependency "rspec", "~> 3.6"
34
+ spec.add_development_dependency "rspec-rails", "~> 4.0"
34
35
  # Uncomment to register a new dependency of your gem
35
36
  # spec.add_dependency "example-gem", "~> 1.0"
36
37
 
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record"
4
+
5
+ module ApiOnlyPagination
6
+ module ActiveRecordPaginate
7
+ extend ActiveSupport::Concern
8
+ module ClassMethods
9
+ def do_paginate(page = ApiOnlyPagination.configuration.default_page,
10
+ per_page = ApiOnlyPagination.configuration.default_page_size)
11
+ pagination = ApiOnlyPagination::Pagination.new(all, { page: page, per_page: per_page })
12
+ meta_data = pagination.metadata
13
+
14
+ ApiOnlyPagination::PaginatedData
15
+ .new(data: pagination.results,
16
+ per_page: meta_data.per_page,
17
+ current_page: page,
18
+ total_pages: meta_data.total_pages,
19
+ total_records: meta_data.count)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ApiOnlyPagination
4
+ class Configuration
5
+ attr_accessor :default_page_size, :max_per_page, :default_page
6
+
7
+ def initialize
8
+ @default_page_size = 20
9
+ @max_per_page = 100
10
+ @default_page = 1
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ApiOnlyPagination
4
+ class Paginate
5
+ attr_reader :page, :count, :per_page
6
+
7
+ def initialize(page, per_page, count)
8
+ @page = (page || ApiOnlyPagination.configuration.default_page).to_i
9
+ @count = count
10
+ @per_page = [(per_page.to_i || ApiOnlyPagination.configuration.default_page_size),
11
+ ApiOnlyPagination.configuration.max_per_page].min
12
+ end
13
+
14
+ def offset
15
+ return 0 if page == 1
16
+
17
+ per_page * (page.to_i - 1)
18
+ end
19
+
20
+ def next_page
21
+ page + 1 unless last_page?
22
+ end
23
+
24
+ def next_page?
25
+ page < total_pages
26
+ end
27
+
28
+ def previous_page
29
+ page - 1 unless first_page?
30
+ end
31
+
32
+ def previous_page?
33
+ page > 1
34
+ end
35
+
36
+ def last_page?
37
+ page == total_pages
38
+ end
39
+
40
+ def first_page?
41
+ page == 1
42
+ end
43
+
44
+ def total_pages
45
+ (count / per_page.to_f).ceil
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Main module
4
+ module ApiOnlyPagination
5
+ # Response object class
6
+ class PaginatedData
7
+ attr_accessor :data,
8
+ :per_page,
9
+ :current_page,
10
+ :total_pages,
11
+ :total_records
12
+
13
+ def initialize(data:, per_page:, current_page:, total_pages:, total_records:)
14
+ @data = data
15
+ @per_page = per_page
16
+ @current_page = current_page
17
+ @total_pages = total_pages
18
+ @total_records = total_records
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ApiOnlyPagination
4
+ class Pagination
5
+ attr_reader :collection, :params
6
+
7
+ def initialize(collection, params = {})
8
+ @collection = collection
9
+ @params = params.merge(count: collection.count)
10
+ end
11
+
12
+ def metadata
13
+ @metadata ||= ApiOnlyPagination::Paginate.new(params[:page], params[:per_page], params[:count])
14
+ end
15
+
16
+ def results
17
+ collection
18
+ .limit(metadata.per_page)
19
+ .offset(metadata.offset)
20
+ end
21
+ end
22
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ApiOnlyPagination
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.2"
5
5
  end
@@ -1,19 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "active_record"
3
4
  require_relative "api_only_pagination/version"
4
5
  require_relative "api_only_pagination/configuration"
5
6
  require_relative "api_only_pagination/paginate"
6
7
  require_relative "api_only_pagination/pagination"
8
+ require_relative "api_only_pagination/paginated_data"
9
+ require_relative "api_only_pagination/active_record_paginate"
7
10
 
8
11
  module ApiOnlyPagination
9
- def paginated_response(collection ,
10
- page = ApiOnlyPagination.configuration.default_page,
11
- per_page = ApiOnlyPagination.configuration.default_page_size )
12
- pagination = ApiOnlyPagination::Pagination.new(collection, {page: page, per_page: per_page})
13
- meta_data = pagination.metadata
14
- meta_data.as_json.merge({ data: pagination.results })
15
- end
16
-
17
12
  class << self
18
13
  def configuration
19
14
  @configuration ||= Configuration.new
@@ -28,3 +23,8 @@ module ApiOnlyPagination
28
23
  end
29
24
  end
30
25
  end
26
+
27
+ ActiveSupport.on_load(:active_record) do
28
+ include ApiOnlyPagination::ActiveRecordPaginate
29
+ ActiveRecord::Relation.include(ApiOnlyPagination::ActiveRecordPaginate)
30
+ end
metadata CHANGED
@@ -1,15 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_only_pagination
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ashok
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-03-24 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2023-05-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.0'
13
55
  description: pagination with api response only
14
56
  email:
15
57
  - kingkoirala1@gmail.com
@@ -21,17 +63,25 @@ files:
21
63
  - CHANGELOG.md
22
64
  - CODE_OF_CONDUCT.md
23
65
  - Gemfile
66
+ - Gemfile.lock
24
67
  - LICENSE.txt
25
68
  - README.md
26
69
  - Rakefile
27
70
  - api_only_pagination.gemspec
28
71
  - lib/api_only_pagination.rb
72
+ - lib/api_only_pagination/active_record_paginate.rb
73
+ - lib/api_only_pagination/configuration.rb
74
+ - lib/api_only_pagination/paginate.rb
75
+ - lib/api_only_pagination/paginated_data.rb
76
+ - lib/api_only_pagination/pagination.rb
29
77
  - lib/api_only_pagination/version.rb
30
78
  - sig/api_only_pagination.rbs
31
- homepage:
79
+ homepage: https://github.com/ashok1/api_only_pagination
32
80
  licenses:
33
81
  - MIT
34
- metadata: {}
82
+ metadata:
83
+ homepage_uri: https://github.com/ashok1/api_only_pagination
84
+ source_code_uri: https://github.com/ashok1/api_only_pagination
35
85
  post_install_message:
36
86
  rdoc_options: []
37
87
  require_paths: