-
Notifications
You must be signed in to change notification settings - Fork 0
/
datamappering.rb
182 lines (135 loc) · 3.85 KB
/
datamappering.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
58
59
60
61
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
require 'rubygems'
require 'sinatra'
require 'data_mapper' # metagem, requires common plugins too.
require 'json'
require 'fileutils'
require 'securerandom'
# need install dm-sqlite-adapter
DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/yt.db")
class Account
include DataMapper::Resource
property :accountNumber, Serial
property :userToken, String
property :email, String
property :facebook, String
property :created, DateTime
property :lastModified, DateTime
has n, :devices
has n, :personalMessage
end
class Device
include DataMapper::Resource
property :id, Serial
property :appId, String
property :deviceToken, String
property :userAgent, Text
property :lastSeen, DateTime
property :lastSeenIP, String
property :created, DateTime
property :lastModified, DateTime
belongs_to :account, :required => false
end
class PersonalMessage
include DataMapper::Resource
property :id, Serial
property :filePath, String
property :fileName, String
belongs_to :account
end
# Perform basic sanity checks and initialize all relationships
# Call this when you've defined all your models
DataMapper.finalize
# automatically create the post table
Device.auto_upgrade!
Account.auto_upgrade!
PersonalMessage.auto_upgrade!
# ======================
# APPLICATION
#
# ======================
# Device CRUD
get '/device/:appId' do
device = Device.first(:appId => params[:appId])
if device == nil
status 404
body "Device with appId[#{params[:appId]}] not found. Try registering a device first."
else
status 200
content_type :json
{:deviceToken => '#{device.deviceToken}', :lastModified => "#{device.lastModified}"}.to_json
end
end
post '/device', :provides => :json do
request.body.rewind
data = JSON.parse request.body.read
#would be nice to do some validatio here
newToken = SecureRandom.hex(16)
device = Device.create(
:appId => "#{data['appId']}",
:deviceToken => "#{newToken}",
:userAgent => "#{request.user_agent}",
:lastSeen => Time.now,
:lastSeenIP => "#{request.ip}",
:created => Time.now,
:lastModified => Time.now
)
device.save
status 201
content_type :json
{:deviceToken => "#{newToken}", :lastModified => "#{device.lastModified}"}.to_json
end
# ======================
# Account CRUD
get '/account/:accountNumber' do
@account = Account.get(params[:accountNumber])
if account == nil
status 404
body "Account with accountNumber[#{params[:accountNumber]}] not found. Try creating an account first."
else
status 200
content_type :json
{:userToken => "#{@account.userToken}", :email => "#{@account.email}", :lastModified => "#{@account.lastModified}"}.to_json
end
erb :account
end
get '/admin/account/:accountNumber' do
@account = Account.get(params[:accountNumber])
erb :account
end
get '/admin/accounts' do
@accounts = Account.all
erb :accounts
end
post '/account', :provides => :json do
request.body.rewind
data = JSON.parse request.body.read
#would be nice to do some validation here
device = Device.first(:deviceToken => "#{data['deviceToken']}")
if device == nil
status 400
body "Attempting to create a user without first registering a device with a valid deviceToken. Try getting a device token first."
else
newToken = SecureRandom.hex(8)
account = Account.create(
:userToken => "#{newToken}",
:email => "#{data['email']}" ,
:facebook => "#{data['facebook']}",
:created => Time.now,
:lastModified => Time.now
)
account.save
#link account to this device
device.update(:account => account, :lastSeen => Time.now, :lastSeenIP => "#{request.ip}", :lastModified => Time.now)
device.save
#return the JSON response
status 201
content_type :json
{:userToken => "#{newToken}", :lastModified => "#{account.lastModified}"}.to_json
end
end
#upload PMS
post '/account/:accountNumber/images/:id' do
File.open(params[:id], 'w+') do |file|
file.write(request.body.read)
end
end