class JSONRenderer

Attributes

output[R]

Public Class Methods

new(quiz,options={}) click to toggle source
# File lib/ruql/renderers/json_renderer.rb, line 6
def initialize(quiz,options={})
  @output = ''
  @json_array = []
  @quiz = quiz
end

Public Instance Methods

answers_to_json_array(answers) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_quiz() click to toggle source
# 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