jsontonetworkgraph 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/jsontonetworkgraph.rb +58 -16
  2. metadata +2 -2
@@ -16,39 +16,81 @@ class JSONToNetworkGraph
16
16
  # Generate all the nodes
17
17
  def genNodes
18
18
  @input.each do |i|
19
- addupdateNode(@field1, i)
20
- addupdateNode(@field2, i)
19
+ if !(i[@field1].nil? || i[@field2].nil? || i[@field1].empty? || i[@field2].empty?)
20
+ arrayCheckNode(@field1, i[@field1], i)
21
+ arrayCheckNode(@field2, i[@field2], i)
22
+ end
23
+ end
24
+ end
25
+
26
+ # Handle fields that are arrays
27
+ def arrayCheckNode(fieldname, fieldvalue, i)
28
+ if fieldvalue.is_a? Array
29
+ fieldvalue.each do |a|
30
+ addupdateNode(fieldname, a, i)
31
+ end
32
+ else
33
+ addupdateNode(fieldname, fieldvalue, i)
21
34
  end
22
35
  end
23
36
 
24
37
  # 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)
38
+ def addupdateNode(fieldname, fieldvalue, i)
39
+ if !(@nodehash.include? fieldvalue)
40
+ @nodehash[fieldvalue] = Node.new(@nodeindex, fieldvalue, fieldname, i)
28
41
  @nodeindex += 1
29
42
  else
30
- @nodehash[i[fieldname]].update(i)
43
+ @nodehash[fieldvalue].update(i)
31
44
  end
32
45
  end
33
46
 
34
47
  # Generate all the links
35
48
  def genLinks
36
49
  @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
50
+ if !(i[@field1].nil? || i[@field2].nil? || i[@field1].empty? || i[@field2].empty?)
51
+ arrayCheckLink(i[@field1], i[@field2], i)
52
+ end
53
+ end
54
+ end
44
55
 
45
- @linkhash[identifier] = Link.new(source, target, i[@field1], i[@field2], i)
46
- else
47
- @linkhash[identifier].update(i)
56
+ # Handle fields that are arrays
57
+ def arrayCheckLink(fieldvalue1, fieldvalue2, i)
58
+ if (fieldvalue1.is_a? Array) && !(fieldvalue2.is_a? Array)
59
+ fieldvalue1.each do |a|
60
+ addupdateLink(a, fieldvalue2, i)
61
+ end
62
+ elsif (fieldvalue2.is_a? Array) && !(fieldvalue1.is_a? Array)
63
+ fieldvalue2.each do |a|
64
+ addupdateLink(fieldvalue1, a, i)
65
+ end
66
+ elsif (fieldvalue1.is_a? Array) && (fieldvalue2.is_a? Array)
67
+ fieldvalue1.each do |a|
68
+ fieldvalue2.each do |b|
69
+ addupateLink(a, b, i)
70
+ end
48
71
  end
72
+ else
73
+ addupdateLink(fieldvalue1, fieldvalue2, i)
74
+ end
75
+ end
76
+
77
+ # Create or update the appropriate link
78
+ def addupdateLink(fieldvalue1, fieldvalue2, i)
79
+ identifier = fieldvalue1 + "-" + fieldvalue2
80
+ if !@linkhash.include? identifier
81
+ source = @nodehash[fieldvalue1].getID
82
+ target = @nodehash[fieldvalue2].getID
83
+
84
+ @nodehash[fieldvalue1].addLink
85
+ @nodehash[fieldvalue2].addLink
86
+
87
+ @linkhash[identifier] = Link.new(source, target, fieldvalue1, fieldvalue2, i)
88
+ else
89
+ @linkhash[identifier].update(i)
49
90
  end
50
91
  end
51
92
 
93
+ # Count the number of links
52
94
  def linkCount
53
95
  @linkhash.each_value do |l|
54
96
  count1 = @nodehash[l.instance_variable_get(:@field1)].instance_variable_get(:@linkcount)
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.2
4
+ version: 0.0.3
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-21 00:00:00.000000000 Z
12
+ date: 2014-02-23 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