Skip to content
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

Validate limit if offset is specified #90

Merged
merged 1 commit into from
Sep 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/api/links_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def initialize(params, href, counts)
@limit = params["limit"].to_i if params["limit"]
@href = href
@counts = counts if counts
validate_limit
end

def links
Expand All @@ -29,6 +30,10 @@ def links?

attr_reader :offset, :limit, :href, :counts

def validate_limit
raise BadRequestError, "Limit must be greater than zero if offset is specified" if links? && limit.zero?
end

def self_href
@self_href ||= format_href(offset)
end
Expand Down
8 changes: 8 additions & 0 deletions spec/lib/api/links_builder_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
RSpec.describe Api::LinksBuilder do
describe ".new" do
it "validates limit is not zero when offset is also specified" do
expect do
described_class.new({"offset" => 5, "limit" => 0}, '/api/collection/', nil)
end.to raise_error(Api::BadRequestError, "Limit must be greater than zero if offset is specified")
end
end

describe "#links" do
it "returns self, first, next, and last links when it is the first page" do
offsets = { "offset" => 0, "limit" => 2 }
Expand Down