forked from sistephens/finder-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application_controller.rb
71 lines (58 loc) · 1.89 KB
/
application_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# typed: false
class ApplicationController < ActionController::Base
include Slimmer::Headers
include Slimmer::Template
slimmer_template "header_footer_only"
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
helper :application
# rescue_from precedence is bottom up - https://stackoverflow.com/a/9121054/170864
rescue_from GdsApi::BaseError, with: :error_503
rescue_from GdsApi::InvalidUrl, with: :unprocessable_entity
rescue_from GdsApi::HTTPNotFound, with: :error_not_found
rescue_from GdsApi::HTTPUnprocessableEntity, with: :unprocessable_entity
private
def error_503(exception)
error(503, exception)
end
def error(status_code, exception = nil)
if exception
GovukError.notify(exception)
end
render status: status_code, plain: "#{status_code} error"
end
def finder_base_path
"/#{finder_slug}"
end
def finder_slug
params[:slug]
end
def error_not_found
render status: :not_found, plain: "404 error not found"
end
def unprocessable_entity
render status: :unprocessable_entity, plain: "422 error: unprocessable entity"
end
def filter_params
# TODO Use a whitelist based on the facets in the schema
@filter_params ||= begin
permitted_params = params
.to_unsafe_hash
.except(
:controller,
:action,
:slug,
:format
)
# Convert a query with 'q=search_term' into 'keywords=search_term'
if permitted_params.has_key?("q")
permitted_params["keywords"] = permitted_params.delete("q")
end
ParamsCleaner
.new(permitted_params)
.cleaned
.delete_if { |_, value| value.blank? }
end
end
end