canvas_sync 0.3.23 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +2 -0
  3. data/lib/canvas_sync.rb +11 -1
  4. data/lib/canvas_sync/generators/templates/services/live_events/assignment/assignment_created_event.rb +5 -4
  5. data/lib/canvas_sync/generators/templates/services/live_events/assignment/assignment_event.rb +40 -49
  6. data/lib/canvas_sync/generators/templates/services/live_events/assignment/assignment_updated_event.rb +5 -3
  7. data/lib/canvas_sync/generators/templates/services/live_events/base_event.rb +12 -19
  8. data/lib/canvas_sync/generators/templates/services/live_events/course/course_created_event.rb +5 -3
  9. data/lib/canvas_sync/generators/templates/services/live_events/course/course_event.rb +32 -38
  10. data/lib/canvas_sync/generators/templates/services/live_events/course/course_updated_event.rb +5 -3
  11. data/lib/canvas_sync/generators/templates/services/live_events/enrollment/enrollment_created_event.rb +5 -3
  12. data/lib/canvas_sync/generators/templates/services/live_events/enrollment/enrollment_event.rb +53 -56
  13. data/lib/canvas_sync/generators/templates/services/live_events/enrollment/enrollment_updated_event.rb +5 -3
  14. data/lib/canvas_sync/generators/templates/services/live_events/grade/grade_changed_event.rb +5 -3
  15. data/lib/canvas_sync/generators/templates/services/live_events/grade/grade_event.rb +47 -48
  16. data/lib/canvas_sync/generators/templates/services/live_events/module/module_created_event.rb +6 -0
  17. data/lib/canvas_sync/generators/templates/services/live_events/module/module_event.rb +39 -0
  18. data/lib/canvas_sync/generators/templates/services/live_events/module/module_updated_event.rb +6 -0
  19. data/lib/canvas_sync/generators/templates/services/live_events/submission/submission_created_event.rb +5 -3
  20. data/lib/canvas_sync/generators/templates/services/live_events/submission/submission_event.rb +40 -45
  21. data/lib/canvas_sync/generators/templates/services/live_events/submission/submission_updated_event.rb +5 -3
  22. data/lib/canvas_sync/generators/templates/services/live_events/syllabus/syllabus_event.rb +29 -30
  23. data/lib/canvas_sync/generators/templates/services/live_events/syllabus/syllabus_updated_event.rb +4 -2
  24. data/lib/canvas_sync/generators/templates/services/live_events/user/user_created_event.rb +5 -3
  25. data/lib/canvas_sync/generators/templates/services/live_events/user/user_event.rb +5 -8
  26. data/lib/canvas_sync/generators/templates/services/live_events/user/user_updated_event.rb +5 -3
  27. data/lib/canvas_sync/version.rb +1 -1
  28. data/spec/canvas_sync/services/module_event_spec.rb +42 -0
  29. data/spec/dummy/app/services/live_events/assignment_created_event.rb +12 -0
  30. data/spec/dummy/app/services/live_events/assignment_event.rb +60 -0
  31. data/spec/dummy/app/services/live_events/assignment_updated_event.rb +12 -0
  32. data/spec/dummy/app/services/live_events/base_event.rb +22 -0
  33. data/spec/dummy/app/services/live_events/course_created_event.rb +12 -0
  34. data/spec/dummy/app/services/live_events/course_event.rb +42 -0
  35. data/spec/dummy/app/services/live_events/course_updated_event.rb +12 -0
  36. data/spec/dummy/app/services/live_events/enrollment_created_event.rb +12 -0
  37. data/spec/dummy/app/services/live_events/enrollment_event.rb +61 -0
  38. data/spec/dummy/app/services/live_events/enrollment_updated_event.rb +12 -0
  39. data/spec/dummy/app/services/live_events/grade_changed_event.rb +12 -0
  40. data/spec/dummy/app/services/live_events/grade_event.rb +61 -0
  41. data/spec/dummy/app/services/live_events/module_created_event.rb +12 -0
  42. data/spec/dummy/app/services/live_events/module_event.rb +45 -0
  43. data/spec/dummy/app/services/live_events/module_updated_event.rb +12 -0
  44. data/spec/dummy/app/services/live_events/submission_created_event.rb +12 -0
  45. data/spec/dummy/app/services/live_events/submission_event.rb +58 -0
  46. data/spec/dummy/app/services/live_events/submission_updated_event.rb +12 -0
  47. data/spec/dummy/app/services/live_events/syllabus_event.rb +40 -0
  48. data/spec/dummy/app/services/live_events/syllabus_updated_event.rb +12 -0
  49. data/spec/dummy/app/services/live_events/user_created_event.rb +12 -0
  50. data/spec/dummy/app/services/live_events/user_event.rb +38 -0
  51. data/spec/dummy/app/services/live_events/user_updated_event.rb +12 -0
  52. data/spec/dummy/log/development.log +666 -0
  53. data/spec/dummy/log/test.log +3758 -0
  54. data/spec/support/fake_canvas.rb +4 -0
  55. data/spec/support/fixtures/canvas_responses/module.json +12 -0
  56. metadata +57 -4
