Class: JSONRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/ruql/renderers/json_renderer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(quiz, options = {}) ⇒ JSONRenderer

Returns a new instance of JSONRenderer.



6
7
8
9
10
# File 'lib/ruql/renderers/json_renderer.rb', line 6

def initialize(quiz,options={})
  @output = ''
  @json_array = []
  @quiz = quiz
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



4
5
6
# File 'lib/ruql/renderers/json_renderer.rb', line 4

def output
  @output
end

Instance Method Details

#answers_to_json_array(answers) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ruql/renderers/json_renderer.rb', line 32

def answers_to_json_array(answers)
  answers_array = []
  answers.each do |answer|
    answer_json = {
      "text" => answer.answer_text,
      "explanation" => answer.explanation,
      "correct" => answer.correct
    }
    answers_array.push(answer_json)
  end
  answers_array
end

#render_fill_in(q) ⇒ Object



45
46
47
# File 'lib/ruql/renderers/json_renderer.rb', line 45

def render_fill_in(q)
  # fill-in-the-blank questions not currently supported
end

#render_multiple_choice(question) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/ruql/renderers/json_renderer.rb', line 24

def render_multiple_choice(question)
  question_hash = {
    "text" => question.question_text,
    "answers" => answers_to_json_array(question.answers)
  }
  @json_array.push(question_hash)
end

#render_quizObject



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ruql/renderers/json_renderer.rb', line 12

def render_quiz
  @quiz.questions.each do |question|
    case question
    when MultipleChoice, SelectMultiple, TrueFalse then render_multiple_choice(question)
    when FillIn then render_fill_in(question) # not currently supported
    else
      raise "Unknown question type: #{question}"
    end
  end
  @output = JSON.pretty_generate(@json_array)
end