Version 1, last updated by Jonathan Ferguson at May 31, 2010 UTC
One vote per mobile
Finally, how to restrict the poll to a unique vote per mobile phone. This would be useful for community voting where a single person could skew results by voting multiple times.
The set up instructions are the same as the previous example apart from the webhook URL has changed to http://taykt-demo.appspot.com/unique?name=community
class SingleVotePerMobilePollServlet extends PollTrait{
val numbers = Map[String, Set[String]]()
override def doPost(request:HttpServletRequest , response:HttpServletResponse ) {
response.setContentType("text/plain");
response.setCharacterEncoding("utf-8");
request.getParameter("name") match {
case pollName:String =>
val pid = request.getParameter("pid")
pollOptionFromString(request.getParameter("text")) match {
case None => response.getWriter().println("You didn't say what you wanted to vote for!")
case Some(pollOption) =>
if (hasntVoted(pollName,pid)) {
recordVote(pollName,pid)
polls.get(pollName) match{
case None => polls += (pollName -> Map(pollOption -> 1))
case Some(poll) =>
if (poll.contains(pollOption)) {
val count = poll(pollOption)
poll.put(pollOption, count + 1)
} else {
poll.put(pollOption, 1)
}
}
response.getWriter().println(sMSResponse(pollName, pollOption))
} else { response.getWriter().println("You can only vote once in the " + pollName + " poll.")}
}
case null => response.getWriter().println("No poll name provided, contact Poll organiser")
}
}
private def hasntVoted( pollName:String, id:String ):Boolean = {
numbers.get(pollName) match {
case None => numbers.put(pollName, Set[String]());true
case Some(poll) => !poll.contains(id)
}
}
private def recordVote(pollName:String , pid:String) = { numbers(pollName) += pid }
override def pollOptionFromString(raw:String):Option[String] = raw match {
case null => None
case option => Some(option.split(" ")(0))
}
}
To view how a poll is going, point your browser at http://taykt-demo.appspot.com/poll?name=pollname, so for the example above it would be http://taykt-demo.appspot.com/unique?name=community
You can try this particular service out with the web hook URL of: http://taykt-demo.appspot.com/unique?name=community
Example call:
$ curl http://taykt-demo.appspot.com/unique?name=community -X POST -d text="community Postman Pat" -d pid="abc123"