Module: Word2Quiz::Helpers

Defined in:
lib/word_2_quiz/helpers.rb

Constant Summary collapse

TIME_TO_MINUTES_MAP =
{
  "hours" => 60,
  "minutes" => 1,
}.freeze
TITLE_START =

indexes of paragraphs where title and description start and end

3
TITLE_END =
4
DESCRIPTION_START =
0
DESCRIPTION_END =
5
QUESTION_START =
0

Class Method Summary collapse

Class Method Details

.get_question_end_index(paragraphs) ⇒ Object

Finds the index of the paragraph where the questions end



81
82
83
# File 'lib/word_2_quiz/helpers.rb', line 81

def self.get_question_end_index(paragraphs)
  paragraphs.find_index { |p| p.text.include?("End of examination") }
end

.get_question_indexes(paragraphs) ⇒ Object

Returns the indexes of the start of each question.



88
89
90
# File 'lib/word_2_quiz/helpers.rb', line 88

def self.get_question_indexes(paragraphs)
  paragraphs.each_index.select { |i| paragraphs[i].text.match(/^\d*\./) }
end

.get_question_number(paragraphs) ⇒ Object

Returns the question number



67
68
69
# File 'lib/word_2_quiz/helpers.rb', line 67

def self.get_question_number(paragraphs)
  paragraphs[QUESTION_START].text[/^(\d+)\./, 1]
end

.get_question_start_index(paragraphs) ⇒ Object

Finds the index of the paragraph where the questions start



74
75
76
# File 'lib/word_2_quiz/helpers.rb', line 74

def self.get_question_start_index(paragraphs)
  paragraphs.find_index { |p| p.text.include?("Multiple Choice") }
end

.get_quiz_description(paragraphs) ⇒ Object

Returns the quiz description. Lines 0-5 are the best description for the quiz.



60
61
62
# File 'lib/word_2_quiz/helpers.rb', line 60

def self.get_quiz_description(paragraphs)
  paragraphs[DESCRIPTION_START..DESCRIPTION_END].map(&:to_html).join("\n")
end

.get_quiz_duration(paragraphs) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/word_2_quiz/helpers.rb', line 31

def self.get_quiz_duration(paragraphs)
  time_paragraph = paragraphs.find do |p|
    p.text.include?("one session not to exceed ")
  end

  time = time_paragraph.text.sub(
    "This examination consists of one session not to exceed ",
    "",
  ).chomp(".")

  time_number = time.split(" ").first
  time_unit = time.split(" ").last
  unit_conversion = TIME_TO_MINUTES_MAP[time_unit]

  NumbersInWords.in_numbers(time_number) * unit_conversion
end

.get_quiz_title(paragraphs) ⇒ Object

Returns the quiz title. The 3rd and 4th lines contain the best title for the quiz.



52
53
54
# File 'lib/word_2_quiz/helpers.rb', line 52

def self.get_quiz_title(paragraphs)
  paragraphs[TITLE_START..TITLE_END].map(&:text).join(" ")
end

.map_to_boundaries(indexes:, paragraphs:) ⇒ Object

Takes in an array of indexes, and returns out an array of arrays of paragraphs bounded by the indexes, e.g. if indexes is [2, 5] then it returns:

paragraphs, paragraphs, paragraphs


22
23
24
25
26
27
28
29
# File 'lib/word_2_quiz/helpers.rb', line 22

def self.map_to_boundaries(indexes:, paragraphs:)
  indexes.map.with_index do |start_index, i|
    first_index = start_index
    last_index = indexes[i + 1] || paragraphs.count

    paragraphs[first_index...last_index]
  end
end

.strip_blanks(paragraphs) ⇒ Object

Returns an array of paragraphs with leading and trailing blank paragraphs removed.



96
97
98
99
100
# File 'lib/word_2_quiz/helpers.rb', line 96

def self.strip_blanks(paragraphs)
  t = paragraphs.drop_while { |p| p.text.strip.empty? }
  t.pop while t.last.text.strip.empty?
  t
end

.valid_quiz?(text) ⇒ Boolean

Checks for if the quiz is a valid quiz we can process

Returns:

  • (Boolean)


105
106
107
# File 'lib/word_2_quiz/helpers.rb', line 105

def self.valid_quiz?(text)
  text.include?("Course Examination") && text.include?("Multiple Choice")
end