/
vote.rb
33 lines (33 loc) · 1.18 KB
/
vote.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
module Ananasblau
module Parsers
class Vote
# can handle following commands:
# * create (which is also default) for new votings
def parse(input)
case input.split(" ")[1] || input
when /^\@/ # vote by user name
option = input.split(" ")[2..-1].join(' ')
return :error if option.blank?
return :vote, {:user_screen_name => input.split(" ")[1][1..-1], :option => option }
when /^\#?\d+/ # vote by survey id
option = input.split(" ")[2..-1].join(' ')
return :error if option.blank?
return :vote, {:id => input.split(" ")[1][/\d+/], :option => option}
when 'create' # a new survey
else
if input.match(/\?/)
# options are everything from the ? on
options = input[/\?.*/][1..-1].split('.')
if options.size == 1
options = input[/\?.*/][1..-1].split(' ')
end
return :create, {:question => input[/.*?( create)? (.*?\?)/].gsub(/.*?( create)?\? /, ''),
:options => options.map!{|w| w.strip.gsub(/\s+/, ' ')}}
else
return :error
end
end
end
end
end
end