Commit partial implementation of casting quickvotes via xmlrpc. WARNING: Currently...
[selectricity] / app / models / selectricity_service.rb
1 require 'action_controller/integration'
2
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
7     if election
8       voter = QuickVoter.new
9       voter.election = election
10       voter.session_id = "XMLRPC:#{voter_id}"
11       voter.vote=Vote.new
12       voter.vote.votes=vote_list[0]
13       voter.vote.time = Time.now
14       voter.save!
15       voter.vote.confirm!
16       voter.save!
17     end
18   end
19   def quickvote_candidate_ids_to_names(shortname, id_list)
20     qv=QuickVote.ident_to_quickvote(shortname)
21     candidates={}
22     return [] unless qv
23     qv.results
24     qv.candidates.each {|c| candidates[c.id] = c}
25     results=[]
26     id_list.each { |id|
27       name=candidates[id]
28       if name
29         results << name
30       else
31         results << ""
32       end
33     }
34     results
35   end
36   def get_quickvote_results(shortname)
37     #TODO: Validate shortname
38     qv=QuickVote.ident_to_quickvote(shortname)
39     result=VoteResultStruct.new
40     result.errors=[]
41     unless qv
42       result.errors << "No quickvote with name #{shortname} found!"
43       return result
44     end
45     qv.results
46     result.plurality_winners=qv.plurality_result.winners
47     result.approval_winners=qv.approval_result.winners
48     result.condorcet_winners=qv.condorcet_result.winners
49     result.ssd_winners=qv.ssd_result.winners
50     result.borda_winners=qv.borda_result.winners
51     result
52   end
53   def get_quickvote_candidate_map(shortname)
54     qv=QuickVote.ident_to_quickvote(shortname)
55     result=CandidateMap.new
56     result.errors=[]
57     unless qv
58       result.errors << "No quickvote with name #{shortname} found!"
59       return result
60     end
61     candidates={}
62     qv.candidates.each {|c| candidates[c.id] = c.name}
63     result.candidate_ids=candidates.keys
64     result.candidate_names=candidates.values
65     result
66   end
67     
68 end

Benjamin Mako Hill || Want to submit a patch?