subnet_calc 0.2.0 → 0.2.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: 4bb254774e26a03f73b61199cb800765362710d7
4
- data.tar.gz: b534f281a42f5d2119145881ebe9852fe5be0eb6
3
+ metadata.gz: 7f08208310333a341c9f31757534e9e40986c827
4
+ data.tar.gz: c7a36481efddf08a866a1ed2697f49f0044dbfd4
5
5
  SHA512:
6
- metadata.gz: 435ad51490ff9c2b322e609917333c2450b2ac24621c5643ad8b453513192e4f279961b23f15d1b9e6fa2c4ca406e20614cd191ea872b27be076af1d53b9117c
7
- data.tar.gz: 71c37c544ed68cf59919a8e41a9fc934d1c5be678f77cf8866e635520d9133575075c95023f72d396f1e34b1b1be8c7521c602257f3ef4ea748278e93a77b476
6
+ metadata.gz: 71f6d30b105979461de671031e9ded95c93f98db2808571d3f63462908a3c7194502a75d73c5e1769b9ad20dfe08c667401665df464176158573991314194159
7
+ data.tar.gz: a0bce9a42f943f9e654a25abb4aaad74c1bafc5955dfed872218f477aeea9508eb71e6bd5100ce105376e317035efc2de149439626177212f5956a86ec95cbeb
checksums.yaml.gz.sig CHANGED
@@ -1,3 +1,3 @@
1
- y���>M���V��+�b'C}�|����ò8�-:����/TEs�?���D[d�ZkSD�{�
2
- iqB���'�͹�
3
- �ܦ�|$�=Q��Y5��q��t��1nm��F�dZ��x��򁖗J��Ҥ�?��om-�ro��,({/8�@���9i}��~MYQ���������x�bJ��i@ ���;�r0hf�n�+�-��(�����=��3
1
+ cl6���/��ں�
2
+ ׈��R?-�2R�AE�-/U�>��1�r�ܵ�%�S��ϵ�W�J�t�i� N������A�(��V�S��3tvg�ڕe���[3* $�c:�������W2�p��e?�ټ{|ha������w(��@���[�.�:'HV�
3
+ "�F<�m��޷�k��pTK�e'��G��2��֎
data.tar.gz.sig CHANGED
Binary file
data/lib/subnet_calc.rb CHANGED
@@ -24,14 +24,14 @@ class SubnetCalc
24
24
 
25
25
  attr_reader :to_h
26
26
 
27
- def initialize(inputs={hosts: 254})
27
+ def initialize(inputs={})
28
28
 
29
29
  @inputs = inputs
30
- default_inputs = {hosts: 254, ip: '192.168.0.0'}.merge(inputs)
30
+ default_inputs = {hosts: nil, ip: '192.168.0.0', prefix: nil}.merge(inputs)
31
31
 
32
32
  # Using the input(s) supplied:
33
33
 
34
- hosts = default_inputs[:hosts]
34
+ hosts, prefix = %i(hosts prefix).map {|x| default_inputs[x]}
35
35
 
36
36
 
37
37
  @prefixes = 1.upto(32).each_slice(8).to_a
@@ -64,14 +64,33 @@ class SubnetCalc
64
64
 
65
65
  end
66
66
 
67
- hosts_per_subnet = octets.map.with_index do |x,i|
68
- x.bits.map.with_index {|y,j| [i, j, y.hosts_per_subnet] }
69
- end.flatten(1)
67
+
68
+ # ------------------------------------
69
+
70
+
71
+ octet_n, col = if hosts then
72
+
73
+ hosts_per_subnet = octets.flat_map.with_index do |octet,i|
74
+ octet.bits.map.with_index {|y,j| [i, j, y.hosts_per_subnet] }
75
+ end
76
+
77
+ # find the smallest decimal value to match the
78
+ # required number of hosts on a network
79
+
80
+ hosts_per_subnet.reverse.detect {|x| x.last > hosts + 1}
70
81
 
82
+ elsif prefix
71
83
 
72
- # determine what class of network we are using
84
+ prefixes = @prefixes.flat_map.with_index do |octet,i|
85
+ octet.map.with_index {|x,j| [i, j, x]}
86
+ end
87
+
88
+ prefixes.detect {|x| x.last == prefix}
89
+
90
+ end
73
91
 
74
- class_type = hosts <= 254 ? 'c' : 'b'
92
+ # determine what class of network we are using
93
+ class_type = (hosts and hosts <= 254 or prefix >= 24) ? 'c' : 'b'
75
94
 
76
95
  # Identify the network bits and the host bits
77
96
 
@@ -80,6 +99,7 @@ class SubnetCalc
80
99
  # identify the initial network bits for a class *a,b or c*
81
100
 
82
101
  class_n = (classes.to_a.index(class_type) + 1)
102
+
83
103
  network_bits = class_n.times.map {|x| Array.new(8, 1)}
84
104
  host_bits = (classes.to_a.length - class_n).times.map {|x| Array.new(8, 0)}
85
105
  address_bits = network_bits + host_bits
@@ -90,14 +110,7 @@ class SubnetCalc
90
110
  octet.mask = address_bits[i].join.to_i(2)
91
111
  end
92
112
 
93
- # ------------------------------------
94
-
95
-
96
113
 
97
- # find the smallest decimal value to match the
98
- # required number of hosts on a network
99
-
100
- octet_n, col = hosts_per_subnet.reverse.detect {|x| x.last > hosts + 1}
101
114
 
102
115
  bit = octets[octet_n].bits[col]
103
116
 
@@ -105,7 +118,8 @@ class SubnetCalc
105
118
 
106
119
  no_of_subnets = (2 ** 8.0) / bit.hosts_per_subnet
107
120
 
108
- segments = (no_of_subnets >= 1 ? no_of_subnets : 256 / (256 * no_of_subnets)).to_i
121
+ segments = (no_of_subnets >= 1 ? no_of_subnets :
122
+ 256 / (256 * no_of_subnets)).to_i
109
123
  n = col
110
124
 
111
125
  # add the new mask to the octet
@@ -148,7 +162,10 @@ class SubnetCalc
148
162
 
149
163
  tfo = TableFormatter.new
150
164
 
151
- tfo.source = @h[:subnets].map.with_index{|x,i| ([i] + x.to_h.values).map(&:to_s) }
165
+ tfo.source = @h[:subnets].map.with_index do |x,i|
166
+ ([i+1] + x.to_h.values).map(&:to_s)
167
+ end
168
+
152
169
  tfo.labels = %w(index Network 1st last broadcast)
153
170
  full_subnets_table = tfo.display(markdown: true).to_s
154
171
 
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.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
metadata.gz.sig CHANGED
Binary file