Class: Ruql::Json

Inherits:
Object
  • Object
show all
Defined in:
lib/ruql/json/json.rb,
lib/ruql/json/version.rb

Constant Summary collapse

VERSION =
"1.0.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Json.



7
8
9
10
11
# File 'lib/ruql/json/json.rb', line 7

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

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



5
6
7
# File 'lib/ruql/json/json.rb', line 5

def output
  @output
end

Class Method Details

.allowed_optionsObject



13
14
15
# File 'lib/ruql/json/json.rb', line 13

def self.allowed_options
  return ['',[]]
end

Instance Method Details

#answers_to_json_array(answers) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ruql/json/json.rb', line 37

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



50
51
52
# File 'lib/ruql/json/json.rb', line 50

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

#render_multiple_choice(question) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/ruql/json/json.rb', line 29

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



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ruql/json/json.rb', line 17

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