forked from maxdemarzi/neovigator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneovigator.rb
123 lines (101 loc) · 3.32 KB
/
neovigator.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
require 'rubygems'
require 'neography'
require 'sinatra/base'
require 'uri'
class Neovigator < Sinatra::Application
set :haml, :format => :html5
set :app_file, __FILE__
configure :test do
require 'net-http-spy'
Net::HTTP.http_logger_options = {:verbose => true}
end
helpers do
def link_to(url, text=url, opts={})
attributes = ""
opts.each { |key,value| attributes << key.to_s << "=\"" << value << "\" "}
"<a href=\"#{url}\" #{attributes}>#{text}</a>"
end
def neo
@neo = Neography::Rest.new(ENV['NEO4J_URL'] || "http://localhost:7474")
end
end
def create_graph
graph_exists = neo.get_node_properties(1)
return if graph_exists && graph_exists['name']
johnathan = create_person('Johnathan')
mark = create_person('Mark')
phil = create_person('Phil')
mary = create_person('Mary')
luke = create_person('Luke')
make_mutual(johnathan, mark, "friends")
make_mutual(mark, mary, "friends")
make_mutual(mark, phil, "friends")
make_mutual(phil, mary, "married")
make_mutual(phil, luke, "enemies")
end
def make_mutual(node1, node2, rel_type)
neo.create_relationship(rel_type, node1, node2)
neo.create_relationship(rel_type, node2, node1)
end
def create_person(name)
neo.create_node("name" => name)
end
def neighbours
{"order" => "depth first",
"uniqueness" => "none",
"return filter" => {"language" => "builtin", "name" => "all_but_start_node"},
"depth" => 1}
end
def node_id(node)
case node
when Hash
node["self"].split('/').last
when String
node.split('/').last
else
node
end
end
def get_properties(node)
properties = "<ul>"
node["data"].each_pair do |key, value|
properties << "<li><b>#{key}:</b> #{value}</li>"
end
properties + "</ul>"
end
get '/resources/show' do
content_type :json
node = neo.get_node(params[:id])
connections = neo.traverse(node, "fullpath", neighbours)
incoming = Hash.new{|h, k| h[k] = []}
outgoing = Hash.new{|h, k| h[k] = []}
nodes = Hash.new
attributes = Array.new
connections.each do |c|
c["nodes"].each do |n|
nodes[n["self"]] = n["data"]
end
rel = c["relationships"][0]
if rel["end"] == node["self"]
incoming["Incoming:#{rel["type"]}"] << {:values => nodes[rel["start"]].merge({:id => node_id(rel["start"]) }) }
else
outgoing["Outgoing:#{rel["type"]}"] << {:values => nodes[rel["end"]].merge({:id => node_id(rel["end"]) }) }
end
end
incoming.merge(outgoing).each_pair do |key, value|
attributes << {:id => key.split(':').last, :name => key, :values => value.collect{|v| v[:values]} }
end
attributes = [{"name" => "No Relationships","name" => "No Relationships","values" => [{"id" => "#{params[:id]}","name" => "No Relationships "}]}] if attributes.empty?
@node = {:details_html => "<h2>Neo ID: #{node_id(node)}</h2>\n<p class='summary'>\n#{get_properties(node)}</p>\n",
:data => {:attributes => attributes,
:name => node["data"]["name"],
:id => node_id(node)}
}
@node.to_json
end
get '/' do
create_graph
@neoid = params["neoid"]
haml :index
end
end