Election results in the London Borough of Sutton.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

68 linhas
1.7 KiB

  1. require_relative '../models'
  2. require 'pp'
  3. # $ ruby scripts/import-results.rb [ELECTION ID] [CSV FILENAME]
  4. # Import a CSV file of election results for a specified election
  5. # The election for which you're importing data must already exist in the elections table
  6. # Run setpositions.rb after this importer to set candidacies.position and candidacies.elected
  7. @election = Election.get(ARGV.shift)
  8. CSV.foreach(ARGV.shift) do |line|
  9. district_name, candidate_name, party, votes, seats = line
  10. pp line
  11. # District
  12. @district = District.first(:name => district_name)
  13. puts @district.name
  14. # Candidate
  15. # Assumes that the candidate name is written "forename surname"
  16. bits = candidate_name.split(" ")
  17. candidate_forenames = bits[0..-2].join(" ")
  18. candidate_surname = bits.last
  19. # Assumes that the candidate name is written "surname, forename(s)"
  20. # bits = candidate_name.split(", ")
  21. # candidate_forenames = bits[1]
  22. # candidate_surname = bits[0]
  23. @candidate = Candidate.first_or_create(:forenames => candidate_forenames, :surname => candidate_surname)
  24. pp @candidate
  25. unless @candidate.saved?
  26. $stderr.puts "Couldn't save candidate #{@candidate}"
  27. exit 1
  28. end
  29. # Party
  30. @party = Party.first_or_create(:name => party)
  31. puts @party.name
  32. unless @party.saved?
  33. $stderr.puts "Couldn't save party #{@party}"
  34. exit 1
  35. end
  36. # Candidacy
  37. @candidacy = Candidacy.create(
  38. :election => @election,
  39. :candidate => @candidate,
  40. :party => @party,
  41. :district => @district,
  42. :votes => votes,
  43. :seats => seats
  44. )
  45. unless @candidacy.saved?
  46. $stderr.puts "Couldn't save candidacy #{@candidacy}"
  47. exit 1
  48. end
  49. end