Class: RequestHandler::SortOptionHandler
Instance Method Summary
collapse
#initialize
Instance Method Details
#allowed_option(name) ⇒ Object
36
37
38
39
40
|
# File 'lib/request_handler/sort_option_handler.rb', line 36
def allowed_option(name)
allowed_options_type&.call(name)
rescue Dry::Types::ConstraintError
raise OptionNotAllowedError, name.to_sym => 'is not an allowed sort option'
end
|
#duplicates?(options) ⇒ Boolean
42
43
44
|
# File 'lib/request_handler/sort_option_handler.rb', line 42
def duplicates?(options)
!options.uniq!(&:field).nil?
end
|
#fetch_options ⇒ Object
14
15
16
17
|
# File 'lib/request_handler/sort_option_handler.rb', line 14
def fetch_options
raise ExternalArgumentError, sort_options: 'the query paramter must not be empty' if empty_param?('sort')
params.fetch('sort') { '' }.split(',')
end
|
#parse_option(option) ⇒ Object
27
28
29
30
31
32
33
34
|
# File 'lib/request_handler/sort_option_handler.rb', line 27
def parse_option(option)
raise ExternalArgumentError, sort_options: 'must not contain a space' if option.include? ' '
if option.start_with?('-')
[option[1..-1], :desc]
else
[option, :asc]
end
end
|
#parse_options(options) ⇒ Object
19
20
21
22
23
24
25
|
# File 'lib/request_handler/sort_option_handler.rb', line 19
def parse_options(options)
options.map do |option|
name, order = parse_option(option)
allowed_option(name)
SortOption.new(name, order)
end
end
|
#run ⇒ Object
7
8
9
10
11
12
|
# File 'lib/request_handler/sort_option_handler.rb', line 7
def run
return [] unless params.key?('sort')
sort_options = parse_options(fetch_options)
raise ExternalArgumentError, sort_options: 'must be unique' if duplicates?(sort_options)
sort_options
end
|