]> projects.mako.cc - selectricity-live/blob - app/models/selectricity_service.rb
544b618d7c78fddce9fb5d91f8a3a65cf263264c
[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       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)
11       end
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}"
18       voter.vote=Vote.new
19       voter.vote.votes=vote_list[0]
20       voter.vote.time = Time.now
21       voter.save!
22       voter.vote.confirm!
23       voter.save!
24     else
25       raise ArgumentError.new("Cannot find election #{election_name}")
26     end
27   end
28   def quickvote_candidate_ids_to_names(shortname, id_list)
29     qv=QuickVote.ident_to_quickvote(shortname)
30     candidates={}
31     raise ArgumentError.new("Quickvote by name #{shortname} doesn't exist") unless qv
32     qv.results
33     qv.candidates.each {|c| candidates[c.id] = c}
34     results=[]
35     id_list.each { |id|
36       name=candidates[id]
37       if name
38         results << name
39       else
40         results << ""
41       end
42     }
43     results
44   end
45   def get_quickvote_results(shortname)
46     #TODO: Validate shortname
47     qv=QuickVote.ident_to_quickvote(shortname)
48     result=VoteResultStruct.new
49     unless qv
50       raise ArgumentError.new("No quickvote with name #{shortname} found!")
51     end
52     qv.results
53     result.plurality_winners=qv.plurality_result.winners
54     result.approval_winners=qv.approval_result.winners
55     result.condorcet_winners=qv.condorcet_result.winners
56     result.ssd_winners=qv.ssd_result.winners
57     result.borda_winners=qv.borda_result.winners
58     result
59   end
60   def get_quickvote_candidate_map(shortname)
61     qv=QuickVote.ident_to_quickvote(shortname)
62     result=CandidateMap.new
63     unless qv
64       raise ArgumentError.new("No quickvote with name #{shortname} found!")
65     end
66     candidates={}
67     qv.candidates.each {|c| candidates[c.id] = c.name}
68     result.candidate_ids=candidates.keys
69     result.candidate_names=candidates.values
70     result
71   end
72
73   def get_quickvote_votes(shortname)
74     qv = QuickVote.ident_to_quickvote(shortname)
75
76     unless qv
77       raise ArgumentError.new("Cannot find QuickVote #{shortname}")
78     end
79
80     qv.votes.collect do |vote|
81        VoteInfo.new(:voter_id => vote.voter.id,
82                     :voter_ipaddress => vote.voter.ipaddress,
83                     :vote_time => vote.time.to_i,
84                     :vote => vote.votes,
85                     :voter_session_id => vote.voter.session_id)
86     end
87   end
88
89   def list_quickvotes()
90     QuickVote.find(:all).collect do |election|
91       get_quickvote(election.name)
92     end
93   end
94
95   def get_quickvote(shortname)
96     unless election = QuickVote.ident_to_quickvote(shortname)
97       raise ArgumentError.new("Cannot find QuickVote named #{shortname}")
98     end
99
100     ElectionStruct.new(
101       :id => election.id,
102       :name => election.name,
103       :description => election.description,
104       :candidate_ids => election.candidates.collect {|c| c.id },
105       :candidate_names => election.candidates.collect {|c| c.name } )
106   end
107
108   def create_quickvote(election)
109     qv = QuickVote.new(:name => election.name,
110                        :description => election.description)
111     qv.candidate_names = election.candidate_names
112
113     if qv.save
114       return ""
115     else
116       raise ArgumentError.new("Saving quickvote FAILED:"+qv.errors.inspect)
117     end
118   end
119
120 end

Benjamin Mako Hill || Want to submit a patch?