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(api_params, **kwargs) ⇒ 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
162 163 164 |
# File 'lib/canvas_sync/concerns/api_syncable.rb', line 162 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
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/canvas_sync/concerns/api_syncable.rb', line 110 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
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/canvas_sync/concerns/api_syncable.rb', line 95 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.
82 83 84 85 86 87 88 89 90 |
# File 'lib/canvas_sync/concerns/api_syncable.rb', line 82 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(api_params, **kwargs) ⇒ self
Apply a response Hash from the API to this model’s attributes and save if changed?
149 150 151 152 |
# File 'lib/canvas_sync/concerns/api_syncable.rb', line 149 def update_from_api_params(api_params, **kwargs) 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?
157 158 159 160 |
# File 'lib/canvas_sync/concerns/api_syncable.rb', line 157 def update_from_api_params!(*args) assign_from_api_params(*args) save! if changed? end |