aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoachim Filip Ignacy Bartosik <jbartosik@gmail.com>2011-05-26 19:07:37 +0200
committerJoachim Filip Ignacy Bartosik <jbartosik@gmail.com>2011-06-03 19:27:05 +0200
commit301b1015f99f8e32a4843cf5bf0243409317e53e (patch)
tree48a2c46f294f424bf256b573e7369f1dd1999f41
parentBot obtains voters, agenda items and voting options lists from webapp (diff)
downloadcouncil-webapp-301b1015f99f8e32a4843cf5bf0243409317e53e.tar.gz
council-webapp-301b1015f99f8e32a4843cf5bf0243409317e53e.tar.bz2
council-webapp-301b1015f99f8e32a4843cf5bf0243409317e53e.zip
Application receives data from IRC bot
-rw-r--r--.gitignore2
-rw-r--r--site/app/controllers/agendas_controller.rb12
-rw-r--r--site/app/models/agenda.rb13
-rw-r--r--site/config/initializers/custom_configs.rb2
-rw-r--r--site/config/routes.rb1
-rw-r--r--site/doc/sample_configs/bot.yml2
-rw-r--r--site/spec/factories.rb1
-rw-r--r--site/spec/models/agenda_spec.rb31
8 files changed, 63 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore
index 1abc1e5..3487f9a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,8 +1,8 @@
site/.bundle
site/db/*.sqlite3
-site/config/database.yml
site/log/*.log
site/tmp/**/*
site/app/views/taglibs/auto
+site/config/*.yml
*.swp
*.pyc
diff --git a/site/app/controllers/agendas_controller.rb b/site/app/controllers/agendas_controller.rb
index 0ee7ae0..ce84f1f 100644
--- a/site/app/controllers/agendas_controller.rb
+++ b/site/app/controllers/agendas_controller.rb
@@ -2,6 +2,7 @@ class AgendasController < ApplicationController
hobo_model_controller
+ before_filter :authenticate_bot, :only => :results
auto_actions :all
def index
@@ -11,4 +12,15 @@ class AgendasController < ApplicationController
def current_items
render :json => Agenda.current.voting_array
end
+
+ def results
+ Agenda.process_results JSON.parse(request.env["rack.input"].read)
+ end
+
+ private
+ def authenticate_bot
+ authenticate_or_request_with_http_basic do |user_name, password|
+ user_name == CustomConfig['Bot']['user'] && password == CustomConfig['Bot']['password']
+ end
+ end
end
diff --git a/site/app/models/agenda.rb b/site/app/models/agenda.rb
index ed8e385..44386e3 100644
--- a/site/app/models/agenda.rb
+++ b/site/app/models/agenda.rb
@@ -62,6 +62,19 @@ class Agenda < ActiveRecord::Base
false
end
+ def self.process_results(results)
+ a = Agenda.current
+ for item_title in results.keys
+ i = AgendaItem.first :conditions => { :agenda_id => a, :title => item_title }
+ votes = results[item_title]
+ for voter in votes.keys
+ o = VotingOption.first :conditions => { :agenda_item_id => i.id, :description => votes[voter] }
+ u = ::User.find_by_irc_nick voter
+ Vote.create! :voting_option => o, :user => u
+ end
+ end
+ end
+
def possible_transitions
transitions = case state
when 'open'
diff --git a/site/config/initializers/custom_configs.rb b/site/config/initializers/custom_configs.rb
new file mode 100644
index 0000000..599646f
--- /dev/null
+++ b/site/config/initializers/custom_configs.rb
@@ -0,0 +1,2 @@
+CustomConfig = {}
+CustomConfig['Bot'] = YAML.load open('config/bot.yml').read
diff --git a/site/config/routes.rb b/site/config/routes.rb
index e3337bb..8621347 100644
--- a/site/config/routes.rb
+++ b/site/config/routes.rb
@@ -5,6 +5,7 @@ Council::Application.routes.draw do
match 'users/voters' => 'users#voters', :as => 'voters'
match 'agendas/current_items' => 'agendas#current_items', :as => 'current_items'
+ match 'agendas/results' => 'agendas#results', :as => 'results'
# The priority is based upon order of creation:
# first created -> highest priority.
diff --git a/site/doc/sample_configs/bot.yml b/site/doc/sample_configs/bot.yml
new file mode 100644
index 0000000..6af6f1a
--- /dev/null
+++ b/site/doc/sample_configs/bot.yml
@@ -0,0 +1,2 @@
+user: user
+password: password
diff --git a/site/spec/factories.rb b/site/spec/factories.rb
index bc6fe8e..1691b50 100644
--- a/site/spec/factories.rb
+++ b/site/spec/factories.rb
@@ -11,6 +11,7 @@ end
Factory.define :agenda do |a|; end
Factory.define :agenda_item do |a|
+ a.sequence(:title) { |n| "Agenda Item #{n}" }
end
Factory.define :participation do |p|; end
diff --git a/site/spec/models/agenda_spec.rb b/site/spec/models/agenda_spec.rb
index 14a1178..5943512 100644
--- a/site/spec/models/agenda_spec.rb
+++ b/site/spec/models/agenda_spec.rb
@@ -104,4 +104,35 @@ describe Agenda do
(council_names - agenda.participations.*.participant.*.name).should be_empty
(agenda.participations.*.participant.*.name - council_names).should be_empty
end
+ it 'should properly create votes' do
+ Factory(:agenda)
+ a1 = Factory(:agenda_item, :agenda => Agenda.current)
+ a2 = Factory(:agenda_item, :agenda => Agenda.current)
+ a3 = Factory(:agenda_item, :agenda => Agenda.current)
+ Agenda.current.agenda_items.each do |item|
+ Factory(:voting_option, :agenda_item => item, :description => 'Yes')
+ Factory(:voting_option, :agenda_item => item, :description => 'No')
+ Factory(:voting_option, :agenda_item => item, :description => 'Dunno')
+ end
+
+ u = users_factory(:council, :council, :council)
+ Vote.count.should be_zero
+
+ results_hash = {
+ a1.title => { u[0].irc_nick => 'Yes', u[1].irc_nick => 'Yes', u[2].irc_nick => 'Yes'},
+ a2.title => { u[0].irc_nick => 'Yes', u[1].irc_nick => 'No', u[2].irc_nick => 'Dunno'},
+ a3.title => { u[0].irc_nick => 'Dunno', u[1].irc_nick => 'Dunno', u[2].irc_nick => 'No'}
+ }
+
+ Agenda.process_results results_hash
+
+ Vote.count.should be_equal(9)
+
+ u[0].votes.*.voting_option.*.description.should == ['Yes', 'Yes', 'Dunno']
+ u[1].votes.*.voting_option.*.description.should == ['Yes', 'No', 'Dunno']
+ u[2].votes.*.voting_option.*.description.should == ['Yes', 'Dunno', 'No']
+ a1.voting_options.*.votes.flatten.*.voting_option.*.description.should == ['Yes', 'Yes', 'Yes']
+ a2.voting_options.*.votes.flatten.*.voting_option.*.description.should == ['Yes', 'No', 'Dunno']
+ a3.voting_options.*.votes.flatten.*.voting_option.*.description.should == ['No', 'Dunno', 'Dunno']
+ end
end