Просмотр исходного кода

Add 2017 general election results

tags/last-sinatra-version
Adrian Short 6 лет назад
Родитель
Сommit
be64e5003d
2 измененных файлов: 77 добавлений и 0 удалений
  1. +10
    -0
      docs/2017/2017-general-election-results.csv
  2. +67
    -0
      scripts/import-results.rb

+ 10
- 0
docs/2017/2017-general-election-results.csv Просмотреть файл

@@ -0,0 +1,10 @@
Carshalton and Wallington,Tom Brake,Liberal Democrats,20819,1
Carshalton and Wallington,Matthew Joseph Maxwell Scott,Conservative Party,19450,0
Carshalton and Wallington,Emine Ibrahim,Labour Party,9360,0
Carshalton and Wallington,Shasha Khan,Green Party,501,0
Carshalton and Wallington,Nick Mattey,Independent,434,0
Carshalton and Wallington,Ashley Keith Dickenson,Christian Peoples Alliance Party,189,0
Sutton and Cheam,Paul Stuart Scully,Conservative Party,26567,1
Sutton and Cheam,Amna Ahmad,Liberal Democrats,13869,0
Sutton and Cheam,Bonnie Craven,Labour Party,10663,0
Sutton and Cheam,Claire Jackson-Prior,Green Party,871,0

+ 67
- 0
scripts/import-results.rb Просмотреть файл

@@ -0,0 +1,67 @@
require_relative '../models'
require 'pp'
# $ ruby scripts/import-results.rb [ELECTION ID] [CSV FILENAME]

# Import a CSV file of election results for a specified election
# The election for which you're importing data must already exist in the elections table

# Run setpositions.rb after this importer to set candidacies.position and candidacies.elected

@election = Election.get(ARGV.shift)

CSV.foreach(ARGV.shift) do |line|
district_name, candidate_name, party, votes, seats = line
pp line
# District
@district = District.first(:name => district_name)

puts @district.name

# Candidate
# Assumes that the candidate name is written "forename surname"
bits = candidate_name.split(" ")
candidate_forenames = bits[0..-2].join(" ")
candidate_surname = bits.last

# Assumes that the candidate name is written "surname, forename(s)"
# bits = candidate_name.split(", ")
# candidate_forenames = bits[1]
# candidate_surname = bits[0]
@candidate = Candidate.first_or_create(:forenames => candidate_forenames, :surname => candidate_surname)

pp @candidate

unless @candidate.saved?
$stderr.puts "Couldn't save candidate #{@candidate}"
exit 1
end
# Party
@party = Party.first_or_create(:name => party)
puts @party.name
unless @party.saved?
$stderr.puts "Couldn't save party #{@party}"
exit 1
end
# Candidacy
@candidacy = Candidacy.create(
:election => @election,
:candidate => @candidate,
:party => @party,
:district => @district,
:votes => votes,
:seats => seats
)

unless @candidacy.saved?
$stderr.puts "Couldn't save candidacy #{@candidacy}"
exit 1
end
end

Загрузка…
Отмена
Сохранить