class Lti::ThinCommonCartridge
Constants
- LTI_LINK_FILEPATH
- LTI_LINK_RESOURCE_TYPE
- MANIFEST_FILEPATH
Attributes
items[R]
link_template[R]
links[R]
xml[R]
Public Class Methods
new(items)
click to toggle source
# File lib/lti/thin_common_cartridge.rb, line 10 def initialize(items) @items = Array.wrap items @xml = File.open(File.expand_path MANIFEST_FILEPATH) { |f| Nokogiri::XML(f) } @link_template = File.open(File.expand_path LTI_LINK_FILEPATH) { |f| Nokogiri::XML(f) } @links = [] create_nodes end
Public Instance Methods
create_nodes()
click to toggle source
# File lib/lti/thin_common_cartridge.rb, line 19 def create_nodes # Build hierarchy of objects root_item = xml.at 'organizations/organization/item' items.each { |c| add_item c, root_item } end
manifest()
click to toggle source
# File lib/lti/thin_common_cartridge.rb, line 25 def manifest xml.to_xml end
Private Instance Methods
add_item(item, parent)
click to toggle source
# File lib/lti/thin_common_cartridge.rb, line 37 def add_item(item, parent) item[:identifier] = "ub#{SecureRandom.hex(17)}" item[:identifierref] = "#{item[:identifier]}_link" params = { identifier: item[:identifier], identifierref: (item[:identifierref] if item[:url].present?) } node = create_node 'item', params node.add_child %(<title>#{item[:title].capitalize}</title>) parent.add_child node item[:children].each { |c| add_item c, node } add_resource(item) if item[:url].present? node end
add_resource(item)
click to toggle source
# File lib/lti/thin_common_cartridge.rb, line 56 def add_resource(item) params = { identifier: item[:identifierref], type: LTI_LINK_RESOURCE_TYPE } node = create_node('resource', params) resources_node.add_child node href = "lti_links/#{item[:identifierref]}.xml" create_link item, href file_node = create_node 'file', href: href node.add_child file_node end
create_link(item, file_href)
click to toggle source
# File lib/lti/thin_common_cartridge.rb, line 71 def create_link(item, file_href) @link_template.at('//blti:launch_url').content = item[:url] links << { name: file_href, data: @link_template.to_xml } end
create_node(name, attrs = {})
click to toggle source
# File lib/lti/thin_common_cartridge.rb, line 79 def create_node(name, attrs = {}) node = Nokogiri::XML::Node.new name, xml attrs.compact.each { |k, v| node[k.to_s] = v } node end
resources_node()
click to toggle source
# File lib/lti/thin_common_cartridge.rb, line 85 def resources_node @resources_node ||= begin if (node = @xml.at 'resources').nil? node = create_node 'resources' xml.root.add_child node end node end end