@@ -0,0 +1,6 @@
1
+ # <%= autogenerated_event_warning %>
2
+
3
+ module LiveEvents
4
+ class ModuleCreatedEvent < LiveEvents::ModuleEvent
5
+ end
6
+ end
@@ -0,0 +1,39 @@
1
+ # <%= autogenerated_event_warning %>
2
+
3
+ module LiveEvents
4
+ class ModuleEvent < LiveEvents::BaseEvent
5
+ # The following is provided in the live events call:
6
+ # {
7
+ # module_id,
8
+ # context_id,
9
+ # context_type,
10
+ # name,
11
+ # position,
12
+ # workflow_state,
13
+ # }
14
+
15
+ def perform(_event_payload)
16
+ super
17
+ create_or_update_from_api(payload)
18
+ end
19
+
20
+ private
21
+
22
+ def create_or_update_from_api(payload)
23
+ return unless payload["context_type"] == "Course"
24
+ context_module = ContextModule.find_or_initialize_by(canvas_context_module_id: payload["module_id"])
25
+ api_params = canvas_sync_client.course_module(payload["context_id"], payload["module_id"])
26
+
27
+ context_module.assign_attributes(
28
+ canvas_context_id: payload["context_id"],
29
+ canvas_context_type: payload["context_type"],
30
+ position: api_params["position"],
31
+ name: api_params["name"],
32
+ workflow_state: payload["workflow_state"],
33
+ unlock_at: api_params["unlock_at"],
34
+ )
35
+
36
+ context_module.save! if context_module.changed?
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,6 @@
1
+ # <%= autogenerated_event_warning %>
2
+
3
+ module LiveEvents
4
+ class ModuleUpdatedEvent < LiveEvents::ModuleEvent
5
+ end
6
+ end
@@ -1,4 +1,6 @@
1
- <%= autogenerated_event_warning %>
1
+ # <%= autogenerated_event_warning %>
2
2
 
3
- class LiveEvents::SubmissionCreatedEvent < LiveEvents::SubmissionEvent
4
- end
3
+ module LiveEvents
4
+ class SubmissionCreatedEvent < LiveEvents::SubmissionEvent
5
+ end
6
+ end
@@ -1,57 +1,52 @@
1
- <%= autogenerated_event_warning %>
1
+ # <%= autogenerated_event_warning %>
2
2
 
3
- class LiveEvents::SubmissionEvent < LiveEvents::BaseEvent
3
+ module LiveEvents
4
+ class SubmissionEvent < LiveEvents::BaseEvent
5
+ # The following is provided in the live events call:
6
+ # {
7
+ # submission_id: submission.global_id,
8
+ # assignment_id: submission.global_assignment_id,
9
+ # user_id: submission.global_user_id,
10
+ # submitted_at: submission.submitted_at,
11
+ # graded_at: submission.graded_at,
12
+ # updated_at: submission.updated_at,
13
+ # score: submission.score,
14
+ # grade: submission.grade,
15
+ # submission_type: submission.submission_type,
16
+ # body: LiveEvents.truncate(submission.body),
17
+ # url: submission.url,
18
+ # attempt: submission.attempt
19
+ # }
4
20
 
