Module: ContentLoader

Defined in:
lib/asker/loader/content_loader.rb

Overview

Define methods that load data from XML contents

Class Method Summary collapse

Class Method Details

.load(filepath, content) ⇒ Object

Load XML content into Asker data objects

Parameters:

  • filepath (String)

    File path

  • content (String)

    XML plane text content



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/asker/loader/content_loader.rb', line 16

def self.load(filepath, content)
  concepts = []
  codes = []
  begin
    xmlcontent = REXML::Document.new(content)
  rescue REXML::ParseException
    raise_error_with(filepath, content)
  end
  lang = read_lang_attribute(xmlcontent)
  context = read_context_attribute(xmlcontent)

  xmlcontent.root.elements.each do |xmldata|
    case xmldata.name
    when 'concept'
      concepts << read_concept(xmldata, filepath, lang, context)
    when 'code'
      codes << read_code(xmldata, filepath)
    else
      Logger.verboseln Rainbow("[ERROR] Unkown tag <#{xmldata.name}>").red
    end
  end

  { concepts: concepts, codes: codes }
end

.raise_error_with(filepath, content) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/asker/loader/content_loader.rb', line 93

def self.raise_error_with(filepath, content)
  msg = Rainbow("[ERROR] ContentLoader: Format error in #{filepath}").red.bright
  Logger.verbose msg
  f = File.open('output/error.xml', 'w')
  f.write(content)
  f.close
  raise msg
end

.read_code(xmldata, filepath) ⇒ Object

Read code from input XML data

Parameters:

  • xmldata (XML Object)
  • filepath (String)


84
85
86
87
88
89
90
91
# File 'lib/asker/loader/content_loader.rb', line 84

def self.read_code(xmldata, filepath)
  project = Project.instance
  c = CodeLoader.load(xmldata, filepath)
  if [ File.basename(filepath), :default ].include? project.process_file
    c.process = true
  end
  c
end

.read_concept(xmldata, filepath, lang, context) ⇒ Object

Read concept from input XML data

Parameters:

  • xmldata (XML Object)
  • filepath (String)
  • lang
  • context


71
72
73
74
75
76
77
78
# File 'lib/asker/loader/content_loader.rb', line 71

def self.read_concept(xmldata, filepath, lang, context)
  project = Project.instance
  c = Concept.new(xmldata, filepath, lang, context)
  if [ File.basename(filepath), :default ].include? project.process_file
    c.process = true
  end
  c
end

.read_context_attribute(xmldata) ⇒ Object

Read context attr from input XML data

Parameters:

  • xmldata (XML Object)


56
57
58
59
60
61
62
63
# File 'lib/asker/loader/content_loader.rb', line 56

def self.read_context_attribute(xmldata)
  begin
    context = xmldata.root.attributes['context']
  rescue StandardError
    context = 'unknown'
  end
  context
end

.read_lang_attribute(xmldata) ⇒ Object

Read lang attr from input XML data

Parameters:

  • xmldata (XML Object)


44
45
46
47
48
49
50
51
# File 'lib/asker/loader/content_loader.rb', line 44

def self.read_lang_attribute(xmldata)
  begin
    lang = xmldata.root.attributes['lang']
  rescue StandardError
    lang = Project.instance.lang
  end
  lang
end