mac_address 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mac_address.rb +20 -9
- data/lib/mac_address/version.rb +1 -1
- data/lib/mac_address/version.rb~ +1 -1
- data/test/mac_address_test.rb +21 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c118549679a92fcacaa8390c4a1c0d95357440f3
|
4
|
+
data.tar.gz: e42d4ebbc91d2139d88f7a86923ba1f01f7eaa94
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5976c405c52eaf7f046cdcadd42f7d3b0cdce02fef264cc8831618811af3a30928eff7972d1fec4c0d8252f0cf557645b7d8f70ecf2281cf6c825b4628828a91
|
7
|
+
data.tar.gz: ca5f8859ddb7f018310fb2a28c7343276f7a2cfb35eb1d15032acc64cb2547e0eae4927e13b17bd80e6d5f60ecac7edc0578b87d7914dc7d1473d7558ac4abbb
|
data/lib/mac_address.rb
CHANGED
@@ -9,29 +9,40 @@ class MacAddress
|
|
9
9
|
@mac_str = @mac_str.split(':')[0]
|
10
10
|
end
|
11
11
|
@mac_str = @mac_str.downcase.gsub(/^0[xX]/,'').gsub(/[^0-9a-f]/,'')
|
12
|
-
|
12
|
+
|
13
13
|
raise ArgumentError.new("Invalid MAC address: #{str}") if @mac_str.length != 12
|
14
14
|
end
|
15
|
+
|
15
16
|
def to_s
|
16
17
|
@mac_str
|
17
18
|
end
|
19
|
+
|
18
20
|
def to_i
|
19
21
|
@mac_str.hex
|
20
22
|
end
|
23
|
+
|
24
|
+
def self.validate_strict(mac)
|
25
|
+
!!(mac =~ /^([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}$/i)
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.validate(mac)
|
29
|
+
MacAddress.new(mac).to_s
|
30
|
+
true
|
31
|
+
rescue ArgumentError
|
32
|
+
false
|
33
|
+
end
|
21
34
|
end
|
22
35
|
|
23
36
|
class String
|
24
37
|
def to_mac
|
25
38
|
MacAddress.new(self).to_s
|
26
39
|
end
|
27
|
-
|
28
|
-
def valid_mac?
|
29
|
-
|
30
|
-
MacAddress.
|
31
|
-
|
32
|
-
|
40
|
+
|
41
|
+
def valid_mac?(options = {})
|
42
|
+
if options[:strict]
|
43
|
+
MacAddress.validate_strict(self)
|
44
|
+
else
|
45
|
+
MacAddress.validate(self)
|
33
46
|
end
|
34
|
-
|
35
|
-
true
|
36
47
|
end
|
37
48
|
end
|
data/lib/mac_address/version.rb
CHANGED
data/lib/mac_address/version.rb~
CHANGED
data/test/mac_address_test.rb
CHANGED
@@ -5,8 +5,28 @@ class MacAddressTest < Test::Unit::TestCase
|
|
5
5
|
def test_truth
|
6
6
|
assert_kind_of Module, MacAddress
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
9
|
def test_mac
|
10
10
|
assert_equal 'aabbccddeeff', 'AA-BB-CC-DD-EE-FF:hello world'.to_mac
|
11
11
|
end
|
12
|
+
|
13
|
+
VALID_MACS = %w[aa:bb:cc:dd:ee:ff 00:11:22:33:44:55]
|
14
|
+
INVALID_MACS = %w[invalid aabbccddeeffff aa:bb:cc:dd:ee:ff:11]
|
15
|
+
INVALID_MACS_STRICT = %w[aabbccddeeff aabbccddeeffxx aa:bb:cc:dd:ee:ff:xx 00:55:56:82:11::38 0011.2233.445566]
|
16
|
+
|
17
|
+
def test_valid_mac?
|
18
|
+
VALID_MACS.each do |valid_mac|
|
19
|
+
assert_equal valid_mac.valid_mac?, true
|
20
|
+
assert_equal valid_mac.valid_mac?(strict: true), true
|
21
|
+
end
|
22
|
+
|
23
|
+
INVALID_MACS.each do |invalid_mac|
|
24
|
+
assert_equal invalid_mac.valid_mac?, false
|
25
|
+
assert_equal invalid_mac.valid_mac?(strict: true), false
|
26
|
+
end
|
27
|
+
|
28
|
+
INVALID_MACS_STRICT.each do |invalid_mac|
|
29
|
+
assert_equal invalid_mac.valid_mac?(strict: true), false
|
30
|
+
end
|
31
|
+
end
|
12
32
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mac_address
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Doug Wiegley
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-05-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: str_quote
|