Class: Syncer

Inherits:
Object
  • Object
show all
Defined in:
lib/syncer/syncer.rb,
lib/syncer/version.rb

Constant Summary collapse

SYNC_VERSION_KEY =
'_sync_version'
SYNC_ORDER_KEY =
'_sync_order'
VERSION =
"0.0.4"

Class Method Summary collapse

Class Method Details

.sync(request) ⇒ Object

request keys should be strings and not symbols



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/syncer/syncer.rb', line 8

def self.sync(request)
  
  # TODO probably check the model actually exists and responds to sync
  # TODO We need to deal with security issues such as User associated models + same for relationships
      
  state_changed = false    
    
  response = {}
  all_sync_request = request.to_rjson
  sync_order = all_sync_request[SYNC_ORDER_KEY]
  # TODO if nil raise an exception
  sync_order.each do |model|
    model_sync_data = all_sync_request[model]
    next unless model_sync_data # it is ok not to have data for each model in the order
    sync_response = model.to_class.sync(model_sync_data)
    response[model] = sync_response
    state_changed = true if sync_response[:state_changed]
  end
  
  # handle version
  increment_sync_state_version if state_changed
  response[SYNC_VERSION_KEY] = sync_state.version
  
  response.to_rjson
end

.versionObject



34
35
36
# File 'lib/syncer/syncer.rb', line 34

def self.version    
  { SYNC_VERSION_KEY => sync_state.version }
end