Module: CanvasSync::ApiSyncable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/canvas_sync/api_syncable.rb
Instance Method Summary collapse
- #api_sync_options ⇒ Object
-
#request_from_api(retries: 3) ⇒ Hash
Fetch this model from the API and return the response.
-
#sync_from_api(retries: 3) ⇒ Hash
Call the API and Syncs this model.
-
#update_from_api_params(api_params) ⇒ self
Apply a response Hash from the API to this model’s attributes, but do not save.
-
#update_from_api_params!(api_params) ⇒ self
Apply a response Hash from the API to this model’s attributes, and save if changed?.
Instance Method Details
#api_sync_options ⇒ Object
107 108 109 |
# File 'lib/canvas_sync/api_syncable.rb', line 107 def self.class. end |
#request_from_api(retries: 3) ⇒ Hash
Fetch this model from the API and return the response
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/canvas_sync/api_syncable.rb', line 47 def request_from_api(retries: 3) 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) ⇒ Hash
Call the API and Syncs this model. Calls the mark_deleted workflow if a 404 is received.
35 36 37 38 39 40 41 42 |
# File 'lib/canvas_sync/api_syncable.rb', line 35 def sync_from_api(retries: 3) api_response = request_from_api(retries: retries) update_from_api_params!(api_response) api_response rescue Faraday::Error::ResourceNotFound => e api_mark_deleted nil end |
#update_from_api_params(api_params) ⇒ self
Apply a response Hash from the API to this model’s attributes, but do not save
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/canvas_sync/api_syncable.rb', line 62 def update_from_api_params(api_params) = 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 |
#update_from_api_params!(api_params) ⇒ self
Apply a response Hash from the API to this model’s attributes, and save if changed?
101 102 103 104 105 |
# File 'lib/canvas_sync/api_syncable.rb', line 101 def update_from_api_params!(api_params) update_from_api_params(api_params) save! if changed? self end |