canvas_sync 0.8.3 → 0.8.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 412a668db952bd2cf0956aa69353615ab3fe528446183040d3f66a8a77f3160a
4
- data.tar.gz: 537d6166a490ecc754e9e8dca2b7d54fe627ed0b5376479e9431c4297e47fd92
3
+ metadata.gz: fd62cb62c721c5e1262ddbac6bfbecfb2712d8830cd6d738350876e6459895ab
4
+ data.tar.gz: 7ac53b25cb60a449c5a678d12c5e902b48c9d96c10199473f630fca728141d99
5
5
  SHA512:
6
- metadata.gz: 0e0bfd9c81593f35a2f19c430ccf4c19b7642babc29afbadb8e67d3f6285eee9fecb730cbb73d10f48819f02c000c38fedd8bfc61e5ae33672570868bb1b5a24
7
- data.tar.gz: f50dfde884b32425a3f679619800c279811060a79765e8f70a0338e64a88d50c8f3a12de6663b26432d1ff4ca036ee45e30c4f7835cd689b48f1cc2fad412c59
6
+ metadata.gz: ac382d711a3ce11c8abc6cf1e7747548c0d8cd851f0bcddedaafcf2225861e2747efae3c97b6037d5d81fe7bba969b2d8ee13da1185dc037b3670c004f63027e
7
+ data.tar.gz: 4b9483d5bdb4c5b3d10b1dc1216fad6a6480b646d655463b4148318a58aa2ae5c8251a1e44e96af1e7970e239e52220b1c9c945cef3e7de097aa220735bda5c3
data/README.md CHANGED
@@ -213,6 +213,21 @@ class CanvasSyncModel < ApplicationRecord
213
213
  ) do |api_response, mapped_fields| # Must accept 1-2 parameters
214
214
  # Override behavior for actually applying the response to the model instance
215
215
  end
216
+
217
+ def something()
218
+ # ApiSyncable models add several instance methods:
219
+
220
+ request_from_api( # Starts an API request and and returns the params
221
+ retries: 3, # Number of times to retry the API call before failing
222
+ )
223
+
224
+ update_from_api_params(params) # Merge the API response into the model instance
225
+ update_from_api_params!(params) # Merge and save! if changed
226
+
227
+ sync_from_api( # Starts an API request and calls save! (if changed)
228
+ retries: 3, # Number of times to retry the API call before failing
229
+ )
230
+ end
216
231
  end
217
232
  ```
218
233
 
@@ -12,7 +12,11 @@ module CanvasSync::ApiSyncable
12
12
  end
13
13
 
14
14
  def sync_from_api(retries: 3)
15
- params = api_call_with_retry(retries || 3) {
15
+ update_from_api_params!(request_from_api(retries: retries))
16
+ end
17
+
18
+ def request_from_api(retries: 3)
19
+ api_call_with_retry(retries || 3) {
16
20
  blk = self.class.api_sync_fetch
17
21
  case blk.arity
18
22
  when 1
@@ -21,10 +25,9 @@ module CanvasSync::ApiSyncable
21
25
  self.instance_exec(&blk)
22
26
  end
23
27
  }
24
- update_from_api_params!(params)
25
28
  end
26
29
 
27
- def update_from_api_params!(api_params)
30
+ def update_from_api_params(api_params)
28
31
  api_params = api_params.with_indifferent_access
29
32
  apply_block = self.class.api_sync_block
30
33
  map = self.class.api_sync_map
@@ -32,7 +35,15 @@ module CanvasSync::ApiSyncable
32
35
  mapped_params = {}
33
36
  if map.present?
34
37
  map.each do |local_name, remote_name|
35
- mapped_params[local_name] = remote_name.respond_to?(:call) ? self.instance_exec(api_params, &remote_name) : api_params[remote_name]
38
+ if remote_name.respond_to?(:call)
39
+ mapped_params[local_name] = self.instance_exec(api_params, &remote_name)
40
+ elsif api_params.include?(remote_name)
41
+ mapped_params[local_name] = api_params[remote_name]
42
+ if remote_name == :id
43
+ current_value = send("#{local_name}")
44
+ raise "Mismatched Canvas ID" if current_value.present? && current_value != api_params[remote_name]
45
+ end
46
+ end
36
47
  end
37
48
  end
38
49
 
@@ -48,8 +59,13 @@ module CanvasSync::ApiSyncable
48
59
  send("#{local_name}=", val)
49
60
  end
50
61
  end
62
+ self
63
+ end
51
64
 
65
+ def update_from_api_params!(api_params)
66
+ update_from_api_params(api_params)
52
67
  save! if changed?
68
+ self
53
69
  end
54
70
 
55
71
  private
@@ -11,19 +11,19 @@ class Assignment < ApplicationRecord
11
11
  has_many :context_modules, through: :context_module_items
12
12
 
13
13
  api_syncable({
14
- title: :tile,
14
+ title: :name,
15
15
  description: :description,
16
16
  due_at: :due_at,
17
17
  unlock_at: :unlock_at,
18
- points_posible: :points_posible,
18
+ points_possible: :points_possible,
19
19
  min_score: :min_score,
20
20
  max_score: :max_score,
21
21
  mastery_score: :mastery_score,
22
22
  grading_type: :grading_type,
23
- submission_types: lambda { |_a, p| p["submission_types"].join(",") },
23
+ submission_types: lambda { |p| p["submission_types"].join(",") },
24
24
  workflow_state: :workflow_state,
25
25
  context_id: :course_id,
26
- context_type: lambda { |_a, _p| "Course" },
26
+ context_type: lambda { |_p| "Course" },
27
27
  canvas_assignment_group_id: :canvas_assignment_group_id,
28
28
  grading_scheme_id: :grading_scheme_id,
29
29
  grading_standard_id: :grading_standard_id,
@@ -1,7 +1,19 @@
1
1
  <%= autogenerated_model_warning %>
2
2
 
3
3
  class AssignmentGroup < ApplicationRecord
4
+ include CanvasSync::ApiSyncable
5
+
4
6
  validates :canvas_assignment_group_id, uniqueness: true, presence: true
5
7
  belongs_to :course, primary_key: :canvas_course_id, foreign_key: :canvas_course_id, optional: true
6
8
  has_many :assignments, primary_key: :canvas_assignment_group_id, foreign_key: :canvas_assignment_group_id
9
+
10
+ serialize :rules
11
+
12
+ api_syncable({
13
+ canvas_assignment_group_id: :id,
14
+ name: :name,
15
+ position: :position,
16
+ rules: :rules,
17
+ group_weight: :group_weight,
18
+ }, -> (api) { api.assignment(course_id, canvas_assignment_group_id) })
7
19
  end
@@ -1,3 +1,3 @@
1
1
  module CanvasSync
2
- VERSION = "0.8.3".freeze
2
+ VERSION = "0.8.4".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: canvas_sync
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.3
4
+ version: 0.8.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nate Collings
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-01 00:00:00.000000000 Z
11
+ date: 2019-03-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler