-
Notifications
You must be signed in to change notification settings - Fork 9
/
app.rb
209 lines (178 loc) · 5.45 KB
/
app.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
%w[ lib ].each do |path|
$:.unshift path unless $:.include?(path)
end
require 'sinatra'
require 'slimmer'
require 'erubis'
require 'json'
require 'csv'
require 'popular_items'
require 'document'
require 'section'
require 'utils'
require 'solr_wrapper'
require 'slimmer_headers'
require 'sinatra/content_for'
require_relative 'helpers'
require_relative 'config'
def solr
@solr ||= SolrWrapper.new(DelSolr::Client.new(settings.solr), settings.recommended_format)
end
helpers do
include Helpers
end
before do
headers SlimmerHeaders.headers(settings.slimmer_headers)
end
def prefixed_path(path)
path_prefix = settings.router[:path_prefix]
raise "Path prefix must start with /" unless path_prefix.blank? || path_prefix =~ /^\//
"#{path_prefix}#{path}"
end
get prefixed_path("/search.?:format?") do
if params['q'].nil? or params['q'].strip == ''
expires 3600, :public
@page_section = "Search"
@page_section_link = "/search"
@page_title = "Search | GOV.UK"
return erb(:no_search_term)
end
@query = params['q']
expires 3600, :public if @query.length < 20
@results = solr.search(@query)
if request.accept.include?("application/json") or params['format'] == 'json'
content_type :json
JSON.dump(@results.map { |r| r.to_hash.merge(highlight: r.highlight) })
else
@page_section = "Search"
@page_section_link = "/search"
@page_title = "#{@query} | Search | GOV.UK"
if @results.any?
erb(:search)
else
erb(:no_search_results)
end
end
end
get prefixed_path("/preload-autocomplete") do
# Eventually this is likely to be a list of commonly searched for terms
# so searching for those is really fast. For the beta, this is just a list
# of all terms.
expires 86400, :public
content_type :json
results = solr.autocomplete_cache rescue []
JSON.dump(results.map { |r| r.to_hash })
end
get prefixed_path("/autocomplete") do
content_type :json
query = params['q']
unless query
expires 86400, :public
return '[]'
end
expires 3600, :public if query.length < 5
results = solr.complete(query) rescue []
JSON.dump(results.map { |r| r.to_hash })
end
get prefixed_path("/sitemap.xml") do
expires 86400, :public
# Site maps can have up to 50,000 links in them.
# We use one for / so we can have up to 49,999 others.
documents = solr.all_documents limit: 49_999
builder do |xml|
xml.instruct!
xml.urlset(xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9") do
xml.url do
xml.loc "#{base_url}#{prefixed_path("/")}"
end
documents.each do |document|
xml.url do
url = document.link
url = "#{base_url}#{url}" if url =~ /^\//
xml.loc url
end
end
end
end
end
if settings.router[:path_prefix].empty?
get prefixed_path("/browse.?:format?") do
expires 3600, :public
@results = solr.facet('section')
@page_section = "Browse"
@page_section_link = "/browse"
@page_title = "Browse | GOV.UK"
if request.accept.include?("application/json") or params['format'] == 'json'
content_type :json
JSON.dump(@results.map { |r| { url: "/browse/#{r.slug}" } })
else
erb(:sections)
end
end
def assemble_section_details(section_slug)
section = params[:section].gsub(/[^a-z0-9\-_]+/, '-')
halt 404 unless section == params[:section]
@ungrouped_results = solr.section(section)
halt 404 if @ungrouped_results.empty?
@ungrouped_results[0].subsection = nil
@section = Section.new(section)
@page_section = formatted_section_name(@section.slug)
@page_section_link = @section.path
@results = @ungrouped_results.group_by { |result| result.subsection }.sort {|l,r| l[0].nil? ? 1 : l[0]<=>r[0]}
end
def compile_section_json(results)
as_hash = {
'name' => @page_section,
'url' => @page_section_link,
'contents' => {}
}
description_path = File.expand_path("../views/_#{@section.slug}.html", __FILE__)
if File.exists?(description_path)
as_hash['description'] = File.read(description_path).gsub(/<\/?[^>]*>/, "")
end
@results.each do |subsection, items|
as_hash['contents'][subsection] = items.collect do |i|
{ 'title' => i.title, 'url' => i.link, 'format' => i.presentation_format}
end
end
as_hash
end
get prefixed_path("/browse/:section.json") do
expires 86400, :public
assemble_section_details(params[:section])
content_type :json
JSON.dump(compile_section_json(@results))
end
get prefixed_path("/browse/:section") do
expires 86400, :public
assemble_section_details(params[:section])
if request.accept.include?("application/json")
content_type :json
JSON.dump(compile_json_for_section)
else
popular_items = PopularItems.new(settings.popular_items_file)
@popular = popular_items.select_from(params[:section], @ungrouped_results)
@page_title = "#{formatted_section_name @section.slug} | GOV.UK"
erb(:section)
end
end
end
post prefixed_path("/documents") do
request.body.rewind
documents = [JSON.parse(request.body.read)].flatten.map { |hash|
Document.from_hash(hash)
}
boosts = {}
CSV.foreach(settings.boost_csv) { |row|
link, phrases = row
boosts[link] = phrases
}
better_documents = boost_documents(documents, boosts)
simple_json_result(solr.add(better_documents))
end
post prefixed_path("/commit") do
simple_json_result(solr.commit)
end
delete prefixed_path("/documents/*") do
simple_json_result(solr.delete(params["splat"].first))
end