class PnoteClient::Converters::PnoteToJsonConverter

Public Class Methods

new(pnote_document) click to toggle source
# File lib/pnote_client/converters/pnote_to_json_converter.rb, line 8
def initialize(pnote_document)
  @pnote_document = pnote_document
end

Public Instance Methods

convert() click to toggle source
# File lib/pnote_client/converters/pnote_to_json_converter.rb, line 12
def convert
  result = {
    pnote_client_version: PnoteClient::VERSION,
    chapters: [],
    images: [],
    created_at: Time.now.strftime("%Y-%m-%d %H:%M")
  }

  @pnote_document.images.each do |image|
    result[:images] << {
      id: image.id,
      format: image.format,
      size: image.size,
      raw_data: image.raw_data
    }
  end

  @pnote_document.chapters.each do |chapter|
    chapter_hash = {
      title: chapter.title,
      sub_chapters: []
    }
    result[:chapters] << chapter_hash

    unless chapter.practices.empty? # 심화단원일 경우 "실전문제" 소단원 하나 만들어서 전부 넣기
      sub_chapter_hash = {
        title: '실전문제',
        exercises: [],
        practices: []
      }
      chapter_hash[:sub_chapters] << sub_chapter_hash

      # 심화단원의 실전 문제
      chapter.practices.each do |practice|
        practice_hash = get_problem_hash(practice)
        sub_chapter_hash[:practices] << practice_hash
      end
    else # 일반 단원일경우 소단원 채우기
      chapter.sub_chapters.each do |sub_chapter|
        sub_chapter_hash = {
          title: sub_chapter.title,
          concepts: [],
          exercises: [],
          practices: []
        }
        chapter_hash[:sub_chapters] << sub_chapter_hash

        sub_chapter.concepts.each do |concept|
          concept_hash = {
            title: concept.title,
            content: concept.content
          }

          sub_chapter_hash[:concepts] << concept_hash
        end

        sub_chapter.exercises.each do |exercise|
          exercise_hash = get_problem_hash(exercise)
          sub_chapter_hash[:exercises] << exercise_hash
        end

        sub_chapter.practices.each do |practice|
          practice_hash = get_problem_hash(practice)
          sub_chapter_hash[:practices] << practice_hash
        end
      end
    end
  end

  return result.to_json
end

Private Instance Methods

get_problem_hash(problem) click to toggle source
# File lib/pnote_client/converters/pnote_to_json_converter.rb, line 86
def get_problem_hash(problem)
  {
    question: problem.question,
    answer: problem.answer,
    explaination: problem.explaination,
    teacher_comments: problem.teacher_comments.sort_by {|tc| tc.show_time }.map do |tc|
      {
        show_time: tc.show_time,
        content: tc.content
      }
    end,
    choices: problem.choices,
    tip: problem.tip
  }
end