Class: CanvasLinkMigrator::ImportedHtmlConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/canvas_link_migrator/imported_html_converter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource_map: nil, migration_id_converter: nil) ⇒ ImportedHtmlConverter

Returns a new instance of ImportedHtmlConverter.



26
27
28
29
30
# File 'lib/canvas_link_migrator/imported_html_converter.rb', line 26

def initialize(resource_map: nil, migration_id_converter: nil)
  @migration_id_converter = migration_id_converter || ResourceMapService.new(resource_map)
  @link_parser = LinkParser.new(@migration_id_converter)
  @link_resolver = LinkResolver.new(@migration_id_converter)
end

Instance Attribute Details

Returns the value of attribute link_parser.



24
25
26
# File 'lib/canvas_link_migrator/imported_html_converter.rb', line 24

def link_parser
  @link_parser
end

Returns the value of attribute link_resolver.



24
25
26
# File 'lib/canvas_link_migrator/imported_html_converter.rb', line 24

def link_resolver
  @link_resolver
end

#migration_id_converterObject (readonly)

Returns the value of attribute migration_id_converter.



24
25
26
# File 'lib/canvas_link_migrator/imported_html_converter.rb', line 24

def migration_id_converter
  @migration_id_converter
end

Instance Method Details

#convert_exported_html(input_html) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/canvas_link_migrator/imported_html_converter.rb', line 47

def convert_exported_html(input_html)
  new_html = link_parser.convert(input_html, "type", "lookup_id", "field")
  replace!(new_html)

  # missing links comes back as a list for all types and fields, but if the user's only
  # sending one piece of html at a time, we only need the first set of missing links,
  # and only the actual missing links, not the look up information we set in this method
  bad_links = missing_links&.first&.dig(:missing_links)
  link_parser.reset!
  [new_html, bad_links]
end


35
36
37
38
39
40
41
42
43
44
45
# File 'lib/canvas_link_migrator/imported_html_converter.rb', line 35

def convert_single_link(single_link, link_type: false)
  url = single_link.dup
  LinkParser::REFERENCE_KEYWORDS.each { |ref| url.gsub!("%24#{ref}%24", "$#{ref}$") }
  # create the link map for a single link (parse)
  link_parsing_result = link_parser.parse_single_url(url, link_type)
  link_parser.handle_parsed_url(url, link_parsing_result, nil, nil, link_type, nil, nil)
  # resolve_link! on the single element link map
  link_resolver.resolve_link!(link_parsing_result, link_type)
  # return the new value
  link_parsing_result[:new_value]
end


68
69
70
71
72
73
74
75
76
77
78
# File 'lib/canvas_link_migrator/imported_html_converter.rb', line 68

def missing_links
  link_parser.unresolved_link_map.each_with_object([]) do |(item_key, field_links), bad_links|
    field_links.each do |field, links|
      unresolved_links = links.select { |link| link[:replaced] && (link[:missing_url] || !link[:new_value]) }
      unresolved_links = unresolved_links.map { |link| link.slice(:link_type, :missing_url) }
      next unless unresolved_links.any?

      bad_links << { object_lookup_id: item_key[:migration_id], object_type: item_key[:type], object_field: field, missing_links: unresolved_links }
    end
  end
end

#replace!(placeholder_html) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/canvas_link_migrator/imported_html_converter.rb', line 59

def replace!(placeholder_html)
  link_map = link_parser.unresolved_link_map
  return unless link_map.present?

  link_resolver.resolve_links!(link_map)
  LinkReplacer.sub_placeholders!(placeholder_html, link_map.values.map(&:values).flatten)
  placeholder_html
end