5
- # The following is provided in the live events call:
6
- # {
7
- # submission_id: submission.global_id,
8
- # assignment_id: submission.global_assignment_id,
9
- # user_id: submission.global_user_id,
10
- # submitted_at: submission.submitted_at,
11
- # graded_at: submission.graded_at,
12
- # updated_at: submission.updated_at,
13
- # score: submission.score,
14
- # grade: submission.grade,
15
- # submission_type: submission.submission_type,
16
- # body: LiveEvents.truncate(submission.body),
17
- # url: submission.url,
18
- # attempt: submission.attempt
19
- # }
20
-
21
- def perform(event_payload)
22
- super
23
- return if assignment.nil? || user.nil?
24
- attrs = {
21
+ def perform(event_payload)
22
+ super
23
+ return if assignment.nil? || user.nil?
24
+ attrs = {
25
25
  canvas_submission_id: local_canvas_id(payload[:submission_id]),
26
- assignment_id: assignment.try(:id),
27
- user_id: user.try(:id),
26
+ canvas_assignment_id: assignment.try(:id),
27
+ canvas_user_id: user.try(:id),
28
28
  submitted_at: payload[:submitted_at],
29
29
  graded_at: payload[:graded_at],
30
30
  score: payload[:score],
31
- grade: payload[:grade],
32
- submission_type: payload[:submission_type]
33
- }
34
- create_or_update(attrs)
35
- rescue => e
36
- log_error e
37
- ensure
38
- true
31
+ }
32
+ create_or_update(attrs)
33
+ end
39
34
  end
40
- end
41
35
 
42
- private
43
- def create_or_update(attrs)
44
- submission = Submission.where(canvas_submission_id: attrs[:canvas_submission_id]).first_or_initialize
45
- submission.assign_attributes(attrs)
46
- submission.save!
47
- submission
48
- end
36
+ private
49
37
 
38
+ def create_or_update(attrs)
39
+ submission = Submission.where(canvas_submission_id: attrs[:canvas_submission_id]).first_or_initialize
40
+ submission.assign_attributes(attrs)
41
+ submission.save!
42
+ submission
43
+ end
50
44
 
51
- def assignment
52
- @assignment ||= Assignment.find_by(canvas_assignment_id: local_canvas_id(payload[:assignment_id]))
53
- end
45
+ def assignment
46
+ @assignment ||= Assignment.find_by(canvas_assignment_id: local_canvas_id(payload[:assignment_id]))
47
+ end
54
48
 
55
- def user
56
- @user ||= User.find_by(canvas_user_id: local_canvas_id(payload[:user_id]))
49
+ def user
50
+ @user ||= User.find_by(canvas_user_id: local_canvas_id(payload[:user_id]))
51
+ end
57
52
  end
@@ -1,4 +1,6 @@
1
- <%= autogenerated_event_warning %>
1
+ # <%= autogenerated_event_warning %>
2
2
 
3
- class LiveEvents::SubmissionUpdatedEvent < LiveEvents::SubmissionEvent
4
- end
3
+ module LiveEvents
4
+ class SubmissionUpdatedEvent < LiveEvents::SubmissionEvent
5
+ end
6
+ end
@@ -1,35 +1,34 @@
1
- <%= autogenerated_event_warning %>
1
+ # <%= autogenerated_event_warning %>
2
2
 
3
- class LiveEvents::SyllabusEvent < LiveEvents::BaseEvent
4
- # The following is provided in the live events call:
5
- # {
6
- # course_id: course.global_id,
7
- # syllabus_body: LiveEvents.truncate(course.syllabus_body),
8
- # old_syllabus_body: LiveEvents.truncate(old_syllabus_body)
9
- # }
10
- #
3
+ module LiveEvents
4
+ class SyllabusEvent < LiveEvents::BaseEvent
5
+ # The following is provided in the live events call:
6
+ # {
7
+ # course_id: course.global_id,
8
+ # syllabus_body: LiveEvents.truncate(course.syllabus_body),
9
+ # old_syllabus_body: LiveEvents.truncate(old_syllabus_body)
10
+ # }
11
+ #
11
12
 
12
- def perform(event_payload)
13
- super
14
- attrs = {
15
- course_id: course.try(:id),
16
- syllabus_body: payload[:syllabus_body]
17
- }
18
- create_or_update(attrs)
19
- rescue => e
20
- log_error e
21
- ensure
22
- true
23
- end
13
+ def perform(event_payload)
14
+ super
15
+ attrs = {
16
+ course_id: course.try(:id),
17
+ syllabus_body: payload[:syllabus_body],
18
+ }
19
+ create_or_update(attrs)
20
+ end
24
21
 
