jsontonetworkgraph 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/jsontonetworkgraph.rb +71 -0
- data/lib/link.rb +18 -0
- data/lib/node.rb +26 -0
- metadata +49 -0
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'json'
|
2
|
+
load 'node.rb'
|
3
|
+
load 'link.rb'
|
4
|
+
|
5
|
+
class JSONToNetworkGraph
|
6
|
+
|
7
|
+
def initialize(input, field1, field2)
|
8
|
+
@input = JSON.parse(input)
|
9
|
+
@field1 = field1
|
10
|
+
@field2 = field2
|
11
|
+
@nodehash = Hash.new
|
12
|
+
@linkhash = Hash.new
|
13
|
+
@nodeindex = 0
|
14
|
+
end
|
15
|
+
|
16
|
+
# Generate all the nodes
|
17
|
+
def genNodes
|
18
|
+
@input.each do |i|
|
19
|
+
addupdateNode(@field1, i)
|
20
|
+
addupdateNode(@field2, i)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# Create or update the appropriate node
|
25
|
+
def addupdateNode(fieldname, i)
|
26
|
+
if !(@nodehash.include? i[fieldname])
|
27
|
+
@nodehash[i[fieldname]] = Node.new(@nodeindex, i[fieldname], fieldname, i)
|
28
|
+
@nodeindex += 1
|
29
|
+
else
|
30
|
+
@nodehash[i[fieldname]].update(i)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# Generate all the links
|
35
|
+
def genLinks
|
36
|
+
@input.each do |i|
|
37
|
+
identifier = i[@field1]+ "-" + i[@field2]
|
38
|
+
if !@linkhash.include? identifier
|
39
|
+
source = @nodehash[i[@field1]].getID
|
40
|
+
target = @nodehash[i[@field2]].getID
|
41
|
+
|
42
|
+
@nodehash[i[@field1]].addLink
|
43
|
+
@nodehash[i[@field2]].addLink
|
44
|
+
|
45
|
+
@linkhash[identifier] = Link.new(source, target, i[@field1], i[@field2], i)
|
46
|
+
else
|
47
|
+
@linkhash[identifier].update(i)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# Generate JSON with nodes and links
|
53
|
+
def genJSON
|
54
|
+
genNodes
|
55
|
+
genLinks
|
56
|
+
|
57
|
+
nodearray = Array.new
|
58
|
+
@nodehash.each_value do |n|
|
59
|
+
nodearray.push(n.nodeData)
|
60
|
+
end
|
61
|
+
|
62
|
+
linkarray = Array.new
|
63
|
+
@linkhash.each_value do |l|
|
64
|
+
linkarray.push(l.linkData)
|
65
|
+
end
|
66
|
+
|
67
|
+
jsonhash = {:nodes => nodearray, :links => linkarray}
|
68
|
+
JSON.pretty_generate(jsonhash)
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
data/lib/link.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
class Link
|
2
|
+
def initialize(source, target, field1, field2, value)
|
3
|
+
@source = source
|
4
|
+
@target = target
|
5
|
+
@field1 = field1
|
6
|
+
@field2 = field2
|
7
|
+
@value = Array.new
|
8
|
+
@value.push(value)
|
9
|
+
end
|
10
|
+
|
11
|
+
def update(toadd)
|
12
|
+
@value.push(toadd)
|
13
|
+
end
|
14
|
+
|
15
|
+
def linkData
|
16
|
+
link_hash = {:source => @source, :target => @target, :field1 => @field1, :field2 => @field2, :data => @value}
|
17
|
+
end
|
18
|
+
end
|
data/lib/node.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
class Node
|
2
|
+
def initialize(num, name, type, value)
|
3
|
+
@num = num
|
4
|
+
@name = name
|
5
|
+
@type = type
|
6
|
+
@linkcount = 0
|
7
|
+
@value = Array.new
|
8
|
+
@value.push(value)
|
9
|
+
end
|
10
|
+
|
11
|
+
def update(toadd)
|
12
|
+
@value.push(toadd)
|
13
|
+
end
|
14
|
+
|
15
|
+
def getID
|
16
|
+
return @num
|
17
|
+
end
|
18
|
+
|
19
|
+
def addLink
|
20
|
+
@linkcount += 1
|
21
|
+
end
|
22
|
+
|
23
|
+
def nodeData
|
24
|
+
json_hash = {:id => @name, :type => @type, :linkcount => @linkcount, :data => @value}
|
25
|
+
end
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jsontonetworkgraph
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- M. C. McGrath
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-02-20 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Generates node and link data from any JSON.
|
15
|
+
email: shidash@shidash.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/jsontonetworkgraph.rb
|
21
|
+
- lib/node.rb
|
22
|
+
- lib/link.rb
|
23
|
+
homepage: https://github.com/Shidash/JSONToNetworkGraph
|
24
|
+
licenses:
|
25
|
+
- GPL
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ! '>='
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
requirements: []
|
43
|
+
rubyforge_project:
|
44
|
+
rubygems_version: 1.8.23
|
45
|
+
signing_key:
|
46
|
+
specification_version: 3
|
47
|
+
summary: Turn JSONs into network graphs
|
48
|
+
test_files: []
|
49
|
+
has_rdoc:
|