Class: Word2Quiz::Answer

Inherits:
Object
  • Object
show all
Defined in:
lib/word_2_quiz/answer.rb

Overview

Answer contains all data for a single answer. An answer has text and whether it is correct.

Constant Summary collapse

ANSWER_START =

index of the very first answer paragraph

0
CORRECT_WEIGHT =

Canvas expects a weight greather than zero for correct, 0 for incorrect.

100
INCORRECT_WEIGHT =
0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text = "", correct = false) ⇒ Answer

Returns a new instance of Answer.



18
19
20
21
# File 'lib/word_2_quiz/answer.rb', line 18

def initialize(text = "", correct = false)
  @text = text
  @correct = correct
end

Instance Attribute Details

#correctObject

Returns the value of attribute correct.



9
10
11
# File 'lib/word_2_quiz/answer.rb', line 9

def correct
  @correct
end

#textObject

Returns the value of attribute text.



9
10
11
# File 'lib/word_2_quiz/answer.rb', line 9

def text
  @text
end

Class Method Details

.from_paragraphs(paragraphs, solution) ⇒ Object

Expects an array of paragraphs that are a single answer, and The correct answer for that question, e.g. answer=“a”



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/word_2_quiz/answer.rb', line 27

def self.from_paragraphs(paragraphs, solution)
  non_empty_paragraphs = Helpers.strip_blanks(paragraphs)

  text = non_empty_paragraphs.map do |paragraph|
    paragraph.to_html.sub(/(>)[a-z]\.\s?/i, '\1') # Remove answer letter.
  end.join("\n")

  start_paragraph = non_empty_paragraphs[ANSWER_START]
  is_correct = start_paragraph.text.downcase.start_with?(solution.downcase)

  Answer.new(text, is_correct)
end

Instance Method Details

#to_canvasObject

Canvas has an undocumented answer_html field for answers that we have to use because we are sending html, not plain text.



49
50
51
52
53
54
# File 'lib/word_2_quiz/answer.rb', line 49

def to_canvas
  {
    answer_html: @text,
    answer_weight: @correct ? CORRECT_WEIGHT : INCORRECT_WEIGHT,
  }
end

#to_hObject



40
41
42
43
44
45
# File 'lib/word_2_quiz/answer.rb', line 40

def to_h
  {
    text: @text,
    correct: @correct,
  }
end