1 # Selectricity: Voting Machinery for the Masses
2 # Copyright (C) 2007, 2008 Benjamin Mako Hill <mako@atdot.cc>
3 # Copyright (C) 2007 Massachusetts Institute of Technology
5 # This program is free software. Please see the COPYING file for
8 require 'action_controller/integration'
10 class SelectricityService < ActionWebService::Base
11 web_service_api SelectricityAPI
14 ## Expects a quickvote name, a voter ID, and a list of candidate ID's respectively.
15 ## Returns a string containing any potential errors that occurred in the process.
16 def cast_quickvote(election_name, voter_id, vote_list)
17 election = QuickVote.ident_to_quickvote election_name
19 candidates=election.candidates.collect { |c| c.id }
20 vote_list[0].each do |vote|
21 raise ArgumentError.new("Invalid Candidate ID #{vote}") unless candidates.index(vote)
23 raise ArgumentError.new("You must rank all candidates") unless candidates.length <= vote_list[0].length
24 raise ArgumentError.new("Please rank each candidate only once") if vote_list[0].uniq!
25 voter = QuickVoter.new
26 voter.election = election
27 voter.ipaddress = "XMLRPC Request"
28 voter.session_id = "XMLRPC:#{voter_id}"
30 voter.vote.votes=vote_list[0]
31 voter.vote.time = Time.now
36 raise ArgumentError.new("Cannot find election #{election_name}")
40 ## Converts QuickVote candidate ID's to names
41 ## Takes in a QuickVote name and a list of candidate ID's, and returns the names of
42 ## each candidate. Useful for doing just a few lookups; it's more efficient to use
43 ## get_quickvote_candidate_map for presenting info about an entire election.
44 def quickvote_candidate_ids_to_names(shortname, id_list)
45 qv=QuickVote.ident_to_quickvote(shortname)
47 raise ArgumentError.new("Quickvote by name #{shortname} doesn't exist") unless qv
49 qv.candidates.each {|c| candidates[c.id] = c}
62 ## Return the results of a QuickVote.
63 ## Takes in the name of a quickvote, and returns a structure as described by
64 ## QuickVoteResultStruct
65 def get_quickvote_results(shortname)
66 #TODO: Validate shortname
67 qv=QuickVote.ident_to_quickvote(shortname)
68 result=QuickVoteResultStruct.new
70 raise ArgumentError.new("No quickvote with name #{shortname} found!")
73 result.plurality_winners=qv.plurality_result.winners
74 result.approval_winners=qv.approval_result.winners
75 result.condorcet_winners=qv.condorcet_result.winners
76 result.ssd_winners=qv.ssd_result.winners
77 result.borda_winners=qv.borda_result.winners
81 ## Returns information regarding all the candidates in a QuickVote
82 ## Takes in a QuickVote name, and returns the list of names and ID's of candidates
83 ## This can be useful for presenting the user with a list of readable names, while
84 ## the software sends results to us in the numeric ID's we require. The two lists are in
86 def get_quickvote_candidate_map(shortname)
87 qv=QuickVote.ident_to_quickvote(shortname)
88 result=QuickVoteCandidateMap.new
90 raise ArgumentError.new("No quickvote with name #{shortname} found!")
93 qv.candidates.each {|c| candidates[c.id] = c.name}
94 result.candidate_ids=candidates.keys
95 result.candidate_names=candidates.values
99 ## Get information on all the votes cast in a QuickVote
100 ## Takes in the name of a QuickVote, returns an array of QuickVoterInfo structures.
101 def get_quickvote_votes(shortname)
102 qv = QuickVote.ident_to_quickvote(shortname)
105 raise ArgumentError.new("Cannot find QuickVote #{shortname}")
108 qv.votes.collect do |vote|
109 QuickVoterInfo.new(:voter_id => vote.voter.id,
110 :voter_ipaddress => vote.voter.ipaddress,
111 :vote_time => vote.time.to_i,
113 :voter_session_id => vote.voter.session_id)
117 ## Gets a list of all QuickVotes in the system.
118 def list_quickvotes()
119 QuickVote.find(:all).collect do |election|
120 get_quickvote(election.name)
124 ## Gets information on a particular QuickVote
125 ## Takes in a QuickVote name
126 def get_quickvote(shortname)
127 unless election = QuickVote.ident_to_quickvote(shortname)
128 raise ArgumentError.new("Cannot find QuickVote named #{shortname}")
133 :name => election.name,
134 :description => election.description,
135 :candidate_ids => election.candidates.collect {|c| c.id },
136 :candidate_names => election.candidates.collect {|c| c.name } )
139 ## Create a QuickVote
140 ## Pass in a QuickVoteStruct populated with all the fields but the candidate ID's
141 ## Any candidate ID's you fill in will be ignored anyway.
142 def create_quickvote(election)
143 qv = QuickVote.new(:name => election.name,
144 :description => election.description)
145 qv.candidate_names = election.candidate_names
150 raise ArgumentError.new("Saving quickvote FAILED:"+qv.errors.inspect)