]> projects.mako.cc - selectricity/blob - app/models/selectricity_service.rb
4075f6401fa40f889f16be53ac80fc7b17398d57
[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       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     end
25   end
26   def quickvote_candidate_ids_to_names(shortname, id_list)
27     qv=QuickVote.ident_to_quickvote(shortname)
28     candidates={}
29     return [] unless qv
30     qv.results
31     qv.candidates.each {|c| candidates[c.id] = c}
32     results=[]
33     id_list.each { |id|
34       name=candidates[id]
35       if name
36         results << name
37       else
38         results << ""
39       end
40     }
41     results
42   end
43   def get_quickvote_results(shortname)
44     #TODO: Validate shortname
45     qv=QuickVote.ident_to_quickvote(shortname)
46     result=VoteResultStruct.new
47     result.errors=[]
48     unless qv
49       result.errors << "No quickvote with name #{shortname} found!"
50       return result
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     result.errors=[]
64     unless qv
65       result.errors << "No quickvote with name #{shortname} found!"
66       return result
67     end
68     candidates={}
69     qv.candidates.each {|c| candidates[c.id] = c.name}
70     result.candidate_ids=candidates.keys
71     result.candidate_names=candidates.values
72     result
73   end
74   def get_quickvote_votes(shortname)
75     qv=QuickVote.ident_to_quickvote(shortname)
76     votes=Array.new
77     unless qv
78       return result
79     end
80     qv.votes.each  do |vote|
81       votes << VoteInfo.new(:voter_id => vote.voter.id, :voter_ipaddress => vote.voter.ipaddress, :vote_time => vote.time.to_i)
82     end
83     return votes
84   end
85   def list_quickvotes()
86     all=Array.new
87     QuickVote.find_all.each do |election|
88       all << get_quickvote(election.name)
89     end
90     return all
91   end
92   def get_quickvote(shortname)
93     return ElectionStruct.new unless election=QuickVote.ident_to_quickvote(shortname)
94     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 } )
95   end
96   def create_quickvote(election)
97     qv=QuickVote.new(:name => election.name, :description => election.description)
98     qv.candidatelist=election.candidate_names
99     return qv.save.to_s
100   end
101
102 end

Benjamin Mako Hill || Want to submit a patch?