This repository has been archived by the owner on Nov 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.ru
139 lines (120 loc) · 3.36 KB
/
config.ru
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
require 'rubygems'
require 'bundler/setup'
require 'json'
require 'active_support/core_ext/hash/slice'
require 'mongo'
require 'sinatra'
require 'sinatra/cross_origin'
COLLECTION_MAP = {
'ocd-membership' => :memberships,
'ocd-organization' => :organizations,
'ocd-person' => :people,
'ocd-post' => :posts,
}
Mongo::Logger.logger.level = Logger::WARN
configure do
enable :cross_origin
end
helpers do
def connection
uri = URI.parse(ENV['MONGOLAB_URI'] || 'mongodb://localhost:27017/pupa')
connection = Mongo::Client.new(["#{uri.host}:#{uri.port}"], database: uri.path[1..-1])
connection = connection.with(user: uri.user, password: uri.password) if uri.user && uri.password
connection
end
def collection(collection_name, criteria)
content_type :json
if params.any?
raise "Unknown parameters: #{params.keys}"
end
if criteria.keys == [:_id] && String === criteria.values[0]
JSON.dump(connection[collection_name].find(criteria).first)
else
JSON.dump(connection[collection_name].find(criteria).to_a)
end
end
def members_of(organization_id)
{'$in' => connection[:memberships].find(organization_id: organization_id).distinct(:person_id)}
end
def network_of(organization_id)
{'$in' => connection[:memberships].find(person_id: members_of(organization_id)).distinct(:organization_id)}
end
end
# Get the memberships of the people who are members of the organization.
get '/memberships' do
in_network_of = params.delete('in_network_of')
id = params.delete('id')
if in_network_of
criteria = {person_id: members_of(in_network_of)}
elsif id
criteria = {_id: id}
else
criteria = params.slice('organization_id', 'person_id')
params.delete('organization_id')
params.delete('person_id')
end
collection(:memberships, criteria)
end
# Get the organizations which the members of the organization are members of.
get '/organizations' do
in_network_of = params.delete('in_network_of')
id = params.delete('id')
if in_network_of
criteria = {_id: network_of(in_network_of)}
elsif id
criteria = {_id: id}
else
criteria = params.slice('classification', 'parent_id')
params.delete('classification')
params.delete('parent_id')
end
collection(:organizations, criteria)
end
# Get the people who are members of the organization.
get '/people' do
in_network_of = params.delete('in_network_of')
organization_id = params.delete('member_of')
id = params.delete('id')
if in_network_of
criteria = {_id: members_of(network_of(in_network_of))}
elsif organization_id
criteria = {_id: members_of(organization_id)}
elsif id
criteria = {_id: id}
else
criteria = {}
end
collection(:people, criteria)
end
get '/posts' do
organization_id = params.delete('organization_id')
if organization_id
criteria = {organization_id: organization_id}
else
criteria = {}
end
collection(:posts, criteria)
end
get '/twitter_users' do
content_type :json
data = {}
connection[:twitter_users].find.each do |user|
data[user['name']] = user['screen_name']
end
JSON.dump(data)
end
get '/robots.txt' do
"User-agent: *\nDisallow: /"
end
get '/favicon.ico' do
204
end
get '/' do
204
end
get '/*' do
params.delete('captures') # ignore
id = params.delete('splat')[0]
collection(COLLECTION_MAP.fetch(id.split('/', 2)[0]), {_id: id})
end
run Sinatra::Application