25
- private
26
- def create_or_update(attrs)
27
- syllabus = Syllabus.where(course_id: attrs[:course_id]).first_or_initialize
28
- syllabus.body = attrs[:syllabus_body] # Syllabus Body can get truncated by live events
29
- syllabus.save!
30
- end
22
+ private
23
+
24
+ def create_or_update(attrs)
25
+ syllabus = Syllabus.where(course_id: attrs[:course_id]).first_or_initialize
26
+ syllabus.body = attrs[:syllabus_body] # Syllabus Body can get truncated by live events
27
+ syllabus.save!
28
+ end
31
29
 
32
- def course
33
- Course.find_by(canvas_course_id: local_canvas_id(payload[:course_id]))
30
+ def course
31
+ Course.find_by(canvas_course_id: local_canvas_id(payload[:course_id]))
32
+ end
34
33
  end
35
- end
34
+ end
@@ -1,4 +1,6 @@
1
- <%= autogenerated_event_warning %>
1
+ # <%= autogenerated_event_warning %>
2
2
 
3
- class LiveEvents::SyllabusUpdatedEvent < LiveEvents::SyllabusEvent
3
+ module LiveEvents
4
+ class SyllabusUpdatedEvent < LiveEvents::SyllabusEvent
5
+ end
4
6
  end
@@ -1,4 +1,6 @@
1
- <%= autogenerated_event_warning %>
1
+ # <%= autogenerated_event_warning %>
2
2
 
3
- class LiveEvents::UserChangedEvent < LiveEvents::UserEvent
4
- end
3
+ module LiveEvents
4
+ class UserChangedEvent < LiveEvents::UserEvent
5
+ end
6
+ end
@@ -1,6 +1,7 @@
1
- <%= autogenerated_event_warning %>
1
+ # <%= autogenerated_event_warning %>
2
2
 
3
- class LiveEvents::UserEvent < LiveEvents::BaseEvent
3
+ module LiveEvents
4
+ class UserEvent < LiveEvents::BaseEvent
4
5
  # The following is provided in the live events call:
5
6
  # {
6
7
  # user_id: user.global_id,
@@ -16,17 +17,13 @@ class LiveEvents::UserEvent < LiveEvents::BaseEvent
16
17
  super
17
18
  attrs = {
18
19
  canvas_user_id: local_canvas_id(payload[:user_id]),
19
- name: payload[:name],
20
- workflow_state: payload[:workflow_state]
20
+ status: payload[:workflow_state],
21
21
  }
22
22
  create_or_update(attrs)
23
- rescue => e
24
- log_error e
25
- ensure
26
- true
27
23
  end
28
24
 
29
25
  private
26
+
30
27
  def create_or_update(attrs)
31
28
  user = User.where(canvas_user_id: attrs[:canvas_user_id]).first_or_initialize
32
29
  user.assign_attributes(attrs)
@@ -1,4 +1,6 @@
1
- <%= autogenerated_event_warning %>
1
+ # <%= autogenerated_event_warning %>
2
2
 
3
- class LiveEvents::UserUpdatedEvent < LiveEvents::UserEvent
4
- end
3
+ module LiveEvents
4
+ class UserUpdatedEvent < LiveEvents::UserEvent
5
+ end
6
+ end
@@ -1,3 +1,3 @@
1
1
  module CanvasSync
2
- VERSION = "0.3.23".freeze
2
+ VERSION = "0.4.0".freeze
3
3
  end
