Skip to content

Commit ef5e006

Browse files
author
Alex Tharp
authored
Merge pull request #23 from cortex-cms/improve-pagination-result
Improve pagination on result object
2 parents a7cc0e2 + feae3a2 commit ef5e006

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Version History
33
* All Version bumps are required to update this file as well!!
44
----
55

6+
* 0.9.0 - Add total pages, next page, and prev page to result object
67
* 0.8.0 - Revert ETag caching flow implementation
78
* 0.7.0 - Implement ability to specify OAuth scopes
89
* 0.6.1 - Update various dependencies

lib/cortex/result.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
require 'hashie'
22
module Cortex
33
class Result
4-
attr_reader :raw_headers, :contents, :total_items, :page, :per_page, :errors, :range_start, :range_end, :range, :status
4+
attr_reader :raw_headers, :contents, :total_items, :page, :per_page, :errors, :range_start, :range_end, :range, :status, :total_pages, :next_page, :prev_page
55

66
def initialize(body, headers, status)
77
@contents = parse(body)
88
@raw_headers = headers
99
@status = status
10-
@total_items = headers['x-total-items'] unless headers['x-total-items'].nil?
1110
parse_headers(headers)
1211
@errors = find_errors
1312
end
@@ -20,15 +19,16 @@ def is_error?
2019

2120
def parse_headers(headers)
2221
if headers['X-Total']
22+
@total_items = headers['X-Total']
2323
@count = headers['X-Total'].to_i
24-
end
25-
if headers['X-Total']
2624
@page = headers['X-Page'].to_i
2725
@per_page = headers['X-Per-Page'].to_i
2826
@range_start = (@page-1) * @per_page
2927
@range_end = @per_page * @page - 1
3028
@range = "#{@range_start}-#{@range_end}"
31-
29+
@total_pages = headers['X-Total-Pages']
30+
@next_page = headers['X-Next-Page']
31+
@prev_page = headers['X-Prev-Page']
3232
end
3333
end
3434

lib/cortex/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Cortex
2-
VERSION = '0.8.0'
2+
VERSION = '0.9.0'
33
end

spec/result_spec.rb

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'spec_helper'
22

33
RSpec.describe Cortex::Result do
4-
let(:result) { Cortex::Result.new('body', {'X-Total' => 10, 'X-Page' => "1", "X-Per-Page" => "10"}, 200) }
4+
let(:result) { Cortex::Result.new('body', { 'X-Total' => '40', 'X-Page' => "1", 'X-Per-Page' => "10", 'X-Total-Pages' => '4', 'X-Next-Page' => '2', 'X-Prev-Page' => nil }, 200) }
55
let(:failed) { Cortex::Result.new('failed body', {}, 403) }
66

77
it 'should construct' do
@@ -31,7 +31,7 @@
3131
end
3232

3333
it 'should expose the headers' do
34-
expect(result.raw_headers).to eq({ 'X-Total' => 10, 'X-Page' => "1", "X-Per-Page" => "10" })
34+
expect(result.raw_headers).to eq({ 'X-Total' => '40', 'X-Page' => "1", 'X-Per-Page' => "10", 'X-Total-Pages' => '4', 'X-Next-Page' => '2', 'X-Prev-Page' => nil })
3535
expect(failed.raw_headers).to eq({})
3636
end
3737

@@ -40,4 +40,28 @@
4040
expect(failed.status).to eq 403
4141
end
4242

43+
describe '#total_pages' do
44+
it 'returns the value of "X-Total-Pages"' do
45+
expect(result.total_pages).to eq '4'
46+
end
47+
end
48+
49+
describe '#next_page' do
50+
it 'returns_the_value of "X-Next-Page"' do
51+
expect(result.next_page).to eq '2'
52+
end
53+
end
54+
55+
describe '#prev_page' do
56+
it 'returns nil when on the first page' do
57+
expect(result.prev_page).to be_nil
58+
end
59+
60+
context 'on the 2nd page' do
61+
let(:result) { Cortex::Result.new('body', { 'X-Total' => '40', 'X-Page' => "2", 'X-Per-Page' => "10", 'X-Total-Pages' => '4', 'X-Next-Page' => '3', 'X-Prev-Page' => '1' }, 200) }
62+
it 'returns the value if "X-Prev-Page"' do
63+
expect(result.prev_page).to eq '1'
64+
end
65+
end
66+
end
4367
end

0 commit comments

Comments
 (0)