common_cartridge_parser 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. checksums.yaml +7 -0
  2. data/lib/common_cartridge.rb +40 -0
  3. data/lib/common_cartridge/elements/lom.rb +77 -0
  4. data/lib/common_cartridge/elements/manifest.rb +30 -0
  5. data/lib/common_cartridge/elements/metadata.rb +12 -0
  6. data/lib/common_cartridge/elements/organizations.rb +40 -0
  7. data/lib/common_cartridge/elements/outcomes.rb +10 -0
  8. data/lib/common_cartridge/elements/outcomes/outcome.rb +15 -0
  9. data/lib/common_cartridge/elements/outcomes/outcome_group.rb +12 -0
  10. data/lib/common_cartridge/elements/outcomes/outcome_root.rb +11 -0
  11. data/lib/common_cartridge/elements/resources.rb +119 -0
  12. data/lib/common_cartridge/elements/resources/assessment.rb +21 -0
  13. data/lib/common_cartridge/elements/resources/assignment.rb +59 -0
  14. data/lib/common_cartridge/elements/resources/attachments.rb +20 -0
  15. data/lib/common_cartridge/elements/resources/basic_lti_link.rb +66 -0
  16. data/lib/common_cartridge/elements/resources/page.rb +22 -0
  17. data/lib/common_cartridge/elements/resources/topic.rb +28 -0
  18. data/lib/common_cartridge/elements/resources/web_link.rb +25 -0
  19. data/lib/common_cartridge/package.rb +87 -0
  20. data/lib/common_cartridge/parsers/dependencies.rb +23 -0
  21. data/lib/common_cartridge/parsers/files.rb +34 -0
  22. data/lib/common_cartridge/parsers/items.rb +58 -0
  23. data/lib/common_cartridge/parsers/outcomes.rb +17 -0
  24. data/lib/common_cartridge/parsers/parser.rb +84 -0
  25. data/lib/common_cartridge/parsers/questions.rb +34 -0
  26. data/lib/common_cartridge/version.rb +3 -0
  27. data/spec/elements/lom_spec.rb +66 -0
  28. data/spec/elements/manifest_spec.rb +31 -0
  29. data/spec/elements/metadata_spec.rb +40 -0
  30. data/spec/elements/organizations_spec.rb +51 -0
  31. data/spec/elements/outcomes/outcome_group_spec.rb +29 -0
  32. data/spec/elements/outcomes/outcome_spec.rb +37 -0
  33. data/spec/elements/resources/assignment_spec.rb +53 -0
  34. data/spec/elements/resources/attachments_spec.rb +24 -0
  35. data/spec/elements/resources/basic_lti_link_spec.rb +29 -0
  36. data/spec/elements/resources/resources_spec.rb +73 -0
  37. data/spec/elements/resources/topic_spec.rb +39 -0
  38. data/spec/elements/resources/web_link_spec.rb +31 -0
  39. data/spec/files/canvas_large_1.3.imscc +0 -0
  40. data/spec/files/canvas_small_1.1.corrupt.imscc +0 -0
  41. data/spec/files/canvas_small_1.1.imscc +0 -0
  42. data/spec/package_spec.rb +110 -0
  43. data/spec/parser_spec.rb +13 -0
  44. data/spec/spec_helper.rb +13 -0
  45. metadata +190 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: acf72a78d5676760c4512914ebcf334b0dbd8d5fc7a30ee6700f5b1dd4e34dd4
