-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathorganisation_importer.rb
69 lines (57 loc) · 2.02 KB
/
organisation_importer.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
require 'gds_api/organisations'
class OrganisationImporter
def run
logger.info "Fetching all organisations from the Organisation API..."
organisations = organisations_api.organisations.with_subsequent_pages.to_a
logger.info "Loaded #{organisations.size} organisations"
organisations.each do |organisation|
create_or_update_organisation(organisation)
end
logger.info "Import complete"
end
private
def create_or_update_organisation(organisation_from_api)
organisation_attrs = {
title: organisation_from_api['title'],
slug: organisation_from_api['details']['slug'],
acronym: organisation_from_api['details']['abbreviation'],
govuk_status: organisation_from_api['details']['govuk_status'],
web_url: organisation_from_api['web_url'],
content_id: organisation_from_api['details']['content_id'],
}
content_id = organisation_from_api['details']['content_id']
existing_organisation = Organisation.find_by(content_id: content_id)
if existing_organisation.present?
existing_organisation.update_attributes(organisation_attrs)
logger.info "Updated #{existing_organisation.title}"
else
Organisation.create!(organisation_attrs)
logger.info "Created #{organisation_attrs[:title]}"
end
end
def logger
@logger ||= build_logger
end
def build_logger
output = case Rails.env
when "development" then STDOUT
when "test" then "/dev/null"
when "production" then Rails.root.join("log", "organisation_import.json.log")
end
Logger.new(output).tap {|logger|
logger.formatter = json_log_formatter if Rails.env.production?
}
end
def json_log_formatter
proc {|severity, datetime, progname, message|
{
"@message" => message,
"@tags" => ["cron", "rake"],
"@timestamp" => datetime.iso8601
}.to_json + "\n"
}
end
def organisations_api
@api_client ||= GdsApi::Organisations.new(Plek.current.find('whitehall-admin'))
end
end