-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathngs_app.rb
108 lines (88 loc) · 2.86 KB
/
ngs_app.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
require "sinatra/reloader"
class App < Sinatra::Base
configure :development do |config|
register Sinatra::Reloader
config.also_reload "lib/ngs/**/*"
end
use Rack::Session::Cookie , :secret => (ENV['SESSION_SECRET'] || "82e042cd6fde2bf1764f777236db799e")
fields = ["id", "email-address", "first-name", "last-name", "headline", "industry", "picture-url", "public-profile-url", "location", "skills"]
use OmniAuth::Builder do
provider :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_SECRET'], :scope => 'user_likes, user_location, friends_likes, friends_location', :client_options => {:ssl => {:ca_file => "./cacert.pem"}}
end
Dir.glob(File.dirname(__FILE__) + '/helpers/*', &method(:require))
helpers App::Helpers
# Homepage
get '/' do
if current_user.nil?
haml :index, :layout => :layout
else
redirect to("/user/#{current_user.uid}")
end
end
# Authentication
['get', 'post'].each do |method|
send(method, "/auth/:provider/callback") do
user = User.create_with_omniauth(env['omniauth.auth'])
session[:uid] = user.uid
redirect to(session[:redirect_url] || "/user/#{session[:uid]}")
session[:redirect_url] = nil
end
end
get '/auth/failure/?' do
raise 'auth error'
end
get '/logout/?' do
session.clear
redirect to('/')
end
# Users
get '/user/:id' do
@user = user(params[:id])
@friends_count = @user.friends_count
@likes_count = @user.likes_count
haml :'user/show'
end
get '/user/:id/friends' do
@user = user(params[:id])
@friends = @user.friends
haml :'user/index'
end
get '/user/:id/likes' do
@user = user(params[:id])
@things = @user.likes
haml :'thing/index'
end
get '/user/:id/search' do
@user = user(params[:id])
@q = params[:q] || "friends"
@q = "friends" if @q == "people"
begin
@cypher = NGS::Parser.parse("(#{@q})")
@cypher[0] = @cypher[0] + " , people.uid, people.name, people.image_url LIMIT 100"
@cypher[1].merge!({"me" => @user.neo_id}) if @cypher[1].has_key?("me")
@people = $neo_server.execute_query(@cypher[0].to_s, @cypher[1])["data"]
@cypher[0].gsub!("MATCH", "<br>MATCH")
@cypher[0].gsub!("RETURN", "<br>RETURN")
@cypher[0].gsub!("LIMIT", "<br>LIMIT")
rescue Exception => e
@cypher = ["Sorry, I didn't understand, please try a different search."]
@people = []
end
haml :'user/search'
end
get '/user/:id/visualization' do
@user = user(params[:id])
haml :'user/visualization'
end
get '/visualization' do
@user = current_user
random_number = 20 + Random.rand(31)
@user.friend_matrix.sample(random_number).map{|fm| {"name" => fm[0], "follows" => fm[1]} }.to_json
end
# Things
get '/thing/:id' do
@thing = Thing.get_by_id(params[:id])
@users = @thing.users
haml :'thing/show'
end
end