aboutsummaryrefslogtreecommitdiff
blob: db2af75e4c989f0b5171ee54d6af0ac1d7fb547f (plain)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class EmailAnswer < Answer
  fields do
    correct :boolean
  end

  # Users can't change Email answers - app does it internally
  multi_permission(:create, :update, :destroy, :edit){ false }

  # Creates new answer/ updates old answer
  # expects user to send email from address [s]he has given to app
  # and title in format "#{question.id}-#{user.token}"
  def self.answer_from_email(email)
    user = User.find_by_email_address(email.from)
    return if user.nil?

    subject       = /^([0-9]+)-(\w+)$/.match(email.subject)
    return if subject.nil?
    return unless user.token == subject.captures[1]

    question      = Question.first :conditions => { :id => subject.captures[0] }

    if(question.nil? || !question.content.is_a?(QuestionContentEmail))
      UserMailer.deliver_unrecognized_email(user, email)
      return
    end

    # Fetch existing answer, if it doesn't exist create a new one
    # them mark it as incorrect (if it passes all tests it will be correct)
    answer          = question.answer_of(user)
    answer          = EmailAnswer.new(:question => question, :owner => user) if answer.nil?
    answer.correct  = false
    answer.save!

    for condition in question.content.req_array
      val     = email.send(condition[0].downcase.sub('-', '_'))  # Value we're testing
      cond    = /#{condition[1]}/i                              # Regexp value should match
      passed  = false                                           # Was test passed

      val = ['']  if val.is_a? NilClass
      val = [val] if val.is_a? String

      for v in val
        if v.match(cond)
          # Test passed
          passed = true
          break
        end
      end

      return unless passed
    end

    # passed all test - mark it and return answer
    answer.correct  = true
    answer.save!
    answer
  end
end