1 require 'action_controller/integration'
3 class SelectricityService < ActionWebService::Base
4 web_service_api SelectricityAPI
5 def cast_quickvote(election_name, voter_id, vote_list)
6 election = QuickVote.ident_to_quickvote election_name
8 candidates=election.candidates.collect { |c| c.id }
9 vote_list[0].each do |vote|
10 raise ArgumentError.new "Invalid Candidate ID #{vote}" unless candidates.index(vote)
12 raise ArgumentError.new "You must rank all candidates" unless candidates.length <= vote_list[0].length
13 raise ArgumentError.new "Please rank each candidate only once" if vote_list[0].uniq!
14 voter = QuickVoter.new
15 voter.election = election
16 voter.ipaddress = "XMLRPC Request"
17 voter.session_id = "XMLRPC:#{voter_id}"
19 voter.vote.votes=vote_list[0]
20 voter.vote.time = Time.now
25 raise ArgumentError.new "Cannot find election #{election_name}"
28 def quickvote_candidate_ids_to_names(shortname, id_list)
29 qv=QuickVote.ident_to_quickvote(shortname)
33 qv.candidates.each {|c| candidates[c.id] = c}
45 def get_quickvote_results(shortname)
46 #TODO: Validate shortname
47 qv=QuickVote.ident_to_quickvote(shortname)
48 result=VoteResultStruct.new
51 result.errors << "No quickvote with name #{shortname} found!"
55 result.plurality_winners=qv.plurality_result.winners
56 result.approval_winners=qv.approval_result.winners
57 result.condorcet_winners=qv.condorcet_result.winners
58 result.ssd_winners=qv.ssd_result.winners
59 result.borda_winners=qv.borda_result.winners
62 def get_quickvote_candidate_map(shortname)
63 qv=QuickVote.ident_to_quickvote(shortname)
64 result=CandidateMap.new
67 result.errors << "No quickvote with name #{shortname} found!"
71 qv.candidates.each {|c| candidates[c.id] = c.name}
72 result.candidate_ids=candidates.keys
73 result.candidate_names=candidates.values
76 def get_quickvote_votes(shortname)
77 qv=QuickVote.ident_to_quickvote(shortname)
82 qv.votes.each do |vote|
83 votes << VoteInfo.new(:voter_id => vote.voter.id, :voter_ipaddress => vote.voter.ipaddress, :vote_time => vote.time.to_i, :vote => vote.votes)
89 QuickVote.find_all.each do |election|
90 all << get_quickvote(election.name)
94 def get_quickvote(shortname)
95 return ElectionStruct.new unless election=QuickVote.ident_to_quickvote(shortname)
96 return ElectionStruct.new (:id => election.id, :name => election.name, :description => election.description, :candidate_ids => election.candidates.collect {|c| c.id }, :candidate_names => election.candidates.collect {|c| c.name } )
98 def create_quickvote(election)
99 qv=QuickVote.new(:name => election.name, :description => election.description)
100 qv.candidatelist=election.candidate_names
104 return "Saving quickvote FAILED:"+qv.errors.inspect