* Tighter validation, closed a number of crashes due to invalid data
[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_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.ipaddress = "XMLRPC Request"
11       voter.session_id = "XMLRPC:#{voter_id}"
12       voter.vote=Vote.new
13       voter.vote.votes=vote_list[0]
14       voter.vote.time = Time.now
15       voter.save!
16       voter.vote.confirm!
17       voter.save!
18     end
19   end
20   def quickvote_candidate_ids_to_names(shortname, id_list)
21     qv=QuickVote.ident_to_quickvote(shortname)
22     candidates={}
23     return [] unless qv
24     qv.results
25     qv.candidates.each {|c| candidates[c.id] = c}
26     results=[]
27     id_list.each { |id|
28       name=candidates[id]
29       if name
30         results << name
31       else
32         results << ""
33       end
34     }
35     results
36   end
37   def get_quickvote_results(shortname)
38     #TODO: Validate shortname
39     qv=QuickVote.ident_to_quickvote(shortname)
40     result=VoteResultStruct.new
41     result.errors=[]
42     unless qv
43       result.errors << "No quickvote with name #{shortname} found!"
44       return result
45     end
46     qv.results
47     result.plurality_winners=qv.plurality_result.winners
48     result.approval_winners=qv.approval_result.winners
49     result.condorcet_winners=qv.condorcet_result.winners
50     result.ssd_winners=qv.ssd_result.winners
51     result.borda_winners=qv.borda_result.winners
52     result
53   end
54   def get_quickvote_candidate_map(shortname)
55     qv=QuickVote.ident_to_quickvote(shortname)
56     result=CandidateMap.new
57     result.errors=[]
58     unless qv
59       result.errors << "No quickvote with name #{shortname} found!"
60       return result
61     end
62     candidates={}
63     qv.candidates.each {|c| candidates[c.id] = c.name}
64     result.candidate_ids=candidates.keys
65     result.candidate_names=candidates.values
66     result
67   end
68     
69 end

Benjamin Mako Hill || Want to submit a patch?