multi_version_common_cartridge 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/.rspec +1 -0
  4. data/.rubocop.yml +54 -0
  5. data/Gemfile +2 -0
  6. data/LICENSE +674 -0
  7. data/README.md +36 -0
  8. data/lib/multi_version_common_cartridge.rb +38 -0
  9. data/lib/multi_version_common_cartridge/cartridge.rb +53 -0
  10. data/lib/multi_version_common_cartridge/cartridge_versions.rb +25 -0
  11. data/lib/multi_version_common_cartridge/item.rb +29 -0
  12. data/lib/multi_version_common_cartridge/manifest.rb +33 -0
  13. data/lib/multi_version_common_cartridge/resources/basic_lti_link.rb +46 -0
  14. data/lib/multi_version_common_cartridge/resources/resource.rb +23 -0
  15. data/lib/multi_version_common_cartridge/sax_machine_nokogiri_xml_saver.rb +85 -0
  16. data/lib/multi_version_common_cartridge/version.rb +19 -0
  17. data/lib/multi_version_common_cartridge/writers/basic_lti_extension_writer.rb +45 -0
  18. data/lib/multi_version_common_cartridge/writers/basic_lti_link_writer.rb +241 -0
  19. data/lib/multi_version_common_cartridge/writers/basic_lti_vendor_writer.rb +66 -0
  20. data/lib/multi_version_common_cartridge/writers/cartridge_writer.rb +85 -0
  21. data/lib/multi_version_common_cartridge/writers/factory.rb +59 -0
  22. data/lib/multi_version_common_cartridge/writers/item_writer.rb +60 -0
  23. data/lib/multi_version_common_cartridge/writers/manifest_metadata_writer.rb +62 -0
  24. data/lib/multi_version_common_cartridge/writers/manifest_organization_writer.rb +67 -0
  25. data/lib/multi_version_common_cartridge/writers/manifest_resources_writer.rb +48 -0
  26. data/lib/multi_version_common_cartridge/writers/manifest_writer.rb +88 -0
  27. data/lib/multi_version_common_cartridge/writers/resource_writer.rb +53 -0
  28. data/lib/multi_version_common_cartridge/writers/supported_versions.rb +37 -0
  29. data/lib/multi_version_common_cartridge/xml_definitions.rb +149 -0
  30. data/multi_version_common_cartridge.gemspec +27 -0
  31. metadata +163 -0
