/
elasticsearch_presenter.rb
285 lines (264 loc) · 12.8 KB
/
elasticsearch_presenter.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
module GovukIndex
class ElasticsearchPresenter
include ElasticsearchIdentity
def initialize(payload:, type_mapper:)
@payload = payload
@inferred_type = type_mapper
end
def type
@type ||= @inferred_type.type
end
def document
{
ai_assurance_technique: specialist.ai_assurance_technique,
aircraft_category: specialist.aircraft_category,
aircraft_type: specialist.aircraft_type,
alert_type: specialist.alert_type,
algorithmic_transparency_record_atrs_version: specialist.algorithmic_transparency_record_atrs_version,
algorithmic_transparency_record_capability: specialist.algorithmic_transparency_record_capability,
algorithmic_transparency_record_date_published: specialist.algorithmic_transparency_record_date_published,
algorithmic_transparency_record_function: specialist.algorithmic_transparency_record_function,
algorithmic_transparency_record_organisation: specialist.algorithmic_transparency_record_organisation,
algorithmic_transparency_record_organisation_type: specialist.algorithmic_transparency_record_organisation_type,
algorithmic_transparency_record_other_tags: specialist.algorithmic_transparency_record_other_tags,
algorithmic_transparency_record_phase: specialist.algorithmic_transparency_record_phase,
algorithmic_transparency_record_region: specialist.algorithmic_transparency_record_region,
algorithmic_transparency_record_task: specialist.algorithmic_transparency_record_task,
areas_of_interest: specialist.areas_of_interest,
assessment_date: specialist.assessment_date,
assurance_technique_approach: specialist.assurance_technique_approach,
attachments: common_fields.attachments,
authors: specialist.authors,
business_sizes: specialist.business_sizes,
business_stages: specialist.business_stages,
case_state: specialist.case_state,
case_type: specialist.case_type,
category: specialist.category,
certificate_status: specialist.certificate_status,
class_category: specialist.class_category,
closed_date: specialist.closed_date,
closing_date: specialist.closing_date,
commodity_type: specialist.commodity_type,
contact_groups: details.contact_groups,
content_id: common_fields.content_id,
content_purpose_subgroup: common_fields.content_purpose_subgroup,
content_purpose_supergroup: common_fields.content_purpose_supergroup,
content_store_document_type: common_fields.content_store_document_type,
continuation_link: specialist.continuation_link,
country: specialist.country,
country_of_origin: specialist.country_of_origin,
date_application: specialist.date_application,
date_of_completion: specialist.date_of_completion,
date_of_occurrence: specialist.date_of_occurrence,
date_of_start: specialist.date_of_start,
date_registration: specialist.date_registration,
date_registration_eu: specialist.date_registration_eu,
decision_subject: specialist.decision_subject,
description: common_fields.description,
destination_country: specialist.destination_country,
development_sector: specialist.development_sector,
digital_market_research_area: specialist.digital_market_research_area,
digital_market_research_category: specialist.digital_market_research_category,
digital_market_research_publish_date: specialist.digital_market_research_publish_date,
digital_market_research_publisher: specialist.digital_market_research_publisher,
digital_market_research_topic: specialist.digital_market_research_topic,
disease_case_closed_date: specialist.disease_case_closed_date,
disease_case_opened_date: specialist.disease_case_opened_date,
disease_type: specialist.disease_type,
document_type: type,
eligible_entities: specialist.eligible_entities,
email_document_supertype: common_fields.email_document_supertype,
first_published_at: specialist.first_published_at,
flood_and_coastal_erosion_category: specialist.flood_and_coastal_erosion_category,
focus_sector: specialist.focus_sector,
format: common_fields.format,
fund_state: specialist.fund_state,
fund_type: specialist.fund_type,
funding_amount: specialist.funding_amount,
funding_source: specialist.funding_source,
government_document_supertype: common_fields.government_document_supertype,
government_name: common_fields.government_name,
grant_type: specialist.grant_type,
grant_schemes: specialist.grant_schemes,
hidden_indexable_content: specialist.hidden_indexable_content,
hmrc_manual_section_id: common_fields.section_id,
image_url:,
indexable_content: indexable.indexable_content,
industries: specialist.industries,
internal_notes: specialist.internal_notes,
is_historic: common_fields.historic?,
is_political: common_fields.political?,
is_withdrawn: common_fields.withdrawn?,
issued_date: specialist.issued_date,
keyword: specialist.keyword,
key_function: specialist.key_function,
laid_date: specialist.laid_date,
land_use: specialist.land_use,
land_types: specialist.land_types,
latest_change_note: details.latest_change_note,
licence_identifier: details.licence_identifier,
licence_transaction_continuation_link: specialist.licence_transaction_continuation_link,
licence_transaction_industry: specialist.licence_transaction_industry,
licence_transaction_licence_identifier: specialist.licence_transaction_licence_identifier,
licence_transaction_location: specialist.licence_transaction_location,
licence_transaction_will_continue_on: specialist.licence_transaction_will_continue_on,
licence_short_description: details.licence_short_description,
link: common_fields.link,
location: specialist.location,
mainstream_browse_page_content_ids: expanded_links.mainstream_browse_page_content_ids,
mainstream_browse_pages: expanded_links.mainstream_browse_pages,
manual: details.parent_manual,
marine_notice_topic: specialist.marine_notice_topic,
marine_notice_type: specialist.marine_notice_type,
marine_notice_vessel_type: specialist.marine_notice_vessel_type,
market_sector: specialist.market_sector,
medical_specialism: specialist.medical_specialism,
opened_date: specialist.opened_date,
organisation_content_ids: expanded_links.organisation_content_ids,
organisations: expanded_links.organisations,
outcome_type: specialist.outcome_type,
part_of_taxonomy_tree: expanded_links.part_of_taxonomy_tree,
parts: parts.presented_parts,
payment_types: specialist.payment_types,
people: expanded_links.people,
policy_groups: expanded_links.policy_groups,
popularity: common_fields.popularity,
popularity_b: common_fields.popularity_b,
primary_publishing_organisation: expanded_links.primary_publishing_organisation,
principle: specialist.principle,
product_alert_type: specialist.product_alert_type,
product_category: specialist.product_category,
product_measure_type: specialist.product_measure_type,
product_recall_alert_date: specialist.product_recall_alert_date,
product_risk_level: specialist.product_risk_level,
project_code: specialist.project_code,
project_status: specialist.project_status,
protection_type: specialist.protection_type,
public_timestamp: common_fields.public_timestamp,
publishing_app: common_fields.publishing_app,
railway_type: specialist.railway_type,
reason_for_protection: specialist.reason_for_protection,
reference_number: specialist.reference_number,
regions: specialist.regions,
register: specialist.register,
registered_name: specialist.registered_name,
registration: specialist.registration,
rendering_app: common_fields.rendering_app,
report_type: specialist.report_type,
research_document_type: specialist.research_document_type,
result: specialist.result,
review_status: specialist.review_status,
role_appointments: expanded_links.role_appointments,
roles: expanded_links.roles,
sector: specialist.sector,
service_provider: specialist.service_provider,
sift_end_date: specialist.sift_end_date,
sifting_status: specialist.sifting_status,
slug:,
stage: specialist.stage,
status: specialist.status,
subject: specialist.subject,
taxons: expanded_links.taxons,
theme: specialist.theme,
therapeutic_area: specialist.therapeutic_area,
tiers_or_standalone_items: specialist.tiers_or_standalone_items,
time_registration: specialist.time_registration,
title: common_fields.title,
topical_events: expanded_links.topical_events,
topics: specialist.topics,
traditional_term_grapevine_product_category: specialist.traditional_term_grapevine_product_category,
traditional_term_language: specialist.traditional_term_language,
traditional_term_type: specialist.traditional_term_type,
tribunal_decision_categories: specialist.tribunal_decision_categories,
tribunal_decision_category: specialist.tribunal_decision_category,
tribunal_decision_country: specialist.tribunal_decision_country,
tribunal_decision_decision_date: specialist.tribunal_decision_decision_date,
tribunal_decision_judges: specialist.tribunal_decision_judges,
tribunal_decision_landmark: specialist.tribunal_decision_landmark,
tribunal_decision_reference_number: specialist.tribunal_decision_reference_number,
tribunal_decision_sub_categories: specialist.tribunal_decision_sub_categories,
tribunal_decision_sub_category: specialist.tribunal_decision_sub_category,
types_of_support: specialist.types_of_support,
updated_at: common_fields.updated_at,
use_case: specialist.use_case,
user_journey_document_supertype: common_fields.user_journey_document_supertype,
value_of_funding: specialist.value_of_funding,
vessel_type: specialist.vessel_type,
view_count: common_fields.view_count,
virus_strain: specialist.virus_strain,
will_continue_on: specialist.will_continue_on,
withdrawn_date: specialist.withdrawn_date,
world_locations: expanded_links.world_locations,
year_adopted: specialist.year_adopted,
zone_restriction: specialist.zone_restriction,
zone_type: specialist.zone_type,
}.reject { |_, v| v.nil? }
end
def updated_at
common_fields.updated_at
end
def format
common_fields.format
end
def base_path
common_fields.base_path
end
def link
common_fields.link
end
def publishing_app
common_fields.publishing_app
end
def valid!
if format == "recommended-link"
details.url || raise(MissingExternalUrl, "url missing from details section")
else
base_path || raise(NotIdentifiable, "base_path missing from payload")
end
end
def image_url
details.image_url || (expanded_links.default_news_image if newslike?)
end
private
attr_reader :payload
def indexable
IndexableContentPresenter.new(
format: common_fields.format,
details: payload["details"],
sanitiser: IndexableContentSanitiser.new,
)
end
def slug
case format
when "mainstream_browse_page"
base_path.gsub(%r{^/browse/}, "")
when "policy"
base_path.gsub(%r{^/government/policies/}, "")
when "person"
base_path.gsub(%r{^/government/people/}, "")
when "ministerial_role"
base_path.gsub(%r{^/government/ministers/}, "")
end
end
def common_fields
@common_fields ||= CommonFieldsPresenter.new(payload)
end
def details
@details ||= DetailsPresenter.new(details: payload["details"], format: common_fields.format)
end
def parts
@parts ||= PartsPresenter.new(parts: payload["details"].fetch("parts", []))
end
def expanded_links
@expanded_links ||= ExpandedLinksPresenter.new(payload["expanded_links"])
end
def specialist
@specialist ||= SpecialistPresenter.new(payload)
end
def newslike?
return false if common_fields.content_store_document_type == "fatality_notice"
common_fields.content_purpose_subgroup == "news" ||
common_fields.content_purpose_subgroup == "speeches_and_statements"
end
end
end