Class: Qti::Models::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/qti/models/base.rb

Direct Known Subclasses

Manifest, V1::Models::Base, V2::Models::Base

Constant Summary collapse

ELEMENTS_REMAP =
{
  'prompt' => 'div',
  'simpleBlock' => 'div',
  'simpleInline' => 'span',
  'atomicBlock' => 'div',
  'atomicInline' => 'span'
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: nil) ⇒ Base

Returns a new instance of Base.

Raises:

  • (ArgumentError)


47
48
49
50
51
# File 'lib/qti/models/base.rb', line 47

def initialize(path: nil)
  @path = path
  @doc = parse_xml(File.read(path))
  raise ArgumentError unless @doc
end

Instance Attribute Details

#docObject (readonly)

Returns the value of attribute doc.



11
12
13
# File 'lib/qti/models/base.rb', line 11

def doc
  @doc
end

Class Method Details

.from_path!(path) ⇒ Object



43
44
45
# File 'lib/qti/models/base.rb', line 43

def self.from_path!(path)
  new(path: path)
end

Instance Method Details

#css_with_single_check(css) ⇒ Object

Raises:



59
60
61
62
63
# File 'lib/qti/models/base.rb', line 59

def css_with_single_check(css)
  node_list = @doc.css(css)
  raise Qti::ParseError, 'Too many matches' if node_list.count > 1
  node_list.first
end

#parse_xml(xml_string) ⇒ Object



65
66
67
# File 'lib/qti/models/base.rb', line 65

def parse_xml(xml_string)
  Nokogiri.XML(xml_string, &:noblanks)
end

#remap_href_path(href, source_path) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/qti/models/base.rb', line 69

def remap_href_path(href, source_path)
  return href unless href
  # Attempts to map to a file path relative href if href doesn't exist
  # Returns original href if that file doesn't exist
  if File.exist?(href)
    href
  else
    new_path = File.join(File.dirname(source_path), href)
    File.exist?(new_path) ? new_path : href
  end
end

#remap_unknown_tags_transformerObject



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/qti/models/base.rb', line 25

def remap_unknown_tags_transformer
  lambda do |env|
    node_name = env[:node_name]
    node = env[:node]

    return if env[:is_whitelisted] || !node.element?
    return unless ELEMENTS_REMAP.keys.include? node_name

    new_name = ELEMENTS_REMAP[node_name]
    env[:node].name = new_name
    env[:node_name] = new_name
  end
end

#sanitize_configObject



39
40
41
# File 'lib/qti/models/base.rb', line 39

def sanitize_config
  Sanitize::Config::RELAXED.merge transformers: remap_unknown_tags_transformer
end

#sanitize_content!(html) ⇒ Object



21
22
23
# File 'lib/qti/models/base.rb', line 21

def sanitize_content!(html)
  Sanitize.fragment(html, sanitize_config)
end

#xpath_with_single_check(xpath) ⇒ Object

Raises:



53
54
55
56
57
# File 'lib/qti/models/base.rb', line 53

def xpath_with_single_check(xpath)
  node_list = @doc.xpath(xpath)
  raise Qti::ParseError, 'Too many matches' if node_list.count > 1
  node_list.first
end