@@ -0,0 +1,241 @@
1
+ # multi_version_common_cartridge
2
+ # Copyright © 2019 Vista Higher Learning, Inc.
3
+ #
4
+ # multi_version_common_cartridge is free software: you can redistribute it
5
+ # and/or modify it under the terms of the GNU General Public
6
+ # License as published by the Free Software Foundation, either
7
+ # version 3 of the License, or (at your option) any later version.
8
+ #
9
+ # multi_version_common_cartridge is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+ # General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with multi_version_common_cartridge. If not, see <http://www.gnu.org/licenses/>.
16
+
17
+ module MultiVersionCommonCartridge
18
+ module Writers
19
+ class BasicLtiLinkWriter < ResourceWriter
20
+ REQUIRED_NAMESPACES = {
21
+ MultiVersionCommonCartridge::CartridgeVersions::CC_1_1_0 => {
22
+ 'xmlns' => 'http://www.imsglobal.org/xsd/imslticc_v1p0',
23
+ 'xmlns:blti' => 'http://www.imsglobal.org/xsd/imsbasiclti_v1p0',
24
+ 'xmlns:lticm' => 'http://www.imsglobal.org/xsd/imslticm_v1p0',
25
+ 'xmlns:lticp' => 'http://www.imsglobal.org/xsd/imslticp_v1p0',
26
+ 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance'
27
+ },
28
+ MultiVersionCommonCartridge::CartridgeVersions::CC_1_2_0 => {
29
+ 'xmlns' => 'http://www.imsglobal.org/xsd/imslticc_v1p2',
30
+ 'xmlns:blti' => 'http://www.imsglobal.org/xsd/imsbasiclti_v1p0',
31
+ 'xmlns:lticm' => 'http://www.imsglobal.org/xsd/imslticm_v1p0',
32
+ 'xmlns:lticp' => 'http://www.imsglobal.org/xsd/imslticp_v1p0',
33
+ 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance'
34
+ },
35
+ MultiVersionCommonCartridge::CartridgeVersions::CC_1_3_0 => {
36
+ 'xmlns' => 'http://www.imsglobal.org/xsd/imslticc_v1p3',
37
+ 'xmlns:blti' => 'http://www.imsglobal.org/xsd/imsbasiclti_v1p0',
38
+ 'xmlns:lticm' => 'http://www.imsglobal.org/xsd/imslticm_v1p0',
39
+ 'xmlns:lticp' => 'http://www.imsglobal.org/xsd/imslticp_v1p0',
40
+ 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance'
41
+ },
42
+ MultiVersionCommonCartridge::CartridgeVersions::THIN_CC_1_2_0 => {
43
+ 'xmlns' => 'http://www.imsglobal.org/xsd/imslticc_v1p2',
44
+ 'xmlns:blti' => 'http://www.imsglobal.org/xsd/imsbasiclti_v1p0',
45
+ 'xmlns:lticm' => 'http://www.imsglobal.org/xsd/imslticm_v1p0',
46
+ 'xmlns:lticp' => 'http://www.imsglobal.org/xsd/imslticp_v1p0',
47
+ 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance'
48
+ },
49
+ MultiVersionCommonCartridge::CartridgeVersions::THIN_CC_1_3_0 => {
50
+ 'xmlns' => 'http://www.imsglobal.org/xsd/imslticc_v1p3',
51
+ 'xmlns:blti' => 'http://www.imsglobal.org/xsd/imsbasiclti_v1p0',
52
+ 'xmlns:lticm' => 'http://www.imsglobal.org/xsd/imslticm_v1p0',
53
+ 'xmlns:lticp' => 'http://www.imsglobal.org/xsd/imslticp_v1p0',
54
+ 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance'
55
+ }
56
+ }.freeze
57
+ REQUIRED_SCHEMA_LOCATIONS = {
58
+ MultiVersionCommonCartridge::CartridgeVersions::CC_1_1_0 => [
59
+ [
60
+ 'http://www.imsglobal.org/xsd/imslticc_v1p0',
61
+ 'http://www.imsglobal.org/xsd/lti/ltiv1p0/imslticc_v1p0p1.xsd'
62
+ ],
63
+ [
64
+ 'http://www.imsglobal.org/xsd/imsbasiclti_v1p0',
65
+ 'http://www.imsglobal.org/xsd/lti/ltiv1p0/imsbasiclti_v1p0p1.xsd'
66
+ ],
67
+ [
68
+ 'http://www.imsglobal.org/xsd/imslticm_v1p0',
69
+ 'http://www.imsglobal.org/xsd/lti/ltiv1p0/imslticm_v1p0.xsd'
70
+ ],
71
+ [
72
+ 'http://www.imsglobal.org/xsd/imslticp_v1p0',
73
+ 'http://www.imsglobal.org/xsd/lti/ltiv1p0/imslticp_v1p0.xsd'
74
+ ]
75
+ ],
76
+ MultiVersionCommonCartridge::CartridgeVersions::CC_1_2_0 => [
77
+ [
78
+ 'http://www.imsglobal.org/xsd/imslticc_v1p2',
79
+ 'http://www.imsglobal.org/xsd/lti/ltiv1p2/imslticc_v1p2.xsd'
80
+ ],
81
+ [
82
+ 'http://www.imsglobal.org/xsd/imsbasiclti_v1p0',
83
+ 'http://www.imsglobal.org/xsd/lti/ltiv1p0/imsbasiclti_v1p0p1.xsd'
84
+ ],
85
+ [
86
+ 'http://www.imsglobal.org/xsd/imslticm_v1p0',
87
+ 'http://www.imsglobal.org/xsd/lti/ltiv1p0/imslticm_v1p0.xsd'
88
+ ],
89
+ [
90
+ 'http://www.imsglobal.org/xsd/imslticp_v1p0',
91
+ 'http://www.imsglobal.org/xsd/lti/ltiv1p0/imslticp_v1p0.xsd'
92
+ ]
93
+ ],
94
+ MultiVersionCommonCartridge::CartridgeVersions::CC_1_3_0 => [
95
+ [
96
+ 'http://www.imsglobal.org/xsd/imslticc_v1p3',
97
+ 'http://www.imsglobal.org/xsd/lti/ltiv1p3/imslticc_v1p3.xsd'
98
+ ],
99
+ [
100
+ 'http://www.imsglobal.org/xsd/imsbasiclti_v1p0',
101
+ 'http://www.imsglobal.org/xsd/lti/ltiv1p0/imsbasiclti_v1p0p1.xsd'
102
+ ],
103
+ [
104
+ 'http://www.imsglobal.org/xsd/imslticm_v1p0',
105
+ 'http://www.imsglobal.org/xsd/lti/ltiv1p0/imslticm_v1p0.xsd'
106
+ ],
107
+ [
108
+ 'http://www.imsglobal.org/xsd/imslticp_v1p0',
109
+ 'http://www.imsglobal.org/xsd/lti/ltiv1p0/imslticp_v1p0.xsd'
110
+ ]
111
+ ],
112
+ MultiVersionCommonCartridge::CartridgeVersions::THIN_CC_1_2_0 => [
113
+ [
114
+ 'http://www.imsglobal.org/xsd/imslticc_v1p2',
115
+ 'http://www.imsglobal.org/xsd/lti/ltiv1p2/imslticc_v1p2.xsd'
116
+ ],
117
+ [
118
+ 'http://www.imsglobal.org/xsd/imsbasiclti_v1p0',
119
+ 'http://www.imsglobal.org/xsd/lti/ltiv1p0/imsbasiclti_v1p0p1.xsd'
120
+ ],
121
+ [
122
+ 'http://www.imsglobal.org/xsd/imslticm_v1p0',
123
+ 'http://www.imsglobal.org/xsd/lti/ltiv1p0/imslticm_v1p0.xsd'
124
+ ],
125
+ [
126
+ 'http://www.imsglobal.org/xsd/imslticp_v1p0',
127
+ 'http://www.imsglobal.org/xsd/lti/ltiv1p0/imslticp_v1p0.xsd'
128
+ ]
129
+ ],
130
+ MultiVersionCommonCartridge::CartridgeVersions::THIN_CC_1_3_0 => [
131
+ [
132
+ 'http://www.imsglobal.org/xsd/imslticc_v1p3',
133
+ 'http://www.imsglobal.org/xsd/lti/ltiv1p3/imslticc_v1p3.xsd'
134
+ ],
135
+ [
136
+ 'http://www.imsglobal.org/xsd/imsbasiclti_v1p0',
137
+ 'http://www.imsglobal.org/xsd/lti/ltiv1p0/imsbasiclti_v1p0p1.xsd'
138
+ ],
139
+ [
140
+ 'http://www.imsglobal.org/xsd/imslticm_v1p0',
141
+ 'http://www.imsglobal.org/xsd/lti/ltiv1p0/imslticm_v1p0.xsd'
142
+ ],
143
+ [
144
+ 'http://www.imsglobal.org/xsd/imslticp_v1p0',
145
+ 'http://www.imsglobal.org/xsd/lti/ltiv1p0/imslticp_v1p0.xsd'
146
+ ]
147
+ ]
148
+ }.freeze
149
+ MESSAGES = {
150
+ no_title: 'A title is required.',
151
+ no_secure_launch_url: 'A secure launch url is required.'
152
+ }.freeze
153
+
154
+ TYPE = {
155
+ MultiVersionCommonCartridge::CartridgeVersions::CC_1_1_0 => 'imsbasiclti_xmlv1p0',
156
+ MultiVersionCommonCartridge::CartridgeVersions::CC_1_2_0 => 'imsbasiclti_xmlv1p0',
157
+ MultiVersionCommonCartridge::CartridgeVersions::CC_1_3_0 => 'imsbasiclti_xmlv1p3',
158
+ MultiVersionCommonCartridge::CartridgeVersions::THIN_CC_1_2_0 => 'imsbasiclti_xmlv1p0',
159
+ MultiVersionCommonCartridge::CartridgeVersions::THIN_CC_1_3_0 => 'imsbasiclti_xmlv1p3'
160
+ }.freeze
161
+ BASIC_LTI_LINK_FILENAME = 'basic_lti_link.xml'.freeze
162
+
163
+ def finalize
164
+ super
165
+ validate_title
166
+ validate_secure_launch_url
167
+ vendor_writer.finalize
168
+ end
169
+
170
+ def type
171
+ TYPE[@version]
172
+ end
173
+
174
+ def files
175
+ [
176
+ File.join(resource_path, BASIC_LTI_LINK_FILENAME)
177
+ ]
178
+ end
179
+
180
+ def create_files(out_dir)
181
+ FileUtils.mkdir_p(File.join(out_dir, resource_path))
182
+ doc = Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |builder|
183
+ SaxMachineNokogiriXmlSaver.new.save(
184
+ builder, basic_lti_link_element, 'cartridge_basiclti_link'
185
+ )
186
+ end
187
+ File.open(File.join(out_dir, resource_path, BASIC_LTI_LINK_FILENAME), 'w') do |file|
188
+ file.write(doc.to_xml)
189
+ end
190
+ end
191
+
192
+ def basic_lti_link_element
193
+ @basic_lti_link_element ||=
194
+ CommonCartridge::Elements::Resources::BasicLtiLink::BasicLtiLink.new.tap do |element|
195
+ element.xmlns = required_namespaces['xmlns']
196
+ element.xmlns_blti = required_namespaces['xmlns:blti']
197
+ element.xmlns_lticm = required_namespaces['xmlns:lticm']
198
+ element.xmlns_lticp = required_namespaces['xmlns:lticp']
199
+ element.xmlns_xsi = required_namespaces['xmlns:xsi']
200
+ element.xsi_schema_location = xsi_schema_location
201
+ element.title = resource.title
202
+ element.description = resource.description if resource.description
203
+ element.secure_launch_url = resource.secure_launch_url
204
+ element.vendor = vendor_writer.vendor_element
205
+ element.extensions = extensions_element
206
+ end
207
+ end
208
+
209
+ private def validate_title
210
+ raise StandardError, MESSAGES[:no_title] unless resource.title
211
+ end
212
+
213
+ private def validate_secure_launch_url
214
+ raise StandardError, MESSAGES[:no_secure_launch_url] unless resource.secure_launch_url
215
+ end
216
+
217
+ private def vendor_writer
218
+ @vendor_writer ||= BasicLtiVendorWriter.new(resource.vendor, @version)
219
+ end
220
+
221
+ private def extensions_element
222
+ resource.extensions.map do |extension|
223
+ BasicLtiExtensionWriter.new(extension, @version).extension_element
224
+ end
225
+ end
226
+
227
+ private def required_namespaces
228
+ REQUIRED_NAMESPACES[@version]
229
+ end
230
+
231
+ private def xsi_schema_location
232
+ locations = REQUIRED_SCHEMA_LOCATIONS[@version]
233
+ locations.flatten.join(' ')
234
+ end
235
+
236
+ private def resource_path
237
+ resource.identifier
238
+ end
239
+ end
240
+ end
241
+ end
@@ -0,0 +1,66 @@
1
+ # multi_version_common_cartridge
2
+ # Copyright © 2019 Vista Higher Learning, Inc.
3
+ #
4
+ # multi_version_common_cartridge is free software: you can redistribute it
5
+ # and/or modify it under the terms of the GNU General Public
6
+ # License as published by the Free Software Foundation, either
7
+ # version 3 of the License, or (at your option) any later version.
8
+ #
9
+ # multi_version_common_cartridge is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+ # General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with multi_version_common_cartridge. If not, see <http://www.gnu.org/licenses/>.
16
+
17
+ module MultiVersionCommonCartridge
18
+ module Writers
19
+ class BasicLtiVendorWriter
20
+ include SupportedVersions
21
+
22
+ MESSAGES = {
23
+ no_code: 'A code is required.',
24
+ no_name: 'A name is required.',
25
+ no_contact_email: 'A contact email is required.'
26
+ }.freeze
27
+
28
+ attr_reader :vendor
29
+
30
+ def initialize(vendor, version)
31
+ @vendor = vendor
32
+ @version = validate_version(version)
33
+ end
34
+
35
+ def finalize
36
+ validate_code
37
+ validate_name
38
+ validate_contact_email
39
+ end
40
+
41
+ def vendor_element
42
+ @vendor_element ||= CommonCartridge::Elements::Resources::BasicLtiLink::Vendor.new.tap do |element|
43
+ element.code = vendor.code
44
+ element.name = vendor.name
45
+ element.description = vendor.description if vendor.description
46
+ element.url = vendor.url if vendor.url
47
+ element.contact = CommonCartridge::Elements::Resources::BasicLtiLink::VendorContact.new(
48
+ email: vendor.contact_email
49
+ )
50
+ end
51
+ end
52
+
53
+ private def validate_code
54
+ raise StandardError, MESSAGES[:no_code] unless vendor.code
55
+ end
56
+
57
+ private def validate_name
58
+ raise StandardError, MESSAGES[:no_name] unless vendor.name
59
+ end
60
+
61
+ private def validate_contact_email
62
+ raise StandardError, MESSAGES[:no_contact_email] unless vendor.contact_email
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,85 @@
1
+ # multi_version_common_cartridge
2
+ # Copyright © 2019 Vista Higher Learning, Inc.
3
+ #
4
+ # multi_version_common_cartridge is free software: you can redistribute it
5
+ # and/or modify it under the terms of the GNU General Public
6
+ # License as published by the Free Software Foundation, either
7
+ # version 3 of the License, or (at your option) any later version.
8
+ #
9
+ # multi_version_common_cartridge is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+ # General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with multi_version_common_cartridge. If not, see <http://www.gnu.org/licenses/>.
16
+
17
+ require 'common_cartridge'
18
+
19
+ module MultiVersionCommonCartridge
20
+ module Writers
21
+ class CartridgeWriter
22
+ include SupportedVersions
23
+
24
+ attr_reader :cartridge
25
+
26
+ def initialize(cartridge, version)
27
+ @cartridge = cartridge
28
+ @finalized = false
29
+ @version = validate_version(version)
30
+ end
31
+
32
+ def finalize
33
+ manifest_writer.finalize
34
+ cartridge.all_items.each { |item| item_writer(item).finalize }
35
+ cartridge.all_resources.each { |resource| resource_writer(resource).finalize }
36
+ @finalized = true
37
+ end
38
+
39
+ def write_in_dir(dir)
40
+ # Should we raise, or should we call finalize from here?
41
+ raise StandardError, 'the cartridge has not been finalized' unless @finalized
42
+ FileUtils.mkdir_p(dir)
43
+ manifest_writer.write(dir)
44
+ cartridge.all_resources.each do |resource|
45
+ resource_writer(resource).create_files(dir)
46
+ end
47
+ end
48
+
49
+ def write_to_zip(filename)
50
+ Dir.mktmpdir do |dir|
51
+ write_in_dir(dir)
52
+ zip_dir(filename, dir)
53
+ end
54
+ end
55
+
56
+ private def zip_dir(filename, dir)
57
+ Zip::OutputStream.open(filename) { |zos| } # make sure it's a zip
58
+
59
+ Zip::File.open(filename, Zip::File::CREATE) do |zipfile|
60
+ base_path = Pathname.new(dir)
61
+ Dir["#{dir}/**/*"].each do |file|
62
+ entry = Pathname.new(file).relative_path_from(base_path)
63
+ zipfile.add(entry, file)
64
+ end
65
+ end
66
+ end
67
+
68
+ private def writers_factory
69
+ @writers_factory ||= Factory.new(cartridge, @version)
70
+ end
71
+
72
+ private def manifest_writer
73
+ writers_factory.manifest_writer
74
+ end
75
+
76
+ private def item_writer(item)
77
+ writers_factory.item_writer(item)
78
+ end
79
+
80
+ private def resource_writer(resource)
81
+ writers_factory.resource_writer(resource)
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,59 @@
1
+ # multi_version_common_cartridge
2
+ # Copyright © 2019 Vista Higher Learning, Inc.
3
+ #
4
+ # multi_version_common_cartridge is free software: you can redistribute it
5
+ # and/or modify it under the terms of the GNU General Public
6
+ # License as published by the Free Software Foundation, either
7
+ # version 3 of the License, or (at your option) any later version.
8
+ #
9
+ # multi_version_common_cartridge is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+ # General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with multi_version_common_cartridge. If not, see <http://www.gnu.org/licenses/>.
16
+
17
+ module MultiVersionCommonCartridge
18
+ module Writers
19
+ class Factory
20
+ RESOURCE_WRITERS = {
21
+ ::MultiVersionCommonCartridge::Resources::BasicLtiLink::BasicLtiLink => BasicLtiLinkWriter
22
+ }.freeze
23
+
24
+ def initialize(cartridge, version)
25
+ @cartridge = cartridge
26
+ @version = version
27
+ @item_writers = {}
28
+ @resource_writers = {}
29
+ end
30
+
31
+ def manifest_writer
32
+ @manifest_writer ||= ManifestWriter.new(@cartridge.manifest, self, @version)
33
+ end
34
+
35
+ def manifest_metadata_writer
36
+ @manifest_metadata_writer ||= ManifestMetadataWriter.new(@cartridge.manifest, @version)
37
+ end
38
+
39
+ def manifest_organization_writer
40
+ @manifest_organization_writer ||= ManifestOrganizationWriter.new(@cartridge, self, @version)
41
+ end
42
+
43
+ def manifest_resources_writer
44
+ @manifest_resources_writer ||= ManifestResourcesWriter.new(@cartridge, self, @version)
45
+ end
46
+
47
+ def item_writer(item)
48
+ @item_writers[item] ||= ItemWriter.new(item, self, @version)
49
+ end
50
+
51
+ def resource_writer(resource)
52
+ return @resource_writers[resource] if @resource_writers.key?(resource)
53
+ writer_class = RESOURCE_WRITERS[resource.class]
54
+ raise "Unknown resource '#{resource.class.name}'" unless writer_class
55
+ @resource_writers[resource] = writer_class.new(resource, @version)
56
+ end
57
+ end
58
+ end
59
+ end