Module: CanvasSync::Concerns::ApiSyncable
- Extended by:
- ActiveSupport::Concern
- Included in:
- Account, Admin, Assignment, AssignmentGroup, ContextModule, ContextModuleItem, Course, Enrollment, Group, GroupMembership, Role, Section, Submission, Term, User
- Defined in:
- lib/canvas_sync/concerns/api_syncable.rb
Constant Summary collapse
- NON_EXISTANT_ERRORS =
[Faraday::Error::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
189 190 191 |
# File 'lib/canvas_sync/concerns/api_syncable.rb', line 189 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
137 138 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 |
# File 'lib/canvas_sync/concerns/api_syncable.rb', line 137 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 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
122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/canvas_sync/concerns/api_syncable.rb', line 122 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.
109 110 111 112 113 114 115 116 117 |
# File 'lib/canvas_sync/concerns/api_syncable.rb', line 109 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?
176 177 178 179 |
# File 'lib/canvas_sync/concerns/api_syncable.rb', line 176 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?
184 185 186 187 |
# File 'lib/canvas_sync/concerns/api_syncable.rb', line 184 def update_from_api_params!(*args) assign_from_api_params(*args) save! if changed? end |