subnet_calc 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a8e521e99a577d3c78b49a8400f3b38133150d4d
4
- data.tar.gz: 0c006f2519c6aa9916020d326b481fa019031c30
3
+ metadata.gz: 6765bee68b5f48ecbac6fdbb859652cd6146b9d7
4
+ data.tar.gz: '049853d600678fc06a67c0035f98ff8883d80891'
5
5
  SHA512:
6
- metadata.gz: c355f531b300e7a96dfa0e1f21ef771de13a69714a2a81836766aa4ec4419a810eb02112134d9e897ed505a7ec5a3f88ac7dfa13c77d3713da25f42a54b3803c
7
- data.tar.gz: 3f275b0feb9baaa4bc2548d19ae8e3fd101f801a75b58ef0798bfa220d3cf111e29b6c8515a9aeb61cc8837fdaa30b35d6bd54009659f56892f0670873d00fe1
6
+ metadata.gz: f9ee059a44c6de04445fcb49e64568649995b3d4d4dde08e004269423d69acda1539bf7d7085868bc75969e73f9b4bba86f9f6dda0f17d82ba6bcd84125dde41
7
+ data.tar.gz: f50ca74be7b2637c3244e264e45b0067f575748b57961388ddd79fea802c88d026838d742629366511fff444d5389506227de9bf9619911421725661041ca222
checksums.yaml.gz.sig CHANGED
Binary file
data/lib/subnet_calc.rb CHANGED
@@ -30,22 +30,39 @@ class SubnetCalc
30
30
  default_inputs = {hosts: 254, ip: '192.168.0.0'}.merge(inputs)
31
31
 
32
32
  @prefixes = 1.upto(32).each_slice(8).to_a
33
+
33
34
  =begin
34
35
  #=> [
35
36
  [1, 2, 3, 4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14, 15, 16],
36
37
  [17, 18, 19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30, 31, 32]
37
38
  ]
38
39
  =end
40
+
39
41
 
40
- octets = @prefixes.map do |octet|
42
+ octets = @prefixes.map.with_index do |octet, j|
41
43
 
42
44
  a = octet.map.with_index do |prefix, i|
43
- OpenStruct.new prefix: prefix, decimal: 2 ** (octet.length - 1 - i )
45
+
46
+ decimal = 2 ** (octet.length - 1 - i )
47
+
48
+ h = {
49
+ prefix: prefix,
50
+ decimal: decimal,
51
+ hosts_per_subnet: (2 ** 8) ** (@prefixes.length - 1 - j) * decimal
52
+ }
53
+
54
+ OpenStruct.new h
55
+
44
56
  end
45
57
 
46
58
  OpenStruct.new mask: nil, bits: a
47
59
 
48
60
  end
61
+
62
+ hosts_per_subnet = octets.map.with_index do |x,i|
63
+ x.bits.map.with_index {|y,j| [i, j, y.hosts_per_subnet] }
64
+ end.flatten(1)
65
+
49
66
 
50
67
  # determine what class of network we are using
51
68
 
@@ -74,19 +91,24 @@ class SubnetCalc
74
91
 
75
92
  hosts = default_inputs[:hosts]
76
93
 
94
+
95
+
77
96
  # find the smallest decimal value to match the
78
- # required number of hosts on a network
97
+ # required number of hosts on a network
98
+
99
+ octet_n, col = hosts_per_subnet.reverse.detect {|x| x.last > hosts}
79
100
 
80
- col = octets[class_n].bits.reverse.detect {|x| x.decimal > hosts }
101
+ bit = octets[octet_n].bits[col]
81
102
 
82
- magic_number, prefix = col.decimal, col.prefix
83
- n = octets[class_n].bits.map(&:decimal).index(magic_number)
84
- no_of_subnets = 2 ** (n+1)
103
+ magic_number, prefix = bit.hosts_per_subnet, bit.prefix
104
+ no_of_subnets = (2 ** 8) / bit.decimal
85
105
 
106
+
107
+ n = col
86
108
 
87
109
  # add the new mask to the octet
88
110
 
89
- octets[class_n].mask = octets[class_n].bits.map(&:decimal)[0..n].inject(:+)
111
+ octets[octet_n].mask = octets[octet_n].bits.map(&:decimal)[0..n].inject(:+)
90
112
  subnet_mask = octets.map(&:mask).join('.')
91
113
 
92
114
  subnets = no_of_subnets.times.inject([]) do |r,n|
@@ -119,7 +141,7 @@ class SubnetCalc
119
141
  max_subnets: no_of_subnets
120
142
  }
121
143
 
122
- @class_n = class_n
144
+ @octet_n = octet_n
123
145
  @h = result
124
146
  end
125
147
 
@@ -137,12 +159,11 @@ class SubnetCalc
137
159
 
138
160
  tfo2 = TableFormatter.new
139
161
 
140
- prefixes = @prefixes[@class_n].map {|x| '/' + x.to_s}
141
- tfo2.source = [@h[:subnet_bitmask][@class_n].chars, prefixes]
162
+ prefixes = @prefixes[@octet_n].map {|x| '/' + x.to_s}
163
+ tfo2.source = [@h[:subnet_bitmask][@octet_n].chars, prefixes]
142
164
  tfo2.labels = 8.times.map {|n| (2 ** n).to_s }.reverse
143
165
  octet_table = tfo2.display(markdown: true).to_s
144
166
 
145
-
146
167
  <<EOF
147
168
 
148
169
 
@@ -172,7 +193,7 @@ Summary
172
193
  Breakdown
173
194
  ---------
174
195
 
175
- #{(@class_n + 1).ordinal} octet:
196
+ #{(@octet_n + 1).ordinal} octet:
176
197
 
177
198
  #{indent octet_table}
178
199
 
@@ -191,9 +212,9 @@ EOF
191
212
  @h
192
213
  end
193
214
 
194
- private
215
+ private
195
216
 
196
217
  def indent(s, i=2)
197
218
  s.lines.map{|x| ' ' * i + x}.join
198
219
  end
199
- end
220
+ end
data.tar.gz.sig CHANGED
@@ -1 +1 @@
1
- 1Kz�ݧ��a�� o���3
1
+ ��Jў`��;V"Fz��=T����?ʖu���d����{_��'��f�Ӽ���խ`^�w�� V[��Ra4n����A��g���É�]�ϋ��i�<H��d�߹"�&Zb�Pą���^d�A{���J��fV�f���|^�|��� ]6�ob����ݕ���-̊���tg�`��$��0���b��o��w���`�xϮ�A���m`&A�EIO��K������!Q�$d_�W���4!�`jT���G��q�91Я�
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: subnet_calc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
metadata.gz.sig CHANGED
Binary file