This repository has been archived by the owner on Feb 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathitems_controller.rb
executable file
·57 lines (46 loc) · 1.98 KB
/
items_controller.rb
1
2
3
4
5
6
7
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class ItemsController < ApplicationController
include Timestamps # Filed under /lib/timestamps.rb
respond_to :json # Respond with JSON objects
def sync
# If this is a HTTP GET request, a client want's to check for updates
# If this is a HTTP POST, a client want's to save pending changes
request.post? ? sync_post : sync_get
end
private
def sync_get
# Process the request
since = from_unix_ms_timestamp params[:since]
# Create a response
# Should be like { "now":1279888110421, "updates": [ {"id": "F89F99F7B887423FB4B9C961C3883C0A",
# "name": "Something", "_lastChange": 1279888110370 } ] }
items = Item.updated_after(since).ordered
items.each { |item| puts "-- Element sent: #{item.uuid}" } # Print items which must be sent
items = items.collect{ |item| item.attributes }
# Respond
now = to_unix_ms_timestamp Time.now
resp = { "now" => now, "updates" => items }.to_json
respond_with JSON.parse(resp.to_s)
end
def sync_post
something_changed = false
# Request body should be like [{"id":"BDDF85807155497490C12D6DA3A833F1", "name":"Something"}]
params["_json"].each do |item_hash|
item = Item.find_by_uuid item_hash[:id] # Checks if already exists
item = Item.new unless item # If it's a new item, create
item.name = item_hash[:name]
item.category = item_hash[:category]
item.deleted = item_hash[:deleted]
item.uuid = item_hash[:id]
something_changed ||= item.changed?
saved = false
saved = item.save if item.changed?
puts "-- Element synced: #{item.uuid} [changed? #{item.changed?} | saved? #{saved}]"
end
# Response should be like {"status": "ok", "now": 1279888110797}
resp = { "status" => "ok", "now" => to_unix_ms_timestamp(Time.now) }
respond_to do |format|
format.json { render :json => resp.to_json }
end
broadcast_ping if something_changed # Send ping to all clients, notifying them to sync
end
end