TriviaMD 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/quizzes_v1.rb +59 -0
  3. data/lib/trivia_parser.rb +68 -33
  4. metadata +3 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cb5c0aca381ec6eb42c430427f21bde8665dc0737d0d0f2aa319756706efa229
4
- data.tar.gz: 0c7b644fdd2a304ed07c430485460e6b7ba86c5507fdf96627badc1c4517258c
3
+ metadata.gz: aaa9f99ee0dbed631214a87e322f9e5a1e060e80468c2aa9bab0fb42c56aaa58
4
+ data.tar.gz: 7a46023d6944327e917686fc40e421ea80d9bd021dc51fd4dd2e77c6647e1558
5
5
  SHA512:
6
- metadata.gz: bf80c938e5921e87a6a0eb93e8d1ab94dbec0c86b3f84b692bbfef99bc6830eb13b80b588593988aee6d3876f72de22419ff471d2357f40f8535d02f79966818
7
- data.tar.gz: 11c6b2b5c8a523321ae0a108804272adff924a280793e81e92e0526ce82d2bd6d2dea92a7b890827beae891061d61c1e8f6018479e56217f4d7f19cc89ed05ee
6
+ metadata.gz: 07e955e9bd7929ab1adc1934a7a74e1dff752ac199d5d859aae16057c8a99446a5c7e2bd32280ccaf3d63d60dd73f32e698642716b4b143e16c434985f4c0f4e
7
+ data.tar.gz: b979eb1de13ef3b65bb79ab04b4e5959b294ecb7171c503268d181e724f36d3867fd89ce8e2c9e60580d906ac2e457f81f7ec18d7681f8b56cb21e77683aba42
data/lib/quizzes_v1.rb ADDED
@@ -0,0 +1,59 @@
1
+ require 'kramdown'
2
+ require 'httparty'
3
+
4
+ class QuizzesV1
5
+ def initialize(course_id)
6
+ @course_id = course_id
7
+ end
8
+
9
+ def quiz_base_url
10
+ "https://#{ENV.fetch("CANVAS_DOMAIN")}/api/v1/courses/#{@course_id}/quizzes"
11
+ end
12
+
13
+ def options(body=nil)
14
+ options = {
15
+ headers: { 'Authorization' => "Bearer #{ENV.fetch("CANVAS_API_TOKEN")}" },
16
+ format: :json
17
+ }
18
+ unless body.nil?
19
+ options[:headers]['Content-Type'] = 'application/json'
20
+ options[:body] = body.to_json
21
+ end
22
+ options
23
+ end
24
+
25
+ def create_quiz(title)
26
+ body = {
27
+ quiz: {
28
+ title: title
29
+ }
30
+ }
31
+ response = HTTParty.send(:post, quiz_base_url, options(body)).parsed_response
32
+ response["id"]
33
+ end
34
+
35
+ def create_question(quiz_id,question)
36
+ puts quiz_id
37
+ url = quiz_base_url + "/#{quiz_id}/questions"
38
+ body = {
39
+ question: {
40
+ question_text: question["question"],
41
+ question_type: "multiple_answers_question"
42
+ }
43
+ }
44
+ body[:question][:answers] = question["answers"].map do |a|
45
+ {
46
+ answer_text: a["text"],
47
+ answer_weight: a["isAnswer"] == true ? 100 : 0
48
+ }
49
+ end
50
+ response = HTTParty.send(:post, url, options(body)).parsed_response
51
+ response
52
+ end
53
+
54
+ def create_from_trivia(trivias)
55
+ parsed = JSON.parse(trivias)
56
+ quiz_id = create_quiz("Catalog quiz #{Time.now.iso8601}")
57
+ parsed.each { |q| create_question(quiz_id, q) }
58
+ end
59
+ end
data/lib/trivia_parser.rb CHANGED
@@ -6,49 +6,84 @@ class TriviaParser
6
6
  @debug = debug
7
7
  end
8
8
 
9
+
10
+ def event_trivia_header(c)
11
+ c.type == :header && c.options[:level] == 2 && c.options[:raw_text] == "Trivia"
12
+ end
13
+
14
+ def event_header(c)
15
+ c.type == :header
16
+ end
17
+
18
+ def event_reason(c)
19
+ c.type == :p && c.children.first.type == :codespan
20
+ end
21
+
22
+ def event_answers(c)
23
+ c.type == :ul
24
+ end
25
+
26
+ def event_question(c)
27
+ c.type == :blockquote
28
+ end
29
+
30
+ def get_event(c)
31
+ return :event_trivia_header if event_trivia_header(c)
32
+ return :event_header if event_header(c)
33
+ return :event_reason if event_reason(c)
34
+ return :event_answers if event_answers(c)
35
+ return :event_question if event_question(c)
36
+ return :no_event
37
+ end
38
+
39
+
40
+ def parse_answers(c)
41
+ c.children.map do |li|
42
+ head = li.children.first.children.first
43
+ if head.type == :strong
44
+ {
45
+ text: head.children.first.value,
46
+ isAnswer: true
47
+ }
48
+ else
49
+ {
50
+ text: head.value
51
+ }
52
+ end
53
+ end
54
+ end
9
55
  def parse
10
56
  @doc = Kramdown::Document.new(@file)
11
57
  state = :not_found
12
58
  questions = []
13
59
  question = {}
60
+ puts '-------------------------------------------'
14
61
  @doc.root.children.each do |c|
15
- p [state, c.type] if @debug
62
+ p [state, c.type, get_event(c)]
63
+ event = get_event(c)
16
64
 
17
- if state == :trivia_started && c.type == :header && c.options[:level] == 2
18
- state = :not_found
19
- end
20
-
21
- if c.type == :header && c.options[:level] == 2 && c.options[:raw_text] == "Trivia"
65
+ case event
66
+ when :event_trivia_header
22
67
  state = :trivia_started
23
68
  question = {}
24
- end
25
-
26
- if state == :answer_parsing && c.type == :ul
27
- answers = []
28
- c.children.map do |li|
29
- head = li.children.first.children.first
30
- if head.type == :strong
31
- answers.push({
32
- text: head.children.first.value,
33
- isAnswer: true
34
- })
35
- else
36
- answers.push({
37
- text: head.value
38
- })
39
- end
69
+ when :event_reason
70
+ if state == :reason_parsing
71
+ question[:reason] = c.children.first.value
72
+ state = :trivia_started
73
+ end
74
+ when :event_answers
75
+ if state == :answer_parsing
76
+ question[:answers] = parse_answers(c)
77
+ state = :reason_parsing
78
+ end
79
+ when :event_question
80
+ if state == :trivia_started || state == :reason_parsing
81
+ questions.push(question) unless question.empty?
82
+ question = {
83
+ question: c.children.first.children.first.value
84
+ }
85
+ state = :answer_parsing
40
86
  end
41
-
42
- question[:answers] = answers
43
- state = :trivia_started
44
- end
45
-
46
- if state == :trivia_started && c.type == :blockquote
47
- questions.push(question) unless question.empty?
48
- question = {
49
- question: c.children.first.children.first.value
50
- }
51
- state = :answer_parsing
52
87
  end
53
88
  end
54
89
  questions.push(question) unless question.empty?
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: TriviaMD
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bence Pjatacsuk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-10-28 00:00:00.000000000 Z
11
+ date: 2022-10-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -60,6 +60,7 @@ extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
62
  - bin/trivia-md
63
+ - lib/quizzes_v1.rb
63
64
  - lib/trivia_md.rb
64
65
  - lib/trivia_parser.rb
65
66
  homepage: https://rubygems.org/gems/trivia_md