Class: PnoteClient::Converters::PnoteToJsonConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/pnote_client/converters/pnote_to_json_converter.rb

Instance Method Summary collapse

Constructor Details

#initialize(pnote_document) ⇒ PnoteToJsonConverter

Returns a new instance of PnoteToJsonConverter.



8
9
10
# File 'lib/pnote_client/converters/pnote_to_json_converter.rb', line 8

def initialize(pnote_document)
  @pnote_document = pnote_document
end

Instance Method Details

#convertObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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