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

Instance Method Details

#api_sync_optionsObject



189
190
191
# File 'lib/canvas_sync/concerns/api_syncable.rb', line 189

def api_sync_options
  self.class.api_sync_options
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

Parameters:

  • api_params (Hash)

    API-format Hash

Returns:

  • (self)

    self



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)
  options = self.api_sync_options
  api_params = api_params.with_indifferent_access

  map = options[: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 = options[: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

Parameters:

  • retries (Number) (defaults to: 3)

    Number of retries

Returns:

  • (Hash)

    Response Hash from API



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 = api_sync_options[: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.

Parameters:

  • retries (Number) (defaults to: 3)

    Number of retries

Returns:

  • (Hash)

    Response Hash from API



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?

Parameters:

  • api_params (Hash)

    API-format Hash

Returns:

  • (self)

    self



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?

Parameters:

  • api_params (Hash)

    API-format Hash

Returns:

  • (self)

    self



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