This repository has been archived by the owner on Feb 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgovuk_content_api.rb
92 lines (75 loc) · 1.74 KB
/
govuk_content_api.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
require 'sinatra'
require 'plek'
require_relative "config"
require 'config/gds_sso_middleware'
class GovUkContentApi < Sinatra::Application
DEFAULT_CACHE_TIME = 15.minutes.to_i
LONG_CACHE_TIME = 1.hour.to_i
ERROR_CODES = {
401 => "unauthorised",
403 => "forbidden",
404 => "not found",
410 => "gone",
422 => "unprocessable",
503 => "unavailable"
}
set :views, File.expand_path('views', File.dirname(__FILE__))
set :show_exceptions, false
set :protection, except: :path_traversal
def set_expiry(duration = DEFAULT_CACHE_TIME, options = {})
visibility = options[:private] ? :private : :public
expires(duration, visibility)
end
before do
content_type :json
end
get '/healthcheck' do
status 200
body 'OK'
end
get "/local_authorities.json" do
set_expiry LONG_CACHE_TIME
custom_410
end
get "/local_authorities/:snac.json" do
set_expiry LONG_CACHE_TIME
custom_410
end
get "/tags.json" do
set_expiry LONG_CACHE_TIME
custom_410
end
get "/tag_types.json" do
set_expiry LONG_CACHE_TIME
custom_410
end
get "/tags/:tag_type_or_id.json" do
set_expiry LONG_CACHE_TIME
custom_410
end
get "/tags/:tag_type/*.json" do |tag_type, tag_id|
set_expiry LONG_CACHE_TIME
custom_410
end
get "/with_tag.json" do
set_expiry LONG_CACHE_TIME
custom_410
end
get "/*.json" do |id|
set_expiry LONG_CACHE_TIME
custom_410
end
protected
def custom_410
custom_error 410, "This item is no longer available"
end
def custom_error(code, message)
error_hash = {
"_response_info" => {
"status" => ERROR_CODES.fetch(code),
"status_message" => message
}
}
halt code, error_hash.to_json
end
end