Class: Consyncful::Sync

Inherits:
Object
  • Object
show all
Includes:
Hooks, Mongoid::Document
Defined in:
lib/consyncful/sync.rb

Overview

A mongoid model that stores the state of a syncronisation feed. Stores the next URL provided by Contentfuls Sync API.

Sync’s are affectivly singletons, there should only ever be one in the database

Is also the entrypoint of a Syncronization run

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.freshObject

Delete the previous sync chains from database and create a fresh one. Used to completely resync all items from Contentful.



37
38
39
40
# File 'lib/consyncful/sync.rb', line 37

def self.fresh
  destroy_all
  latest
end

.latestObject



30
31
32
# File 'lib/consyncful/sync.rb', line 30

def self.latest
  last || new
end

Instance Method Details

#drop_staleObject

Makes sure that the database contains only records that have been provided during this chain of syncronisation.



45
46
47
48
49
# File 'lib/consyncful/sync.rb', line 45

def drop_stale
  stale = Base.where(:sync_id.ne => id, :sync_id.exists => true)
  puts Rainbow("Dropping #{stale.count} records that haven't been touched in this sync").red
  stale.destroy
end

#runObject

Entry point to a syncronization run. Is responsible for updating Sync state



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/consyncful/sync.rb', line 53

def run
  run_hook :before_run

  stats = Consyncful::Stats.new
  load_all_models

  sync = start_sync

  changed_ids = sync_items(sync, stats)

  drop_stale

  update_run(sync.next_sync_url)
  stats.print_stats

  run_hook :after_run, changed_ids
end