maca 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +53 -4
- data/lib/maca/macaddress.rb +42 -3
- data/lib/maca/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aee82516c71df181d561ed67d5aab2fbaf266c1e23b463692584e9920bc495a1
|
4
|
+
data.tar.gz: d2374172b04b9ea1f198e94b40872a3d98e4ac1a07219d08c86f642f83f19c1f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 24b2d90e99227d6c1ce894f625932fd561605ec4a60983353869ded49e37ff13849e13702667ba986026bb1a2eaa099e15ba90a11ffb736de9228c14d0ad994a
|
7
|
+
data.tar.gz: 6679b4d4f25160966aed55c642bdfd06cebf176dcdec020b1800e139e4809215bc6e5e098edc212823a6b3c90c23ce11651511d2233bd32c7c1e075ca32971d1
|
data/README.md
CHANGED
@@ -17,17 +17,30 @@ gem install maca
|
|
17
17
|
```
|
18
18
|
|
19
19
|
## Usage
|
20
|
+
### Basic
|
21
|
+
|
22
|
+
Create a new `Macaddress` object from a MAC Address in various formats.
|
20
23
|
|
21
24
|
```ruby
|
22
25
|
require 'maca'
|
23
26
|
|
24
|
-
Macaddress.new("
|
25
|
-
=> #<Macaddress @macaddress="
|
27
|
+
Macaddress.new("01:23:45:67:89:ab")
|
28
|
+
=> #<Macaddress:0x000000012073db08 @macaddress="0123456789ab">
|
26
29
|
|
27
30
|
Macaddress.new("01:23:45:67:89:ab").to_s
|
28
31
|
=> "01:23:45:67:89:AB"
|
29
32
|
|
30
|
-
|
33
|
+
Macaddress.new("01:23:45:67:89:ab").to_i
|
34
|
+
=> 1250999896491
|
35
|
+
```
|
36
|
+
|
37
|
+
### Format String
|
38
|
+
|
39
|
+
**Format Normalization**
|
40
|
+
|
41
|
+
Normalize various MAC Address notations (hyphenated, dot-separated, raw hex, etc.) into the standard colon-separated format.
|
42
|
+
|
43
|
+
```ruby
|
31
44
|
Macaddress.new("00-00-00-00-00-00").to_s
|
32
45
|
=> "00:00:00:00:00:00"
|
33
46
|
|
@@ -39,8 +52,22 @@ Macaddress.new("0000.0000.0000").to_s
|
|
39
52
|
|
40
53
|
Macaddress.new("000000-000000").to_s
|
41
54
|
=> "00:00:00:00:00:00"
|
55
|
+
```
|
42
56
|
|
43
|
-
|
57
|
+
**Format Conversion**
|
58
|
+
|
59
|
+
Convert the MAC Address to a custom string format using a specified delimiter and step size.
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
Macaddress.new("00-00-00-00-00-00").to_fs(delimiter: '.', step: 4)
|
63
|
+
=> "0000.0000.0000"
|
64
|
+
```
|
65
|
+
|
66
|
+
### Comparison
|
67
|
+
|
68
|
+
Compare `Macaddress` objects for equality based on their normalized form.
|
69
|
+
|
70
|
+
```ruby
|
44
71
|
Macaddress.new("00:00:00:00:00:00") == Macaddress.new("00:00:00:00:00:00")
|
45
72
|
=> true
|
46
73
|
|
@@ -48,6 +75,28 @@ Macaddress.new("00:00:00:00:00:00") == Macaddress.new("00:00:00:00:00:01")
|
|
48
75
|
=> false
|
49
76
|
```
|
50
77
|
|
78
|
+
### Address type check
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
Macaddress.new("00:00:00:00:00:00").unicast?
|
82
|
+
=> true
|
83
|
+
|
84
|
+
Macaddress.new("00:00:00:00:00:00").broadcast?
|
85
|
+
=> false
|
86
|
+
|
87
|
+
Macaddress.new("33:33:00:00:00:01").multicast?
|
88
|
+
=> true
|
89
|
+
|
90
|
+
Macaddress.new("02:00:00:00:00:00").locally_administered?
|
91
|
+
=> true
|
92
|
+
|
93
|
+
Macaddress.new("01:00:00:00:00:00").universally_administered?
|
94
|
+
=> true
|
95
|
+
|
96
|
+
Macaddress.new("02:00:00:00:00:00").random?
|
97
|
+
=> true
|
98
|
+
```
|
99
|
+
|
51
100
|
## Development
|
52
101
|
|
53
102
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/maca/macaddress.rb
CHANGED
@@ -2,11 +2,14 @@
|
|
2
2
|
|
3
3
|
module Maca
|
4
4
|
class Macaddress
|
5
|
-
|
5
|
+
DEFAULT_DELIMITER = ':'
|
6
|
+
DEFAULT_STEP = 2
|
7
|
+
DELIMITERS = ':.-'
|
8
|
+
REGEXP_MACADDRESS = /^([0-9A-Fa-f]{2}[#{DELIMITERS}]?){5}([0-9A-Fa-f]{2})$/
|
6
9
|
|
7
10
|
def initialize(addr)
|
8
11
|
if Maca::Macaddress.valid?(addr)
|
9
|
-
@macaddress = addr
|
12
|
+
@macaddress = addr.downcase.delete(DELIMITERS)
|
10
13
|
else
|
11
14
|
raise Maca::InvalidAddressError, "Invalid MAC Address #{addr}"
|
12
15
|
end
|
@@ -17,7 +20,43 @@ module Maca
|
|
17
20
|
end
|
18
21
|
|
19
22
|
def to_s
|
20
|
-
|
23
|
+
to_fs
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_fs(delimiter: DEFAULT_DELIMITER, step: DEFAULT_STEP)
|
27
|
+
raise RangeError, "step must be even, and between 2 and 6" unless (2..6).cover?(step) && step.even?
|
28
|
+
|
29
|
+
@macaddress.upcase.scan(/.{1,#{step}}/).join(delimiter)
|
30
|
+
end
|
31
|
+
alias_method :to_formatted_s, :to_fs
|
32
|
+
|
33
|
+
def to_i
|
34
|
+
@macaddress.hex
|
35
|
+
end
|
36
|
+
|
37
|
+
def broadcast?
|
38
|
+
@macaddress == 'ff' * 6
|
39
|
+
end
|
40
|
+
|
41
|
+
def multicast?
|
42
|
+
(@macaddress[0..1].hex & 0x01).positive?
|
43
|
+
end
|
44
|
+
|
45
|
+
def unicast?
|
46
|
+
!multicast?
|
47
|
+
end
|
48
|
+
|
49
|
+
def locally_administered?
|
50
|
+
return false if broadcast?
|
51
|
+
(@macaddress[0..1].hex & 0x02).positive?
|
52
|
+
end
|
53
|
+
|
54
|
+
def universally_administered?
|
55
|
+
!locally_administered?
|
56
|
+
end
|
57
|
+
|
58
|
+
def random?
|
59
|
+
unicast? && locally_administered?
|
21
60
|
end
|
22
61
|
|
23
62
|
def ==(other)
|
data/lib/maca/version.rb
CHANGED