Module: CanvasSync::Concerns::ApiSyncable
- Extended by:
- ActiveSupport::Concern
- Included in:
- Account, Admin, Assignment, AssignmentGroup, ContentMigration, ContextModule, ContextModuleItem, Course, CourseNickname, CourseProgress, Enrollment, Group, GroupMembership, LearningOutcome, LearningOutcomeResult, Role, Rubric, RubricAssessment, RubricAssociation, Section, Submission, Term, User, UserObserver
- Defined in:
- lib/canvas_sync/concerns/api_syncable.rb
Constant Summary collapse
- NON_EXISTANT_ERRORS =
[Faraday::ResourceNotFound, Footrest::HttpError::NotFound]
Instance Method Summary collapse
- #api_sync_options ⇒ Object
-
#assign_from_api_params(api_params, **kwargs) ⇒ self
Apply a response Hash from the API to this model’s attributes, but do not save.
-
#request_from_api(retries: 3, **kwargs) ⇒ Hash
Fetch this model from the API and return the response.
-
#sync_from_api(retries: 3, **kwargs) ⇒ Hash
Call the API and Syncs this model.
-
#update_from_api_params(*args) ⇒ self
Apply a response Hash from the API to this model’s attributes and save if changed?.
-
#update_from_api_params!(*args) ⇒ self
Apply a response Hash from the API to this model’s attributes, and save! if changed?.
Instance Method Details
#api_sync_options ⇒ Object
196 197 198 |
# File 'lib/canvas_sync/concerns/api_syncable.rb', line 196 def self.class. end |
#assign_from_api_params(api_params, **kwargs) ⇒ self
Apply a response Hash from the API to this model’s attributes, but do not save
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/canvas_sync/concerns/api_syncable.rb', line 139 def assign_from_api_params(api_params, **kwargs) = self. api_params = api_params.with_indifferent_access map = [:field_map] mapped_params = {} if map.present? map.each do |local_name, remote_name| if remote_name.respond_to?(:call) mapped_params[local_name] = self.instance_exec(api_params, &remote_name) elsif api_params.include?(remote_name) mapped_params[local_name] = api_params[remote_name] if remote_name == :id current_value = send("#{local_name}") raise "Mismatched Canvas ID" if current_value.present? && current_value != api_params[remote_name] end end end end apply_block = [:process_response] if self.class.column_names.include?("canvas_synced_at") self.canvas_synced_at = mapped_params[:canvas_synced_at] = DateTime.now end if apply_block.present? case apply_block.arity when 1 self.instance_exec(api_params, &apply_block) when 2 self.instance_exec(api_params, mapped_params, &apply_block) end else mapped_params.each do |local_name, val| send("#{local_name}=", val) end end self end |
#request_from_api(retries: 3, **kwargs) ⇒ Hash
Fetch this model from the API and return the response
124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/canvas_sync/concerns/api_syncable.rb', line 124 def request_from_api(retries: 3, **kwargs) api_call_with_retry(retries || 3) { blk = [:fetch_from_api] case blk.arity when 1 self.instance_exec(canvas_sync_client, &blk) else self.instance_exec(&blk) end } end |
#sync_from_api(retries: 3, **kwargs) ⇒ Hash
Call the API and Syncs this model. Calls the mark_deleted workflow if a 404 is received.
111 112 113 114 115 116 117 118 119 |
# File 'lib/canvas_sync/concerns/api_syncable.rb', line 111 def sync_from_api(retries: 3, **kwargs) api_response = request_from_api(retries: retries, **kwargs) update_from_api_params!(api_response, **kwargs) api_response rescue *NON_EXISTANT_ERRORS api_mark_deleted save! if changed? nil end |
#update_from_api_params(*args) ⇒ self
Apply a response Hash from the API to this model’s attributes and save if changed?
183 184 185 186 |
# File 'lib/canvas_sync/concerns/api_syncable.rb', line 183 def update_from_api_params(*args) assign_from_api_params(*args) save if changed? end |
#update_from_api_params!(*args) ⇒ self
Apply a response Hash from the API to this model’s attributes, and save! if changed?
191 192 193 194 |
# File 'lib/canvas_sync/concerns/api_syncable.rb', line 191 def update_from_api_params!(*args) assign_from_api_params(*args) save! if changed? end |