jsontonetworkgraph 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/jsontonetworkgraph.rb +48 -1
  2. data/lib/node.rb +18 -1
  3. metadata +2 -2
@@ -4,12 +4,16 @@ load 'link.rb'
4
4
 
5
5
  class JSONToNetworkGraph
6
6
 
7
- def initialize(input, field1, field2)
7
+ def initialize(input, field1, group1, field2, group2)
8
8
  @input = JSON.parse(input)
9
9
  @field1 = field1
10
10
  @field2 = field2
11
+ @group1 = group1
12
+ @group2 = group2
11
13
  @nodehash = Hash.new
12
14
  @linkhash = Hash.new
15
+ @grouphash = Hash.new
16
+ @groupnum = 0
13
17
  @nodeindex = 0
14
18
  end
15
19
 
@@ -104,11 +108,51 @@ class JSONToNetworkGraph
104
108
  end
105
109
  end
106
110
 
111
+ # Groups nodes based on another field
112
+ def groupNodes(groupfield, nodetype)
113
+ @input.each do |i|
114
+ if !(i[@field1].nil? || i[@field2].nil? || i[@field1].empty? || i[@field2].empty?)
115
+
116
+ # Handles fields that are arrays for groups
117
+ if i[groupfield].is_a? Array
118
+ i[groupfield].each do |a|
119
+ if !@grouphash.include? a
120
+ @groupnum += 1
121
+ @grouphash[a] = @groupnum
122
+ end
123
+ end
124
+
125
+ # Adds non-array values to grouphash
126
+ else
127
+ if !@grouphash.include? i[groupfield]
128
+ @groupnum += 1
129
+ @grouphash[i[groupfield]] = @groupnum
130
+ end
131
+ end
132
+ end
133
+ end
134
+
135
+ # Assigns the correct value to each node from the grouphash
136
+ @nodehash.each_value do |n|
137
+ if n.getType == nodetype
138
+ n.getValue.each do |v|
139
+ if v[groupfield].is_a? Array
140
+ n.setGroup(@grouphash[n.getName])
141
+ else
142
+ n.setGroup(@grouphash[v[groupfield]])
143
+ end
144
+ end
145
+ end
146
+ end
147
+ end
148
+
107
149
  # Generate JSON with nodes and links
108
150
  def genJSON
109
151
  genNodes
110
152
  genLinks
111
153
  linkCount
154
+ groupNodes(@group1, @field1)
155
+ groupNodes(@group2, @field2)
112
156
 
113
157
  nodearray = Array.new
114
158
  @nodehash.each_value do |n|
@@ -125,3 +169,6 @@ class JSONToNetworkGraph
125
169
  end
126
170
 
127
171
  end
172
+
173
+ n = JSONToNetworkGraph.new(File.read("data.json"), "name", "company", "extract", "extract")
174
+ puts n.genJSON
data/lib/node.rb CHANGED
@@ -3,6 +3,7 @@ class Node
3
3
  @num = num
4
4
  @name = name
5
5
  @type = type
6
+ @group = nil
6
7
  @linkcount = 0
7
8
  @value = Array.new
8
9
  @value.push(value)
@@ -16,11 +17,27 @@ class Node
16
17
  return @num
17
18
  end
18
19
 
20
+ def getName
21
+ return @name
22
+ end
23
+
24
+ def getType
25
+ return @type
26
+ end
27
+
28
+ def getValue
29
+ return @value
30
+ end
31
+
32
+ def setGroup(group)
33
+ @group = group
34
+ end
35
+
19
36
  def addLink
20
37
  @linkcount += 1
21
38
  end
22
39
 
23
40
  def nodeData
24
- json_hash = {:id => @name, :type => @type, :linkcount => @linkcount, :data => @value}
41
+ json_hash = {:name => @name, :type => @type, :group => @group, :linkcount => @linkcount, :data => @value}
25
42
  end
26
43
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsontonetworkgraph
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-02-23 00:00:00.000000000 Z
12
+ date: 2014-02-24 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Generates node and link data from any JSON.
15
15
  email: shidash@shidash.com