XMLRPC: Add translator from candidate ID to names
[selectricity-live] / 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_id, vote_id, vote_list)
6     #Obviously not implemented
7   end
8   def quickvote_candidate_ids_to_names(shortname, id_list)
9     qv=QuickVote.ident_to_quickvote(shortname)
10     candidates={}
11     return [] unless qv
12     qv.results
13     qv.candidates.each {|c| candidates[c.id] = c}
14     results=[]
15     id_list.each { |id|
16       name=candidates[id]
17       if name
18         results << name
19       else
20         results << ""
21       end
22     }
23     results
24   end
25   def get_quickvote_results(shortname)
26     #TODO: Validate shortname
27     qv=QuickVote.ident_to_quickvote(shortname)
28     result=VoteResultStruct.new
29     result.errors=[]
30     unless qv
31       result.errors << "No quickvote with name #{shortname} found!"
32       return result
33     end
34     qv.results
35     result.plurality_winners=qv.plurality_result.winners
36     result.approval_winners=qv.approval_result.winners
37     result.condorcet_winners=qv.condorcet_result.winners
38     result.ssd_winners=qv.ssd_result.winners
39     result.borda_winners=qv.borda_result.winners
40     result
41   end
42   def get_quickvote_candidate_map(shortname)
43     qv=QuickVote.ident_to_quickvote(shortname)
44     result=CandidateMap.new
45     result.errors=[]
46     unless qv
47       result.errors << "No quickvote with name #{shortname} found!"
48       return result
49     end
50     candidates={}
51     qv.candidates.each {|c| candidates[c.id] = c.name}
52     result.candidate_ids=candidates.keys
53     result.candidate_names=candidates.values
54     result
55   end
56     
57 end

Benjamin Mako Hill || Want to submit a patch?