-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DEV-994 OFFSET and LIMIT Queries (#8)
* DEV-994 Rights API: OFFSET and LIMIT Queries - Add `QueryParser` class. - Process CGI parameters and Integrate `id` queries. - Remove `wait-for` from Dockerfile and docker-compose - Use docker-compose healthchecks, remove `test` service - Rename test.yml to tests.yml - Add millisecond timer and result field for Sequel operations.
- Loading branch information
Showing
18 changed files
with
309 additions
and
47 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,7 @@ jobs: | |
run: docker compose run web bundle exec standardrb | ||
|
||
- name: Run tests | ||
run: docker compose run test | ||
run: docker compose run web bundle exec rspec | ||
|
||
- name: Report to Coveralls | ||
uses: coverallsapp/[email protected] | ||
|
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
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
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,8 @@ | ||
# frozen_string_literal: true | ||
|
||
module RightsAPI | ||
class QueryParserError < StandardError | ||
# This is raised whenever the query parser determines the query is ill-formed. | ||
# Should result in a 400 Bad Request response | ||
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
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,84 @@ | ||
# frozen_string_literal: true | ||
|
||
require "sequel" | ||
|
||
require_relative "error" | ||
|
||
# Processes the Hash of URL parameters passed to the API into an | ||
# Array of WHERE constraints, as well as LIMIT, and OFFSET values. | ||
# ORDER BY (other than the schema default) will be handled here | ||
# in a future iteration. | ||
module RightsAPI | ||
class QueryParser | ||
DEFAULT_LIMIT = 1000 | ||
DEFAULT_OFFSET = 0 | ||
attr_reader :params, :model, :where, :order, :offset, :limit | ||
|
||
# @param model [Class] Sequel::Model subclass for the table being queried | ||
def initialize(model:) | ||
@model = model | ||
@where = [] | ||
@order = [] | ||
@limit = DEFAULT_LIMIT | ||
@offset = DEFAULT_OFFSET | ||
end | ||
|
||
def parse(params: {}) | ||
@params = params | ||
params.each do |key, values| | ||
key = key.to_sym | ||
case key | ||
when :offset | ||
parse_offset(values: values) | ||
when :limit | ||
parse_limit(values: values) | ||
else | ||
parse_parameter(key: key, values: values) | ||
end | ||
end | ||
@order = [model.default_order] if @order.empty? | ||
self | ||
end | ||
|
||
private | ||
|
||
# Parses a general search parameter and appends the resulting Sequel | ||
# expression to the @where array. | ||
# Currently only handles primary key searches. | ||
# Example: a URL ending with ?id=1 results in: | ||
# parse_parameter(key: :id, values: [1]) | ||
# @param key [Symbol] The search parameter | ||
# @param values [Array] One or more parameter values | ||
def parse_parameter(key:, values:) | ||
values.each do |value| | ||
@where << {model.query_for_field(field: key.to_sym) => value} | ||
end | ||
end | ||
|
||
# Extract a single integer that can be passed to dataset.offset. | ||
def parse_offset(values:) | ||
@offset = parse_int_value(values: values, type: "OFFSET") | ||
end | ||
|
||
# Extract a single integer that can be passed to dataset.limit. | ||
# @param values [Array] One or more limit values | ||
def parse_limit(values:) | ||
@limit = parse_int_value(values: values, type: "LIMIT") | ||
end | ||
|
||
# Extract integer offset= or limit= value from potentially multiple values. | ||
# If multiple values, use the last. | ||
# @param values [Array] One or more offset or limit values | ||
# @param type [String] "OFFSET" or "LIMIT", used only for reporting errors. | ||
# @return [Integer] | ||
def parse_int_value(values:, type:) | ||
value = values.last.to_i | ||
# Make sure the offset can make a round-trip conversion between Int and String | ||
# https://stackoverflow.com/a/1235891 | ||
if value.to_s != values.last | ||
raise QueryParserError, "#{type} expression '#{values.last}' is not an integer (#{value.to_s.class} vs #{values.last.class})" | ||
end | ||
value | ||
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
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,18 @@ | ||
# frozen_string_literal: true | ||
|
||
module RightsAPI | ||
class ErrorResult < Result | ||
def initialize(exception:) | ||
super(offset: 0, total: 0) | ||
@exception = exception | ||
end | ||
|
||
private | ||
|
||
def finalize(hash) | ||
# FIXME: should we include the backtrace or just the message? | ||
hash[:error] = @exception.to_s + " #{@exception.backtrace}" | ||
hash | ||
end | ||
end | ||
end |
Oops, something went wrong.