-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.rb
57 lines (49 loc) · 1.74 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
require 'bundler/setup'
Bundler.require
Dotenv.load
require 'open-uri'
require 'json'
require 'pry'
require 'active_support/core_ext'
require 'fileutils'
require 'csv'
class NameCsvGenerator
include Sidekiq::Worker
include Everypoliticianbot::Github
def perform(countries_json_url)
with_git_repo('everypolitician/everypolitician-names', branch: 'gh-pages', message: 'Update names.csv') do
countries = JSON.parse(open(countries_json_url).read, symbolize_names: true)
CSV.open('names.csv','w') do |output_csv|
headers = [:id, :name, :country, :legislature]
output_csv << headers
countries.each do |c|
c[:legislatures].each do |l|
legislature_namefile = File.join(File.dirname(l[:popolo]), 'names.csv')
warn legislature_namefile
legislature_names = open("https://raw.githubusercontent.com/everypolitician/everypolitician-data/master/#{legislature_namefile}").read
csv = CSV.new(legislature_names, headers: true, header_converters: :symbol)
csv.each do |row|
row[:country] = c[:slug]
row[:legislature] = l[:slug]
output_csv << row.values_at(*headers)
end
end
end
end
end
end
end
post '/' do
everypolitician_event = request.env['HTTP_X_EVERYPOLITICIAN_EVENT']
if everypolitician_event == 'pull_request_merged'
request.body.rewind
payload = JSON.parse(request.body.read, symbolize_names: true)
job_id = NameCsvGenerator.perform_async(payload[:countries_json_url])
"Queued job #{job_id}"
else
"Unhandled event #{everypolitician_event}"
end
end
get '/' do
'This is everypolitician-names, waiting to receive a webhook POST request to this URL.'
end