mac_address 0.0.3 → 0.0.4

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: c118549679a92fcacaa8390c4a1c0d95357440f3
4
- data.tar.gz: e42d4ebbc91d2139d88f7a86923ba1f01f7eaa94
3
+ metadata.gz: c43f112f891d36f920a3bb25a2a31970041a703a
4
+ data.tar.gz: 55e14042efbf71216d56dd6cb546dffb008dad23
5
5
  SHA512:
6
- metadata.gz: 5976c405c52eaf7f046cdcadd42f7d3b0cdce02fef264cc8831618811af3a30928eff7972d1fec4c0d8252f0cf557645b7d8f70ecf2281cf6c825b4628828a91
7
- data.tar.gz: ca5f8859ddb7f018310fb2a28c7343276f7a2cfb35eb1d15032acc64cb2547e0eae4927e13b17bd80e6d5f60ecac7edc0578b87d7914dc7d1473d7558ac4abbb
6
+ metadata.gz: 2cc9b9046eddc6c56828fa3bea6c8baafbdf9a03a47a4f8251f09f74387cc6b2f8057b351d65ea50725ba715f324d472a9849c837eb7cc6aa7567d07894689d5
7
+ data.tar.gz: 5ba83ddd0ee5e8633f0d24db413af238f26fb9eff80f68297f2756110f913bd89f6ea0679aaef66367aaaecbc09647a78debf47954531cd7f353e1a155792267
data/README.md CHANGED
@@ -1,2 +1,72 @@
1
- mac_address
2
- ===========
1
+ # Mac Address
2
+
3
+ [![Build Status](https://travis-ci.org/uceem/mac_address.svg?branch=master)](https://travis-ci.org/uceem/mac_address)
4
+
5
+ A Mac Address utility that can validate mac address formats and convert strings to valid macs.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```
12
+ gem 'mac_address'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ ```
18
+ $ bundle
19
+ ```
20
+
21
+ Or install it yourself as:
22
+
23
+ ```
24
+ $ gem install mac_address
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ ```ruby
30
+ MacAddress.new("aa:bb:cc:dd:ee:ff")
31
+ # => #<MacAddress:0x007fe783981788 @mac_str="aabbccddeeff">
32
+
33
+ MacAddress.new("aa:bb:cc:dd:ee:ff").to_s # => 'aabbccddee'
34
+ MacAddress.new("invalid mac") # => Raises ArgumentError
35
+ MacAddress.new("aa:bb:cc:dd:ee:ff").to_i # => 187723572702975
36
+ ```
37
+
38
+ The MacAddress class will also remove any cruft or extraneous content. If there's a mac to be found, MacAddress will find it:
39
+
40
+ ```ruby
41
+ MacAddress.new("aa:bb:cc:dd:ee:ff:qqqqq")
42
+ # => #<MacAddress:0x007fe783981788 @mac_str="aabbccddeeff">
43
+
44
+ MacAddress.new("&*^*&(aa:bb:cc:dd:ee:ff:")
45
+ # => #<MacAddress:0x007fe783981788 @mac_str="aabbccddeeff">
46
+ ```
47
+
48
+ ### String Extending
49
+
50
+ ```ruby
51
+ 'aa:bb:cc:dd:ee:ff'.to_mac # => 'aabbccddeeff'
52
+ 'aa:bb:cc:dd:ee:ff:xx'.to_mac # => 'aabbccddeeff'
53
+ ''.to_mac # => Raises ArgumentError
54
+ 'aa:bb:cc:dd:ee:ff'.valid_mac? # => true
55
+ 'aa:bb:cc:dd:ee:ff:*&^*qqqq'.valid_mac? # => true
56
+ 'aabbcc'.valid_mac? # => false
57
+ ```
58
+
59
+ Enable strict mode for validation.
60
+
61
+ ```ruby
62
+ 'aa:bb:cc:dd:ee:ff'.valid_mac?(strict: true) # => true
63
+ 'aa:bb:cc:dd:ee:ff:*&^*qqqq'.valid_mac?(strict: true) # => false
64
+ ```
65
+
66
+ ## Contributing
67
+
68
+ 1. Fork it
69
+ 2. Create your feature branch (git checkout -b my-new-feature)
70
+ 3. Commit your changes (git commit -am 'Add some feature')
71
+ 4. Push to the branch (git push origin my-new-feature)
72
+ 5. Create new Pull Request
@@ -2,8 +2,13 @@ require 'str_quote'
2
2
  require 'mac_address/version'
3
3
 
4
4
  class MacAddress
5
- def initialize(str)
5
+ def initialize(str, options = {})
6
6
  @mac_str = str.strip.dequote.strip
7
+
8
+ if options[:strict] && !MacAddress.validate_strict(@mac_str)
9
+ raise_argument_error(@mac_str)
10
+ end
11
+
7
12
  n = @mac_str.index(':')
8
13
  if not n.nil? and n >= 12
9
14
  @mac_str = @mac_str.split(':')[0]
@@ -25,24 +30,30 @@ class MacAddress
25
30
  !!(mac =~ /^([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}$/i)
26
31
  end
27
32
 
28
- def self.validate(mac)
29
- MacAddress.new(mac).to_s
30
- true
33
+ def self.validate(mac, options = {})
34
+ if options[:strict]
35
+ MacAddress.validate_strict(mac)
36
+ else
37
+ MacAddress.new(mac).to_s
38
+ true
39
+ end
31
40
  rescue ArgumentError
32
41
  false
33
42
  end
43
+
44
+ private
45
+
46
+ def raise_argument_error(mac_str, error_str = nil)
47
+ raise ArgumentError.new(error_str || "Invalid MAC address: #{mac_str}")
48
+ end
34
49
  end
35
50
 
36
51
  class String
37
- def to_mac
38
- MacAddress.new(self).to_s
52
+ def to_mac(options = {})
53
+ MacAddress.new(self, options).to_s
39
54
  end
40
55
 
41
56
  def valid_mac?(options = {})
42
- if options[:strict]
43
- MacAddress.validate_strict(self)
44
- else
45
- MacAddress.validate(self)
46
- end
57
+ MacAddress.validate(self, options)
47
58
  end
48
59
  end
@@ -1,3 +1,3 @@
1
1
  class MacAddress
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -2,6 +2,11 @@ require 'test/unit'
2
2
  require 'mac_address'
3
3
 
4
4
  class MacAddressTest < Test::Unit::TestCase
5
+
6
+ VALID_MACS = %w[aa:bb:cc:dd:ee:ff 00:11:22:33:44:55]
7
+ INVALID_MACS = %w[invalid aabbccddeeffff aa:bb:cc:dd:ee:ff:11]
8
+ INVALID_MACS_STRICT = %w[aabbccddeeff aabbccddeeffxx aa:bb:cc:dd:ee:ff:xx 00:55:56:82:11::38 0011.2233.445566]
9
+
5
10
  def test_truth
6
11
  assert_kind_of Module, MacAddress
7
12
  end
@@ -10,21 +15,49 @@ class MacAddressTest < Test::Unit::TestCase
10
15
  assert_equal 'aabbccddeeff', 'AA-BB-CC-DD-EE-FF:hello world'.to_mac
11
16
  end
12
17
 
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]
18
+ def test_mac_strict
19
+ assert_equal 'aabbccddeeff', 'aa:bb:cc:dd:ee:ff'.to_mac(strict: true)
20
+ end
21
+
22
+ def test_error_raise_with_invalid_mac
23
+ assert_raise ArgumentError do
24
+ MacAddress.new(INVALID_MACS.first.to_mac)
25
+ end
26
+ end
16
27
 
17
- def test_valid_mac?
28
+ def test_error_raise_with_invalid_mac_strict
29
+ assert_raise ArgumentError do
30
+ MacAddress.new(INVALID_MACS_STRICT.first.to_mac(strict: true))
31
+ end
32
+ end
33
+
34
+ def test_error_raise_with_invalid_mac_for_string
35
+ assert_raise ArgumentError do
36
+ INVALID_MACS.first.to_mac
37
+ end
38
+ end
39
+
40
+ def test_error_raise_with_invalid_mac_strict_for_string
41
+ assert_raise ArgumentError do
42
+ INVALID_MACS_STRICT.first.to_mac(strict: true)
43
+ end
44
+ end
45
+
46
+ def test_valid_mac
18
47
  VALID_MACS.each do |valid_mac|
19
48
  assert_equal valid_mac.valid_mac?, true
20
49
  assert_equal valid_mac.valid_mac?(strict: true), true
21
50
  end
51
+ end
22
52
 
53
+ def test_invalid_macs
23
54
  INVALID_MACS.each do |invalid_mac|
24
55
  assert_equal invalid_mac.valid_mac?, false
25
56
  assert_equal invalid_mac.valid_mac?(strict: true), false
26
57
  end
58
+ end
27
59
 
60
+ def test_invalid_macs_strict
28
61
  INVALID_MACS_STRICT.each do |invalid_mac|
29
62
  assert_equal invalid_mac.valid_mac?(strict: true), false
30
63
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mac_address
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Doug Wiegley