]> projects.mako.cc - selectricity/blob - app/models/selectricity_service.rb
e13037535bf04b37e290244feff0c21b597ee458
[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.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   def get_quickvote_votes(shortname)
69     qv=QuickVote.ident_to_quickvote(shortname)
70     votes=Array.new
71     unless qv
72       return result
73     end
74     qv.votes.each  do |vote|
75       votes << VoteInfo.new(:voter_id => vote.voter.id, :voter_ipaddress => vote.voter.ipaddress, :vote_time => vote.time.to_i)
76     end
77     return votes
78   end
79   def list_quickvotes()
80     all=Array.new
81     QuickVote.find_all.each do |election|
82       all << ElectionStruct.new (:id => election.id, :name => election.name, :description => election.description, :candidates => election.candidates.collect {|c| c.id } )
83     end
84     return all
85   end
86 end

Benjamin Mako Hill || Want to submit a patch?