4
+ data.tar.gz: 3203fd4fa50225535fee228b3753ca22a1ba683ae679426b5a7767f32b51b6c6
5
+ SHA512:
6
+ metadata.gz: 882e19dca1111f76ae6f76743cdc169f94d5e711b06e0aa5465e96404567b614f1c12fe8a7c867b446065ec8bddd9cd7086be6bf02d47fbb2936afe6b463f3c2
7
+ data.tar.gz: f4ed5e8151f36623f74d97fa01092733844e622b6edff1bcfafc0a8ea377ab35032dcd55838b306f37c174affc2ded0c9b76a7ab0c7cdc7d37739a36f054b304
@@ -0,0 +1,40 @@
1
+ require 'sax-machine'
2
+
3
+ module CommonCartridge
4
+ def self.parse_from_zip(zipfile)
5
+ parser = Parsers::Parser.new(zipfile)
6
+ package = parser.parse
7
+ package
8
+ end
9
+
10
+ # todo: methods to detect version and inject resultant namespace
11
+
12
+ # Configuration
13
+ class << self
14
+ attr_accessor :config
15
+ end
16
+
17
+ def self.configure
18
+ self.config ||= Config.new
19
+ yield(config)
20
+ end
21
+
22
+ class Config
23
+ attr_accessor :import_directory, :export_directory
24
+
25
+ def initialize
26
+ @import_directory = '.'
27
+ @export_directory = '/tmp'
28
+ end
29
+ end
30
+ end
31
+
32
+ require 'common_cartridge/elements/lom'
33
+ require 'common_cartridge/elements/metadata'
34
+ require 'common_cartridge/elements/organizations'
35
+ require 'common_cartridge/elements/resources'
36
+ require 'common_cartridge/elements/outcomes'
37
+ require 'common_cartridge/elements/manifest'
38
+ require 'common_cartridge/package'
39
+ require 'common_cartridge/parsers/parser'
40
+ require 'common_cartridge/version'
@@ -0,0 +1,77 @@
1
+ module CommonCartridge
2
+ module Elements
3
+ module Lom
4
+ class LanguageString
5
+ include SAXMachine
6
+
7
+ attribute :language
8
+ value :value
9
+ end
10
+
11
+ class Title
12
+ include SAXMachine
13
+
14
+ # http://www.imsglobal.org/profile/cc/ccv1p3/LOM/ccv1p3_lommanifest_v1p0.xsd
15
+ elements 'lomimscc:string', as: :strings, class: CommonCartridge::Elements::Lom::LanguageString
16
+
17
+ # backward compatibility
18
+ def value; strings.first.value; end
19
+ def language; strings.first.language; end
20
+
21
+ def values
22
+ strings.map do |str|
23
+ [str.language, str.value]
24
+ end.to_h
25
+ end
26
+
27
+ def to_s; string; end
28
+ end
29
+
30
+ class Description
31
+ include SAXMachine
32
+
33
+ element 'lomimscc:string', as: :value
34
+ element 'lomimscc:string', value: :language, as: :language
35
+
36
+ def to_s; string; end
37
+ end
38
+
39
+ class Keyword
40
+ include SAXMachine
41
+
42
+ element 'lomimscc:string', as: :value
43
+ element 'lomimscc:string', value: :language, as: :language
44
+
45
+ def to_s; string; end
46
+ end
47
+
48
+ class CopyrightAndOtherRestrictions
49
+ include SAXMachine
50
+
51
+ element :value
52
+ end
53
+
54
+ class Rights
55
+ include SAXMachine
56
+
57
+ element 'lomimscc:description', as: :description, class: CommonCartridge::Elements::Lom::Description
58
+ element :copyrightAndOtherRestrictions, class: CommonCartridge::Elements::Lom::CopyrightAndOtherRestrictions
59
+ end
60
+
61
+ class General
62
+ include SAXMachine
63
+
64
+ element 'lomimscc:title', as: :title, class: CommonCartridge::Elements::Lom::Title
65
+ element 'lomimscc:description', as: :description, class: CommonCartridge::Elements::Lom::Description
66
+ elements 'lomimscc:keyword', as: :keywords, class: CommonCartridge::Elements::Lom::Keyword
67
+ end
68
+
69
+ class Lom
70
+ include SAXMachine
71
+
72
+ element 'lomimscc:general', as: :general, class: CommonCartridge::Elements::Lom::General
73
+ element :rights, class: CommonCartridge::Elements::Lom::Rights
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,30 @@
1
+ module CommonCartridge
2
+ module Elements
3
+ class Manifest
4
+ include SAXMachine
5
+
6
+ element :metadata, class: CommonCartridge::Elements::Metadata
7
+ element :organizations, class: CommonCartridge::Elements::Organizations::RootOrganization, as: :root_organization
8
+ element :resources, class: CommonCartridge::Elements::Resources::RootResource, as: :root_resource
9
+ attribute :identifier, required: true
10
+ attribute :xmlns
11
+ attribute 'xmlns:lomimscc', as: :xmlns_lomimscc
12
+ attribute 'xmlns:lom', as: :xmlns_lom
13
+ attribute 'xmlns:xsi', as: :xmlns_xsi
14
+ attribute 'xsi:schemaLocation', as: :xsi_schema_location
15
+
16
+ def organizations
17
+ root_organization.organizations
18
+ end
19
+
20
+ def resources
21
+ root_resource.resources
22
+ end
23
+
24
+ def organization
25
+ organizations.first
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,12 @@
1
+ module CommonCartridge
2
+ module Elements
3
+ class Metadata
4
+ include SAXMachine
5
+
6
+ element :schema
7
+ element :schemaversion
8
+
9
+ element 'lomimscc:lom', as: :lom, class: CommonCartridge::Elements::Lom::Lom
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,40 @@
1
+ module CommonCartridge
2
+ module Elements
3
+ module Organizations
4
+ class Item
5
+ attr_accessor :type
6
+
7
+ include SAXMachine
8
+
9
+ attribute :identifier
10
+ attribute :identifierref
11
+ element :title
12
+
13
+ elements :item, class: self, as: :items
14
+ end
15
+
16
+ class RootItem
17
+ include SAXMachine
18
+
19
+ attribute :identifier
20
+ elements :item, class: Item, as: :items
21
+
22
+ # todo: implement 'find_by' method here to quickly scan for particular items
23
+ end
24
+
25
+ class Organization
26
+ include SAXMachine
27
+
28
+ attribute :identifier
29
+ attribute :structure
30
+ element :item, class: RootItem, as: :root_item
31
+ end
32
+
33
+ class RootOrganization
34
+ include SAXMachine
35
+
36
+ elements :organization, class: Organization, as: :organizations
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,10 @@
1
+ require 'common_cartridge/elements/outcomes/outcome'
2
+ require 'common_cartridge/elements/outcomes/outcome_group'
3
+ require 'common_cartridge/elements/outcomes/outcome_root'
4
+ module CommonCartridge
5
+ module Elements
6
+ module Outcomes
7
+ end
8
+ end
9
+ end
10
+
@@ -0,0 +1,15 @@
1
+ module CommonCartridge
2
+ module Elements
3
+ module Outcomes
4
+ class Outcome
5
+ include SAXMachine
6
+
7
+ attribute :identifier
8
+
9
+ element :title
10
+ element :points_possible
11
+ element :mastery_points
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,12 @@
1
+ module CommonCartridge
2
+ module Elements
3
+ module Outcomes
4
+ class OutcomeGroup
5
+ include SAXMachine
6
+
7
+ attribute :identifier
8
+ elements 'learningOutcome', as: :outcomes, class: Outcome
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ module CommonCartridge
2
+ module Elements
3
+ module Outcomes
4
+ class OutcomeRoot
5
+ include SAXMachine
6
+
7
+ elements 'learningOutcomeGroup', as: :outcome_groups, class: OutcomeGroup
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,119 @@
1
+ require 'common_cartridge/elements/resources/attachments'
2
+ require 'common_cartridge/elements/resources/web_link'
3
+ require 'common_cartridge/elements/resources/topic'
4
+ require 'common_cartridge/elements/resources/assignment'
5
+ require 'common_cartridge/elements/resources/assessment'
6
+ require 'common_cartridge/elements/resources/page'
7
+ require 'common_cartridge/elements/resources/basic_lti_link'
8
+
9
+ module CommonCartridge
10
+ module Elements
11
+ module Resources
12
+
13
+ def self.type_mappings
14
+ {
15
+ Topic.pattern => Topic,
16
+ WebLink.pattern => WebLink,
17
+ Assignment.pattern => Assignment,
18
+ Assessment.pattern => Assessment,
19
+ Page.pattern => Page,
20
+ BasicLtiLink::BasicLtiLink.pattern => BasicLtiLink::BasicLtiLink
21
+ }
22
+ end
23
+
24
+
25
+ class Content
26
+ include SAXMachine
27
+
28
+ element :title
29
+ element :points_possible
30
+ element :type
31
+ end
32
+
33
+ class Dependency
34
+ attr_writer :contents
35
+
36
+ include SAXMachine
37
+ attribute :identifierref
38
+
39
+ def contents
40
+ @contents ||= []
41
+ end
42
+
43
+ def title
44
+ if c = contents.detect { |content| !content.title.to_s.empty? }
45
+ return c.title
46
+ end
47
+ end
48
+
49
+ def points_possible
50
+ if c = contents.detect { |content| !content.points_possible.to_s.empty? }
51
+ return c.points_possible
52
+ end
53
+ end
54
+ end
55
+
56
+ class File
57
+ attr_accessor :content
58
+
59
+ include SAXMachine
60
+ attribute :href
61
+ element :attachments, class: Attachments::RootAttachment, as: :attachment_root
62
+ elements :dependency, class: Dependency, as: :dependencies
63
+
64
+ def attachments; attachment_root.attachments; end
65
+ end
66
+
67
+ class Resource
68
+ attr_accessor :question_count
69
+ attr_writer :title, :points_possible
70
+
71
+ include SAXMachine
72
+ attribute :identifier
73
+ attribute :type
74
+ attribute :href
75
+ attribute :intendeduse
76
+
77
+ elements :file, class: File, as: :files
78
+ elements :dependency, class: Dependency, as: :dependencies
79
+
80
+
81
+ # Switch statement based on 'type'
82
+ def points_possible
83
+ @points_possible ||= if dependency = dependencies.detect { |d| d.points_possible && !d.points_possible.empty? }
84
+ dependency.points_possible
85
+ end
86
+ end
87
+
88
+ def title
89
+ @title ||= if file = files.detect { |f| f.content && f.content.title && !f.content.title.empty? }
90
+ file.content.title
91
+ elsif dependency = dependencies.detect { |d| d.title && !d.title.empty? }
92
+ dependency.title
93
+ end
94
+ end
95
+
96
+ def file_locations
97
+ [
98
+ ::File.join(identifier, "assessment.xml"),
99
+ ::File.join(identifier, "assessment_qti.xml"),
100
+ ::File.join("non_cc_assessments", "#{identifier}.xml"),
101
+ ::File.join("non_cc_assessments", "#{identifier}.xml.qti")
102
+ ]
103
+ end
104
+
105
+ def is_announcement?
106
+ !!dependencies.collect(&:contents).flatten.detect { |c| c.type == 'announcement' }
107
+ end
108
+ end
109
+
110
+ class RootResource
111
+ include SAXMachine
112
+
113
+ elements :resource, class: Resource, as: :resources
114
+
115
+ end
116
+
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,21 @@
1
+ module CommonCartridge
2
+ module Elements
3
+ module Resources
4
+ class Assessment
5
+ attr_accessor :title, :identifier
6
+
7
+ include SAXMachine
8
+
9
+ element :points_possible
10
+
11
+ def self.type
12
+ :quiz
13
+ end
14
+
15
+ def self.pattern
16
+ /assessment|quiz/
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,59 @@
1
+ module CommonCartridge
2
+ module Elements
3
+ module Resources
4
+ class Format
5
+ include SAXMachine
6
+
7
+ attribute :type
8
+ end
9
+
10
+ class SubmissionFormats
11
+ include SAXMachine
12
+
13
+ elements :format, class: Format, as: :formats
14
+ end
15
+
16
+ class Text
17
+ include SAXMachine
18
+
19
+ attribute :href
20
+ attribute :texttype, as: :text_type
21
+ value :value
22
+ end
23
+
24
+ class InstructorText
25
+ include SAXMachine
26
+
27
+ attribute :href
28
+ attribute :texttype, as: :text_type
29
+ value :value
30
+ end
31
+
32
+ class Assignment
33
+ attr_accessor :identifier, :points_possible
34
+
35
+ include SAXMachine
36
+
37
+ element :title
38
+ element :text, class: Text
39
+ element :instructor_text, class: InstructorText
40
+ element :gradable
41
+
42
+ element :submission_formats, class: SubmissionFormats
43
+ element :attachments, class: CommonCartridge::Elements::Resources::Attachments::RootAttachment, as: :attachment_root
44
+
45
+ def attachments
46
+ attachment_root.attachments
47
+ end
48
+
49
+ def self.type
50
+ :assignment
51
+ end
52
+
53
+ def self.pattern
54
+ /assignment|associatedcontent\/imscc_xmlv1p1\/learning\-application\-resource/
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end