mac_address 0.0.2 → 0.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 85c5c73e4237d5d8ae0e79dcaa3008c021ae94c1
4
- data.tar.gz: b0acba3a3c515f8fce3466d4db8f7b252e8a3069
3
+ metadata.gz: c118549679a92fcacaa8390c4a1c0d95357440f3
4
+ data.tar.gz: e42d4ebbc91d2139d88f7a86923ba1f01f7eaa94
5
5
  SHA512:
6
- metadata.gz: 15e18c1a43dc7f7c576f3b9102c6baea3a7745382672f9472a5ba8ff277dd49920e367746e1df2aa6c8e3d39c6e1ea42ac48014a988b4e31422d3520dce15ea1
7
- data.tar.gz: 56bef829c38938e0cb93f360a10b5e1849affad14a3b75da4782d4e0a6c0866b19a44724bc41233253e0b219c0f9213d74b0f4f91ef79a88cc17e3ff4b336762
6
+ metadata.gz: 5976c405c52eaf7f046cdcadd42f7d3b0cdce02fef264cc8831618811af3a30928eff7972d1fec4c0d8252f0cf557645b7d8f70ecf2281cf6c825b4628828a91
7
+ data.tar.gz: ca5f8859ddb7f018310fb2a28c7343276f7a2cfb35eb1d15032acc64cb2547e0eae4927e13b17bd80e6d5f60ecac7edc0578b87d7914dc7d1473d7558ac4abbb
@@ -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
- begin
30
- MacAddress.new(self).to_s
31
- rescue ArgumentError => e
32
- return false
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
@@ -1,3 +1,3 @@
1
1
  class MacAddress
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -1,3 +1,3 @@
1
1
  class MacAddress
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -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.2
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: 2013-08-29 00:00:00.000000000 Z
11
+ date: 2014-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: str_quote