Permalink
Browse files

Hmm

  • Loading branch information...
1 parent f663fc5 commit 2394787f9a39721e8c62deb9ce2c89e05a41df8a @peterc committed Jul 29, 2012
View
@@ -8,4 +8,5 @@ gem 'json'
gem 'redis'
gem 'ohm'
gem 'ohm-contrib'
-gem 'awesome_print'
+gem 'awesome_print'
+gem 'sinatra-flash'
View
@@ -43,6 +43,8 @@ GEM
rack (~> 1.3, >= 1.3.6)
rack-protection (~> 1.2)
tilt (~> 1.3, >= 1.3.3)
+ sinatra-flash (0.3.0)
+ sinatra (>= 1.0.0)
thin (1.4.1)
daemons (>= 1.0.9)
eventmachine (>= 0.12.6)
@@ -62,4 +64,5 @@ DEPENDENCIES
redis
sass
sinatra
+ sinatra-flash
thin
View
@@ -0,0 +1,55 @@
+class Willy
+ post '/poll' do
+ question = params[:question] || ''
+ answers = params[:answer]
+
+ unless question.to_s.length > 5 && answers.all? { |a| !a.empty? }
+ halt erb 'Invalid inputs!!'
+ end
+
+ poll = Poll.create question: question, ip: request.ip
+
+ answers.each do |answer|
+ poll.answers.push(Answer.create text: answer)
+ end
+
+ session[:tokens] ||= []
+ session[:tokens] << poll.token
+
+ redirect "#{poll.id}"
+ end
+
+ post '/poll/:id/activate' do
+ @poll = Poll[params[:id]]
+ redirect '/' unless @poll
+ redirect '/' unless params[:token] == @poll.token
+
+ @poll.start
+
+ content_type "application/json"
+ erb({status: 'success', seconds_remaining: @poll.seconds_remaining }.to_json, :layout => false)
+ end
+
+ get %r{/(\d+)} do |id|
+ @poll = Poll[id]
+ redirect '/' unless @poll
+
+ @owner = session[:tokens] && session[:tokens].include?(@poll.token)
+ erb :poll
+ end
+
+ post '/vote/:poll_id/:answer_id' do
+ @poll = Poll[params[:poll_id]]
+ redirect '/' unless @poll
+
+ Vote.create poll: @poll, answer: Answer[params[:answer_id]]
+
+ # TODO: DON'T DO THIS HERE!!
+ client = Faye::Client.new('http://localhost:9292/fayex')
+ client.publish('/messages', {'poll_id' => @poll.id, 'status' => 'counts'}.merge(@poll.counts) )
+
+ content_type "application/json"
+ erb({ status: 'success' }.to_json, :layout => false)
+ end
+
+end
View
@@ -1,14 +0,0 @@
-class Willy
- post '/vote' do
- question = params[:question]
- answers = params[:answer]
-
- poll = Poll.create question: question, ip: request.ip
-
- answers.each do |answer|
- poll.answers.push Answer.create text: answer
- end
-
- redirect "#{poll.id}"
- end
-end
View
@@ -1,6 +1,7 @@
class Answer < Ohm::Model
attribute :text
reference :poll, :Poll
+ collection :votes, :Vote
def validate
assert_present :text
View
@@ -2,17 +2,71 @@ class Poll < Ohm::Model
attribute :question
attribute :token
attribute :active
+ attribute :visible
attribute :ip
+ attribute :started_at
+ attribute :ending_at
+
list :answers, :Answer
- set :votes, :Vote
+ collection :votes, :Vote
include Ohm::Callbacks
def before_create
self.token = rand(10 ** 8).to_s(20)
+ self.visible = true
+ self.active = false
+ end
+
+ def active?
+ return false if self.active == false || self.active == 'false'
+ true
end
def validate
assert_present :question
end
+
+ def start
+ self.started_at = Time.now.to_i
+ self.ending_at = Time.now.to_i + 20
+ self.active = true
+ self.save
+ client = Faye::Client.new('http://localhost:9292/fayex')
+ client.publish('/messages', 'status' => 'start', 'seconds_remaining' => self.seconds_remaining, 'poll_id' => self.id)
+ end
+
+ def ready_to_start?
+ return true unless self.ending_at
+ return false if self.ending_at.to_i < Time.now.to_i
+ return false if Time.now.to_i > self.started_at.to_i
+ true
+ end
+
+ def counts
+ h = Hash[answers.to_a.map { |a| ["answer-#{a.id}", votes.count { |v| v.answer_id == a.id }] }]
+ h['max'] = h.values.max
+ h['total'] = self.votes.size
+ h
+ end
+
+ def finished?
+ return true if self.ending_at && Time.now.to_i > self.ending_at.to_i
+ false
+ end
+
+ def finish
+ self.active = false
+ self.save
+ end
+
+ def seconds_remaining
+ r = [self.ending_at.to_i - Time.now.to_i, 0].max rescue 0
+ finish if r < 1
+ r
+ end
+
+ def winner
+ votes.group_by { |v| v.answer }.max_by { |k, v| v.size }.first
+ end
end
View
@@ -1,14 +1,17 @@
Thread.new do
client = Faye::Client.new('http://localhost:9292/fayex')
- client.subscribe('/messages') do |message|
- puts message.inspect
+ client.subscribe('/votes') do |message|
+ File.open('/tmp/log.log', 'a') { |f| f.puts message.inspect }
end
+ File.open('/tmp/log.log', 'a') { |f| f.puts "balls" }
end
+
#Thread.new do
# client = Faye::Client.new('http://localhost:9292/fayex')
# 10.times do
# sleep 1
# client.publish('/messages', 'text' => 'Hellxxxo world')
# end
-#end
+#end
+
View
@@ -1,39 +1,39 @@
-<!doctype html>
-<html>
-<head>
- <meta charset="utf-8">
- <title></title>
- <meta name="description" content="">
- <meta name="author" content="">
- <link rel="stylesheet" href="/style.css" />
-</head>
-<body>
- <header>
- <h1>Blah.</h1>
- </header>
- <div id="main">
- <div id="debug"></div>
- <button id="send">Send</button>
- </div>
- <footer>
-
- </footer>
- <script src="/jquery.min.js"></script>
- <script src="/ICanHaz.js"></script>
- <script src="/fayex/client.js"></script>
- <script>
- var client = new Faye.Client('/fayex');
- ich.addTemplate('message', "<div>oooh.. {{text}}</div>");
-
- $('#send').click(function() {
- client.publish('/messages', {
- text: 'Hello world'
- });
- });
- client.subscribe('/messages', function(message) {
- $('#debug').append(ich.message(message));
- });
+ <form id="questionForm" method="post" action="/poll">
+
+ <p>Question</p>
+ <input type="text" name="question" id="question" value="" required />
+
+ <p>Answers</p>
+ <input type="text" name="answer[]" value="" required />
+ <input type="text" name="answer[]" value="" required />
+ <button id="remove" style="display: none;">Remove - </button>
- </script>
-</body>
-</html>
+ <button id="add">Add + </button>
+
+ <button type="submit">Create Poll</button>
+ </form>
+
+<script>
+ $(document).ready(function() {
+
+ $('#add').click( function(e) {
+ e.preventDefault;
+ $('<input type="text" name="answer[]" value="" class="optionalAnswer" required />').insertBefore('#remove');
+ $('#remove').show();
+ });
+
+
+ $('#remove').click( function(e) {
+ e.preventDefault;
+ $('.optionalAnswer').last().remove();
+
+ // if last child DOES !NOT have a class of optional answer,
+ // we know we have our origional two left - so hold fire!!
+ if (!$('input[type=text]').last().hasClass('optionalAnswer')) {
+ $('#remove').hide();
+ }
+
+ });
+
+ });
+</script>
View
@@ -0,0 +1,24 @@
+<!doctype html>
+<html>
+<head>
+ <meta charset="utf-8">
+ <title></title>
+ <meta name="description" content="">
+ <meta name="author" content="">
+ <link rel="stylesheet" href="/style.css" />
+ <script src="/jquery.min.js"></script>
+ <meta name="viewport" content="width=420" />
+ <!-- script src="/ICanHaz.js"></script -->
+ <!-- link href='http://fonts.googleapis.com/css?family=Homenaje' rel='stylesheet' type='text/css' -->
+</head>
+<body>
+ <div id="main">
+
+ <%= yield %>
+
+ </div>
+ <footer>
+
+ </footer>
+</body>
+</html>
View
@@ -0,0 +1,44 @@
+<% if @owner %>
+ <h2 class="url">http://no.gd/poll/<%= @poll.id %></h2>
+<% end %>
+
+<% if @owner && @poll.ready_to_start? %>
+ <div class="activator"><button id="start">Start poll!</button></div>
+<% end %>
+
+<div class="question">
+ <%= @poll.question %>
+</div>
+
+<% if !@owner %>
+ <div class="buttons" style="<%= 'display: none' if @poll.finished? %>">
+ <% @poll.answers.each do |answer| %>
+ <div class="answer">
+ <button class="vote" data-answerid="<%= answer.id %>"><%= answer.text %></button>
+ </div>
+ <% end %>
+ </div>
+<% end %>
+
+<div class="answers" style="display: none; <%= 'display: block' if @owner || @poll.finished? %>">
+ <% @poll.answers.each do |answer| %>
+ <div class="answer" id="answer-<%= answer.id %>">
+ <div class="text"><%= answer.text %></div>
+ <div class="bar"></div>
+ <div class="votes"><%= answer.votes.size %></div>
+ </div>
+ <% end %>
+</div>
+
+<div class="timer" style="<%= 'display: none' unless @poll.active? %>"><div class="value"></div> seconds left</div>
+
+<script>
+ <% if @owner %>var token = '<%= @poll.token %>';<% end %>
+ var poll_id = <%= @poll.id %>;
+ var active = <%= @poll.active %>;
+ var seconds_remaining = <%= @poll.seconds_remaining %>;
+ var owner = <%= !!@owner %>;
+</script>
+
+<script src="/fayex/client.js"></script>
+<script src="/main.js"></script>
View
@@ -0,0 +1,30 @@
+<h2><%= @poll.question %></h2>
+
+<% @poll.answers.each do |answer| %>
+ <div class="answer">
+ <button class="vote" data-answerid="<%= answer.id %>"><%= answer.text %></button>
+ </div>
+<% end %>
+
+<div id="debug"></div>
+
+<script src="/fayex/client.js"></script>
+<!-- script src="/main.js"></script -->
+<script>
+ var client = new Faye.Client('/fayex');
+ ich.addTemplate('vote', "<div>{{poll_id}}-{{answer_id}}</div>");
+
+ $('button.vote').click(function() {
+ console.log($(this).data('answerid'));
+ client.publish('/votes', {
+ poll_id: '<%= @poll.id %>',
+ answer_id: $(this).data('answerid')
+ });
+ console.log('hmm');
+ });
+
+ client.subscribe('/votes', function(vote) {
+ $('#debug').append(ich.vote(vote));
+ });
+
+</script>
Oops, something went wrong.

0 comments on commit 2394787

Please sign in to comment.