-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pagination using Range header #234
Open
gudmundur
wants to merge
13
commits into
main
Choose a base branch
from
http-ranges
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8464905
Add RangeParser with attributes
gudmundur b967f70
Add test with expected range parser behavior
gudmundur 99610c1
Include RangeParser in Pliny
gudmundur 774c81d
Handle empty range header
gudmundur 4b89829
Raise on multiple semicolons
gudmundur 968fb2a
Add methods to parse bounds and parameters
gudmundur da27407
Raise on non "objects" unit
gudmundur 7cebe3c
Parse start and end bounds
gudmundur 9499700
Parse range parameters
gudmundur cfbca69
Check for empty parameters when absent
gudmundur 783b649
styling
gudmundur a252ca4
Clean up start and end bound parsing
gudmundur b8aeda6
Assert against unbound start bound
gudmundur File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
module Pliny | ||
class RangeParser | ||
attr_reader :range_header | ||
attr_reader :start, :end, :parameters | ||
|
||
RANGE_FORMAT_ERROR = 'Invalid `Range` header. Please use format like `objects 0-99; sort=name, order=desc`.'.freeze | ||
|
||
def initialize(range_header) | ||
@range_header = range_header | ||
|
||
set_defaults | ||
return if range_header.nil? | ||
parse | ||
end | ||
|
||
private | ||
|
||
def parse | ||
parts = range_header.split(';') | ||
raise_range_format_error if parts.size > 2 | ||
bounds_str, parameters_str = parts | ||
parse_range_bounds(bounds_str) | ||
parse_range_parameters(parameters_str) | ||
end | ||
|
||
def parse_range_bounds(bounds_str) | ||
return if bounds_str.nil? | ||
unit, bounds = bounds_str.split(/\s+/, 2) | ||
raise_range_format_error unless unit.downcase == 'objects' | ||
/(?<start_bound>\d*)-(?<end_bound>\d*)/ =~ bounds | ||
@start = start_bound.to_i unless start_bound.empty? | ||
@end = end_bound.to_i unless end_bound.empty? | ||
end | ||
|
||
def parse_range_parameters(parameters_str) | ||
return if parameters_str.nil? | ||
@parameters = Hash[ | ||
parameters_str.split(',') | ||
.map { |option| option.split('=') } | ||
.select { |k, v| k && v } | ||
.map { |k, v| [k.strip.to_sym, v.strip] } | ||
] | ||
end | ||
|
||
def raise_range_format_error | ||
fail Pliny::Errors::BadRequest, RANGE_FORMAT_ERROR | ||
end | ||
|
||
def set_defaults | ||
@start = nil | ||
@end = nil | ||
@parameters = {} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
require "spec_helper" | ||
|
||
describe Pliny::RangeParser do | ||
subject(:parser) { described_class.new(range_header) } | ||
|
||
context 'with an empty header' do | ||
let(:range_header) { nil } | ||
|
||
it 'parses' do | ||
assert_nil parser.start | ||
assert_nil parser.end | ||
assert_equal({}, parser.parameters) | ||
end | ||
end | ||
|
||
context 'with a bound range' do | ||
let(:range_header) { 'objects 0-99' } | ||
|
||
it 'parses a start and an end' do | ||
assert_equal 0, parser.start | ||
assert_equal 99, parser.end | ||
assert_equal({}, parser.parameters) | ||
end | ||
end | ||
|
||
context 'with an unbound start range' do | ||
let(:range_header) { 'objects -99' } | ||
|
||
it 'parses a start' do | ||
assert_nil parser.start | ||
assert_equal 99, parser.end | ||
assert_equal({}, parser.parameters) | ||
end | ||
end | ||
|
||
context 'with an unbound end range' do | ||
let(:range_header) { 'objects 0-' } | ||
|
||
it 'parses a start' do | ||
assert_equal 0, parser.start | ||
assert_nil parser.end | ||
assert_equal({}, parser.parameters) | ||
end | ||
end | ||
|
||
context 'with parameters' do | ||
let(:range_header) { 'objects 0-99; a=b, c=d' } | ||
|
||
it 'parses parameters' do | ||
assert_equal({ a: 'b', c: 'd' }, parser.parameters) | ||
end | ||
end | ||
|
||
context 'with multiple semicolons' do | ||
let(:range_header) { 'objects 0-99; a=b; c=d' } | ||
let(:message) { Pliny::RangeParser::RANGE_FORMAT_ERROR } | ||
|
||
it 'raises a bad request' do | ||
assert_raises Pliny::Errors::BadRequest, message do | ||
parser | ||
end | ||
end | ||
end | ||
|
||
context 'with a non objects unit' do | ||
let(:range_header) { 'ids 0-99' } | ||
let(:message) { Pliny::RangeParser::RANGE_FORMAT_ERROR } | ||
|
||
it 'raises a bad request' do | ||
assert_raises Pliny::Errors::BadRequest, message do | ||
parser | ||
end | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@geemus Do you think unbound ranges should be allowed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gudmundur I think they are in the API already!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not absolutely required, but certainly nice to have.