Permalink
Browse files

Application receives data from IRC bot

  • Loading branch information...
1 parent d93c2e5 commit 301b1015f99f8e32a4843cf5bf0243409317e53e @ahenobarbi committed May 26, 2011
View
@@ -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
@@ -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 @@ def index
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
View
@@ -62,6 +62,19 @@ def self.transitions_available(user)
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'
@@ -0,0 +1,2 @@
+CustomConfig = {}
+CustomConfig['Bot'] = YAML.load open('config/bot.yml').read
View
@@ -5,6 +5,7 @@
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.
@@ -0,0 +1,2 @@
+user: user
+password: password
View
@@ -11,6 +11,7 @@
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
@@ -104,4 +104,35 @@ def test_migration(object, migration, prohibited, allowed, final_state)
(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

0 comments on commit 301b101

Please sign in to comment.