Skip to content

Commit 561f816

Browse files
committed
Merge pull request #8 from cbdr/topic/535-pagination-helper
Adding support for new cortex pagination style caused by grape-kaminari
2 parents 638c90b + 9a9fa9c commit 561f816

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

cortex-client-0.4.0.gem

9.5 KB
Binary file not shown.

lib/cortex/result.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ def is_error?
1919
private
2020

2121
def parse_headers(headers)
22-
if headers['x-total-items']
23-
@count = headers['x-total-items']
22+
if headers['X-Total']
23+
@count = headers['X-Total'].to_i
2424
end
25-
if headers['content-range']
26-
matches = headers['content-range'].match(/^(\w+) (\d+)\-(\d+):(\d+)\/\d+$/i)
27-
@per_page = matches[4].to_i
28-
@range_start = matches[2].to_i
29-
@range_end = matches[3].to_i
25+
if headers['X-Total']
26+
@page = headers['X-Page'].to_i
27+
@per_page = headers['X-Per-Page'].to_i
28+
@range_start = (@page-1) * @per_page
29+
@range_end = @per_page * @page - 1
3030
@range = "#{@range_start}-#{@range_end}"
31-
@page = (@range_end / @per_page) + 1
31+
3232
end
3333
end
3434

spec/result_spec.rb

Lines changed: 3 additions & 3 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-items' => 10, 'content-range' => "posts 0-9:10/200"}, 200) }
4+
let(:result) { Cortex::Result.new('body', {'X-Total' => 10, 'X-Page' => "1", "X-Per-Page" => "10"}, 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-items' => 10, 'content-range' => "posts 0-9:10/200" })
34+
expect(result.raw_headers).to eq({ 'X-Total' => 10, 'X-Page' => "1", "X-Per-Page" => "10" })
3535
expect(failed.raw_headers).to eq({})
3636
end
3737

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

43-
end
43+
end

0 commit comments

Comments
 (0)