class Synced::Strategies::UpdatedSince
This strategy performs partial synchronization. It fetches only changes (additions, modifications and deletions) from the API.
Public Class Methods
new(model_class, options = {})
click to toggle source
@option options [Time|Proc] initial_sync_since
: A point in time from which
objects will be synchronized on first synchronization.
Calls superclass method
Synced::Strategies::Full::new
# File lib/synced/strategies/updated_since.rb, line 11 def initialize(model_class, options = {}) super @initial_sync_since = options[:initial_sync_since] @tolerance = options[:tolerance] timestampt_strategy_class = options[:timestamp_strategy] || Synced::Strategies::SyncedAllAtTimestampStrategy @timestamp_strategy = timestampt_strategy_class.new(relation_scope: relation_scope, scope: @scope, model_class: model_class) end
Public Instance Methods
perform()
click to toggle source
Calls superclass method
Synced::Strategies::Full#perform
# File lib/synced/strategies/updated_since.rb, line 19 def perform super.tap do |local_objects| instrument("update_synced_timestamp_perform.synced", model: @model_class) do @timestamp_strategy.update(first_request_timestamp) end end end
reset_synced()
click to toggle source
# File lib/synced/strategies/updated_since.rb, line 27 def reset_synced @timestamp_strategy.reset end
Private Instance Methods
additional_errors_check()
click to toggle source
# File lib/synced/strategies/updated_since.rb, line 82 def additional_errors_check raise MissingTimestampError.new unless first_request_timestamp end
api_request_options()
click to toggle source
Calls superclass method
Synced::Strategies::Full#api_request_options
# File lib/synced/strategies/updated_since.rb, line 33 def api_request_options super.merge(updated_since: updated_since) end
deleted_remote_objects_ids()
click to toggle source
# File lib/synced/strategies/updated_since.rb, line 57 def deleted_remote_objects_ids meta && meta[:deleted_ids] or raise CannotDeleteDueToNoDeletedIdsError.new(@model_class) end
first_request_timestamp()
click to toggle source
# File lib/synced/strategies/updated_since.rb, line 61 def first_request_timestamp if first_response_headers && first_response_headers["x-updated-since-request-synced-at"] Time.zone.parse(first_response_headers["x-updated-since-request-synced-at"]) end end
first_response_headers()
click to toggle source
# File lib/synced/strategies/updated_since.rb, line 72 def first_response_headers @first_response_headers ||= (api.pagination_first_response && api.pagination_first_response.headers) || {} end
initial_sync_since()
click to toggle source
# File lib/synced/strategies/updated_since.rb, line 37 def initial_sync_since if @initial_sync_since.respond_to?(:call) @initial_sync_since.arity == 0 ? @initial_sync_since.call : @initial_sync_since.call(@scope) else @initial_sync_since end end
last_synced_at_with_offset()
click to toggle source
# File lib/synced/strategies/updated_since.rb, line 52 def last_synced_at_with_offset return if @timestamp_strategy.last_synced_at.blank? @timestamp_strategy.last_synced_at - @tolerance end
meta()
click to toggle source
# File lib/synced/strategies/updated_since.rb, line 67 def meta @meta ||= (api.last_response && api.last_response.meta) || {} end
remove_relation()
click to toggle source
Remove all objects with ids from deleted_ids field in the meta key
# File lib/synced/strategies/updated_since.rb, line 78 def remove_relation relation_scope.where(@id_key => deleted_remote_objects_ids) end
updated_since()
click to toggle source
# File lib/synced/strategies/updated_since.rb, line 46 def updated_since instrument("updated_since.synced") do [last_synced_at_with_offset, initial_sync_since].compact.max end end