Class: IMS::LTI::ToolConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/ims/lti/tool_config.rb

Overview

Class used to represent an LTI configuration

It can create and read the Common Cartridge XML representation of LTI links as described here: www.imsglobal.org/LTI/v1p1pd/ltiIMGv1p1pd.html#_Toc309649689

Usage

To generate an XML configuration:

# Create a config object and set some options
tc = IMS::LTI::ToolConfig.new(:title => "Example Sinatra Tool Provider", :launch_url => url)
tc.description = "This example LTI Tool Provider supports LIS Outcome pass-back."

# generate the XML
tc.to_xml

Or to create a config object from an XML String:

tc = IMS::LTI::ToolConfig.create_from_xml(xml)

Constant Summary collapse

LTI_NAMESPACES =

Namespaces used for parsing configuration XML

{
        "xmlns" => 'http://www.imsglobal.org/xsd/imslticc_v1p0',
        "blti" => 'http://www.imsglobal.org/xsd/imsbasiclti_v1p0',
        "lticm" => 'http://www.imsglobal.org/xsd/imslticm_v1p0',
        "lticp" => 'http://www.imsglobal.org/xsd/imslticp_v1p0',
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ ToolConfig

Create a new ToolConfig with the given options

Parameters:

  • opts (Hash) (defaults to: {})

    The initial options for the ToolConfig



31
32
33
34
35
36
37
38
# File 'lib/ims/lti/tool_config.rb', line 31

def initialize(opts={})
  @custom_params = opts.delete("custom_params") || {}
  @extensions = opts.delete("extensions") || {}

  opts.each_pair do |key, val|
    self.send("#{key}=", val) if self.respond_to?("#{key}=")
  end
end

Instance Attribute Details

#cartridge_bundleObject

Returns the value of attribute cartridge_bundle.



23
24
25
# File 'lib/ims/lti/tool_config.rb', line 23

def cartridge_bundle
  @cartridge_bundle
end

#cartridge_iconObject

Returns the value of attribute cartridge_icon.



23
24
25
# File 'lib/ims/lti/tool_config.rb', line 23

def cartridge_icon
  @cartridge_icon
end

#custom_paramsObject (readonly)

Returns the value of attribute custom_params.



21
22
23
# File 'lib/ims/lti/tool_config.rb', line 21

def custom_params
  @custom_params
end

#descriptionObject

Returns the value of attribute description.



23
24
25
# File 'lib/ims/lti/tool_config.rb', line 23

def description
  @description
end

#extensionsObject (readonly)

Returns the value of attribute extensions.



21
22
23
# File 'lib/ims/lti/tool_config.rb', line 21

def extensions
  @extensions
end

#iconObject

Returns the value of attribute icon.



23
24
25
# File 'lib/ims/lti/tool_config.rb', line 23

def icon
  @icon
end

#launch_urlObject

Returns the value of attribute launch_url.



23
24
25
# File 'lib/ims/lti/tool_config.rb', line 23

def launch_url
  @launch_url
end

#secure_iconObject

Returns the value of attribute secure_icon.



23
24
25
# File 'lib/ims/lti/tool_config.rb', line 23

def secure_icon
  @secure_icon
end

#secure_launch_urlObject

Returns the value of attribute secure_launch_url.



23
24
25
# File 'lib/ims/lti/tool_config.rb', line 23

def secure_launch_url
  @secure_launch_url
end

#titleObject

Returns the value of attribute title.



23
24
25
# File 'lib/ims/lti/tool_config.rb', line 23

def title
  @title
end

#vendor_codeObject

Returns the value of attribute vendor_code.



23
24
25
# File 'lib/ims/lti/tool_config.rb', line 23

def vendor_code
  @vendor_code
end

#vendor_contact_emailObject

Returns the value of attribute vendor_contact_email.



23
24
25
# File 'lib/ims/lti/tool_config.rb', line 23

def vendor_contact_email
  @vendor_contact_email
end

#vendor_contact_nameObject

Returns the value of attribute vendor_contact_name.



23
24
25
# File 'lib/ims/lti/tool_config.rb', line 23

def vendor_contact_name
  @vendor_contact_name
end

#vendor_descriptionObject

Returns the value of attribute vendor_description.



23
24
25
# File 'lib/ims/lti/tool_config.rb', line 23

def vendor_description
  @vendor_description
end

#vendor_nameObject

Returns the value of attribute vendor_name.



23
24
25
# File 'lib/ims/lti/tool_config.rb', line 23

def vendor_name
  @vendor_name
end

#vendor_urlObject

Returns the value of attribute vendor_url.



23
24
25
# File 'lib/ims/lti/tool_config.rb', line 23

def vendor_url
  @vendor_url
end

Class Method Details

.create_from_xml(xml) ⇒ Object

Create a ToolConfig from the given XML

Parameters:

  • xml (String)


43
44
45
46
47
48
# File 'lib/ims/lti/tool_config.rb', line 43

def self.create_from_xml(xml)
  tc = ToolConfig.new
  tc.process_xml(xml)

  tc
end

Instance Method Details

#get_custom_param(key) ⇒ Object



54
55
56
# File 'lib/ims/lti/tool_config.rb', line 54

def get_custom_param(key)
  @custom_params[key]
end

#get_ext_param(ext_key, param_key) ⇒ Object



76
77
78
# File 'lib/ims/lti/tool_config.rb', line 76

def get_ext_param(ext_key, param_key)
  @extensions[ext_key] && @extensions[ext_key][param_key]
end

#get_ext_params(ext_key) ⇒ Object



67
68
69
# File 'lib/ims/lti/tool_config.rb', line 67

def get_ext_params(ext_key)
  @extensions[ext_key]
end

#process_xml(xml) ⇒ Object

Parse tool configuration data out of the Common Cartridge LTI link XML



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/ims/lti/tool_config.rb', line 89

def process_xml(xml)
  doc = REXML::Document.new xml
  if root = REXML::XPath.first(doc, 'xmlns:cartridge_basiclti_link')
    @title = get_node_text(root, 'blti:title')
    @description = get_node_text(root, 'blti:description')
    @launch_url = get_node_text(root, 'blti:launch_url')
    @secure_launch_url = get_node_text(root, 'blti:secure_launch_url')
    @icon = get_node_text(root, 'blti:icon')
    @secure_icon = get_node_text(root, 'blti:secure_icon')
    @cartridge_bundle = get_node_att(root, 'xmlns:cartridge_bundle', 'identifierref')
    @cartridge_icon = get_node_att(root, 'xmlns:cartridge_icon', 'identifierref')

    if vendor = REXML::XPath.first(root, 'blti:vendor')
      @vendor_code = get_node_text(vendor, 'lticp:code')
      @vendor_description = get_node_text(vendor, 'lticp:description')
      @vendor_name = get_node_text(vendor, 'lticp:name')
      @vendor_url = get_node_text(vendor, 'lticp:url')
      @vendor_contact_email = get_node_text(vendor, '//lticp:contact/lticp:email')
      @vendor_contact_name = get_node_text(vendor, '//lticp:contact/lticp:name')
    end

    if custom = REXML::XPath.first(root, 'blti:custom', LTI_NAMESPACES)
      set_properties(@custom_params, custom)
    end

    REXML::XPath.each(root, 'blti:extensions', LTI_NAMESPACES) do |vendor_ext_node|
      platform = vendor_ext_node.attributes['platform']
      properties = {}
      set_properties(properties, vendor_ext_node)
      set_options(properties, vendor_ext_node)
      self.set_ext_params(platform, properties)
    end

  end
end

#set_custom_param(key, val) ⇒ Object



50
51
52
# File 'lib/ims/lti/tool_config.rb', line 50

def set_custom_param(key, val)
  @custom_params[key] = val
end

#set_ext_param(ext_key, param_key, val) ⇒ Object



71
72
73
74
# File 'lib/ims/lti/tool_config.rb', line 71

def set_ext_param(ext_key, param_key, val)
  @extensions[ext_key] ||= {}
  @extensions[ext_key][param_key] = val
end

#set_ext_params(ext_key, ext_params) ⇒ Object

Set the extension parameters for a specific vendor

Parameters:

  • ext_key (String)

    The identifier for the vendor-specific parameters

  • ext_params (Hash)

    The parameters, this is allowed to be two-levels deep

Raises:

  • (ArgumentError)


62
63
64
65
# File 'lib/ims/lti/tool_config.rb', line 62

def set_ext_params(ext_key, ext_params)
  raise ArgumentError unless ext_params.is_a?(Hash)
  @extensions[ext_key] = ext_params
end

#to_xml(opts = {}) ⇒ Object

Generate XML from the current settings



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/ims/lti/tool_config.rb', line 126

def to_xml(opts = {})
  raise IMS::LTI::InvalidLTIConfigError, "A launch url is required for an LTI configuration." unless self.launch_url || self.secure_launch_url

  builder = Builder::XmlMarkup.new(:indent => opts[:indent] || 0)
  builder.instruct!
  builder.cartridge_basiclti_link("xmlns" => "http://www.imsglobal.org/xsd/imslticc_v1p0",
                                  "xmlns:blti" => 'http://www.imsglobal.org/xsd/imsbasiclti_v1p0',
                                  "xmlns:lticm" => 'http://www.imsglobal.org/xsd/imslticm_v1p0',
                                  "xmlns:lticp" => 'http://www.imsglobal.org/xsd/imslticp_v1p0',
                                  "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance",
                                  "xsi:schemaLocation" => "http://www.imsglobal.org/xsd/imslticc_v1p0 http://www.imsglobal.org/xsd/lti/ltiv1p0/imslticc_v1p0.xsd http://www.imsglobal.org/xsd/imsbasiclti_v1p0 http://www.imsglobal.org/xsd/lti/ltiv1p0/imsbasiclti_v1p0p1.xsd http://www.imsglobal.org/xsd/imslticm_v1p0 http://www.imsglobal.org/xsd/lti/ltiv1p0/imslticm_v1p0.xsd http://www.imsglobal.org/xsd/imslticp_v1p0 http://www.imsglobal.org/xsd/lti/ltiv1p0/imslticp_v1p0.xsd"
  ) do |blti_node|

    %w{title description launch_url secure_launch_url icon secure_icon}.each do |key|
      blti_node.blti key.to_sym, self.send(key) if self.send(key)
    end

    vendor_keys = %w{name code description url}
    if vendor_keys.any?{|k|self.send("vendor_#{k}")} || vendor_contact_email
      blti_node.blti :vendor do |v_node|
        vendor_keys.each do |key|
          v_node.lticp key.to_sym, self.send("vendor_#{key}") if self.send("vendor_#{key}")
        end
        if vendor_contact_email
          v_node.lticp :contact do |c_node|
            c_node.lticp :name, vendor_contact_name
            c_node.lticp :email, vendor_contact_email
          end
        end
      end
    end

    if !@custom_params.empty?
      blti_node.tag!("blti:custom") do |custom_node|
        @custom_params.keys.sort.each do |key|
          val = @custom_params[key]
          custom_node.lticm :property, val, 'name' => key
        end
      end
    end

    if !@extensions.empty?
      @extensions.keys.sort.each do |ext_platform|
        ext_params = @extensions[ext_platform]
        blti_node.blti(:extensions, :platform => ext_platform) do |ext_node|
          ext_params.keys.sort.each do |key|
            nest_xml(ext_node, key, ext_params[key])
          end
        end
      end
    end

    blti_node.cartridge_bundle(:identifierref => @cartridge_bundle) if @cartridge_bundle
    blti_node.cartridge_icon(:identifierref => @cartridge_icon) if @cartridge_icon

  end
end