@@ -0,0 +1,42 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe LiveEvents::ModuleEvent do
4
+ let(:payload) {
5
+ {
6
+ "body" => {
7
+ module_id: "1",
8
+ name: "Module 2",
9
+ position: 1337,
10
+ workflow_state: "active",
11
+ context_id: 1,
12
+ context_type: "Course",
13
+ },
14
+ }
15
+ }
16
+
17
+ describe "#perform" do
18
+ context "the module already exists" do
19
+ let!(:context_module) { FactoryGirl.create(:context_module, canvas_context_module_id: payload["body"][:module_id]) }
20
+
21
+ it "updates it" do
22
+ expect {
23
+ LiveEvents::ModuleEvent.new.perform(payload)
24
+ }.to_not change { ContextModule.count }
25
+ expect(context_module.reload.name).to eq(payload["body"][:name])
26
+ expect(context_module.position).to eq(payload["body"][:position])
27
+ expect(context_module.workflow_state).to eq(payload["body"][:workflow_state])
28
+ end
29
+ end
30
+
31
+ context "the module does not already exist" do
32
+ it "creates it" do
33
+ expect {
34
+ LiveEvents::ModuleEvent.new.perform(payload)
35
+ }.to change { ContextModule.count }.by(1)
36
+ expect(ContextModule.last.name).to eq(payload["body"][:name])
37
+ expect(ContextModule.last.position).to eq(payload["body"][:position])
38
+ expect(ContextModule.last.workflow_state).to eq(payload["body"][:workflow_state])
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,12 @@
1
+ # #
2
+ # AUTO GENERATED LIVE EVENT
3
+ # This was auto generated by the CanvasSync Gem.
4
+ # You can customize it as needed, but make sure you test
5
+ # any changes you make to the auto generated methods.
6
+ #
7
+
8
+
9
+ module LiveEvents
10
+ class AssignmentCreatedEvent < LiveEvents::AssignmentEvent
11
+ end
12
+ end
@@ -0,0 +1,60 @@
1
+ # #
2
+ # AUTO GENERATED LIVE EVENT
3
+ # This was auto generated by the CanvasSync Gem.
4
+ # You can customize it as needed, but make sure you test
5
+ # any changes you make to the auto generated methods.
6
+ #
7
+
8
+
9
+ module LiveEvents
10
+ class AssignmentEvent < LiveEvents::BaseEvent
11
+ # The following is provided in the live events call:
12
+ # {
13
+ # assignment_id: assignment.global_id,
14
+ # context_id: assignment.global_context_id,
15
+ # context_type: assignment.context_type,
16
+ # workflow_state: assignment.workflow_state,
17
+ # title: LiveEvents.truncate(assignment.title),
18
+ # description: LiveEvents.truncate(assignment.description),
19
+ # due_at: assignment.due_at,
20
+ # unlock_at: assignment.unlock_at,
21
+ # lock_at: assignment.lock_at,
22
+ # updated_at: assignment.updated_at,
23
+ # points_possible: assignment.points_possible
24
+ # }
25
+
26
+ def perform(event_payload)
27
+ super
28
+ attrs = {
29
+ canvas_assignment_id: local_canvas_id(payload[:assignment_id]),
30
+ title: payload[:title],
31
+ workflow_state: payload[:workflow_state],
32
+ description: payload[:description],
33
+ due_at: payload[:due_at],
34
+ unlock_at: payload[:unlock_at],
35
+ lock_at: payload[:lock_at],
36
+ points_possible: payload[:points_possible],
37
+ }
38
+ create_or_update(attrs)
39
+ end
40
+
41
+ private
42
+
43
+ def create_or_update(attrs)
44
+ assignment = Assignment.where(canvas_assignment_id: attrs[:canvas_assignment_id]).first_or_initialize
45
+ assignment.assign_attributes(attrs)
46
+ assignment.save!
47
+ assignment
48
+ end
49
+
50
+ # This could be nil if the context type for this assignment is not 'Course' of if a local course was not found
51
+ def canvas_course_id
52
+ payload[:context_type] == "Course" ? local_canvas_id(payload[:context_id]) : nil
53
+ end
54
+
55
+ # This could be nil if the context type for this assignment is not 'Course' of if a local course was not found
56
+ def course
57
+ @course ||= Course.find_by(canvas_course_id: canvas_course_id)
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,12 @@
1
+ # #
2
+ # AUTO GENERATED LIVE EVENT
3
+ # This was auto generated by the CanvasSync Gem.
4
+ # You can customize it as needed, but make sure you test
5
+ # any changes you make to the auto generated methods.
6
+ #
7
+
8
+
9
+ module LiveEvents
10
+ class AssignmentUpdatedEvent < LiveEvents::AssignmentEvent
11
+ end
12
+ end
@@ -0,0 +1,22 @@
1
+ # #
2
+ # AUTO GENERATED LIVE EVENT
3
+ # This was auto generated by the CanvasSync Gem.
4
+ # You can customize it as needed, but make sure you test
5
+ # any changes you make to the auto generated methods.
6
+ #
7
+
8
+
9
+ module LiveEvents
10
+ class BaseEvent < CanvasSync::Job
11
+ attr_accessor :payload
12
+
13
+ def perform(event_payload)
14
+ @payload = HashWithIndifferentAccess.new(event_payload["body"])
15
+ end
16
+
17
+ # Live events will use a canvas global ID (cross shard) for any ID's provided. This method will return the local ID.
18
+ def local_canvas_id(id)
19
+ id.to_i % 10_000_000_000_000
20
+ end
21
+ end
22
+ end