This repository has been archived by the owner on Jul 3, 2020. It is now read-only.
forked from banker/mongulator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mongulator.rb
65 lines (56 loc) · 1.47 KB
/
mongulator.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
require 'rubygems'
require 'mongo'
require 'sinatra'
require 'json'
configure do
CONN = Mongo::Connection.new
DB = 'mongulator'
EMLDB = 'mongulator_emails'
end
enable :sessions
def user_scope
session['user_scope'] ||= BSON::ObjectId.new.to_s
end
def scoped_collection(name)
CONN[DB][user_scope + '.' + name]
end
get '/' do
send_file 'public/index.html'
end
post '/insert' do
coll = scoped_collection(params['name'])
doc = JSON.parse(params['doc'])
if coll.count < 200
coll.insert(doc)
end
if params['name'] == 'email' and doc.has_key? 'email'
CONN[EMLDB]['collected_emails'].insert({
"email" => doc['email'],
"first" => doc['first_name'],
"last" => doc['last_name'],
"processed"=>false
})
end
end
post '/update' do
coll = scoped_collection(params['name'])
query = JSON.parse(params['query'])
doc = JSON.parse(params['doc'])
upsert = (params['upsert'] == 'true')
multi = (params['multi'] == 'true')
coll.update(query, doc, :upsert => upsert, :multi => multi)
end
post '/remove' do
coll = scoped_collection(params['name'])
coll.remove(JSON.parse(params['doc']))
end
post '/find' do
coll = scoped_collection(params['name'])
query = JSON.parse(params['query'])
fields = JSON.parse(params['fields'])
fields = nil if fields == {}
limit = params['limit'].to_i
skip = params['skip'].to_i
cursor = coll.find(query, :fields => fields, :limit => limit, :skip => skip)
cursor.to_a.to_json
end