-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.rb
83 lines (77 loc) · 3.02 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
require 'bundler/setup'
Bundler.require
Dotenv.load
require 'open-uri'
require 'json'
require 'pry'
require 'active_support/core_ext'
require 'fileutils'
class RebuildLegislatureFiles
include Sidekiq::Worker
include Everypoliticianbot::Github
def perform
with_git_repo('everypolitician/everypolitician-writeinpublic', branch: 'gh-pages', message: 'Update legislatures') do
countries = JSON.parse(open('https://raw.githubusercontent.com/everypolitician/everypolitician-data/master/countries.json').read, symbolize_names: true)
output = []
countries.each do |country|
country[:legislatures].each do |legislature|
warn "#{country[:name]} - #{legislature[:name]}"
legislative_period = legislature[:legislative_periods].first
raw_url_prefix = 'https://raw.githubusercontent.com/everypolitician/everypolitician-data/master/'
csv = raw_url_prefix + legislative_period[:csv]
popolo = nil
Dir.mktmpdir do |dir|
csv_file = File.join(dir, 'out.csv')
File.write(csv_file, open(csv).read)
popolo = Popolo::CSV.new(csv_file).data
end
people = popolo[:persons]
people = people.map do |person|
person[:memberships] = popolo[:memberships].find_all { |m| m[:person_id] == person[:id] }
person[:contact_details] ||= []
person
end
legislature_json_file = File.join(
country[:slug],
legislature[:slug],
'persons.json'
)
FileUtils.mkdir_p(File.dirname(legislature_json_file))
File.write(
legislature_json_file,
JSON.pretty_generate(
total: people.size,
page: 1,
per_page: people.size,
has_more: false,
result: people
)
)
output << {
country: country[:name],
legislature: legislature[:name],
slug: [country[:slug], legislature[:slug]].join('-').parameterize,
person_count: people.select { |p| !p[:end_date] }.size,
people_with_contact_details: people.count { |p| !p[:email].to_s.empty? },
popolo_json_url: legislature[:popolo_url],
popolo_json_master_url: raw_url_prefix + legislature[:popolo],
url: "https://everypolitician-writeinpublic.herokuapp.com/#{country[:slug]}/#{legislature[:slug]}"
}
end
end
File.write('countries.json', JSON.pretty_generate(output))
end
end
end
post '/event_handler' do
RebuildLegislatureFiles.perform_async
'ok'
end
get '/:country/:legislature' do |country_slug, legislature_slug|
redirect to("/#{country_slug}/#{legislature_slug}/persons/")
end
get '/:country/:legislature/persons/?' do |country_slug, legislature_slug|
content_type 'application/json; charset=utf-8'
url_base = 'https://everypolitician.github.io/everypolitician-writeinpublic'
open(url_base + "/#{country_slug}/#{legislature_slug}/persons.json").read
end