Class: MacAddress
- Inherits:
-
Object
- Object
- MacAddress
- Defined in:
- lib/mac_address.rb,
lib/mac_address/version.rb
Constant Summary collapse
- VERSION =
"0.0.4"
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(str, options = {}) ⇒ MacAddress
constructor
A new instance of MacAddress.
- #to_i ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(str, options = {}) ⇒ MacAddress
Returns a new instance of MacAddress.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/mac_address.rb', line 5 def initialize(str, = {}) @mac_str = str.strip.dequote.strip if [:strict] && !MacAddress.validate_strict(@mac_str) raise_argument_error(@mac_str) end n = @mac_str.index(':') if not n.nil? and n >= 12 @mac_str = @mac_str.split(':')[0] end @mac_str = @mac_str.downcase.gsub(/^0[xX]/,'').gsub(/[^0-9a-f]/,'') raise ArgumentError.new("Invalid MAC address: #{str}") if @mac_str.length != 12 end |
Class Method Details
.validate(mac, options = {}) ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/mac_address.rb', line 33 def self.validate(mac, = {}) if [:strict] MacAddress.validate_strict(mac) else MacAddress.new(mac).to_s true end rescue ArgumentError false end |
.validate_strict(mac) ⇒ Object
29 30 31 |
# File 'lib/mac_address.rb', line 29 def self.validate_strict(mac) !!(mac =~ /^([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}$/i) end |
Instance Method Details
#to_i ⇒ Object
25 26 27 |
# File 'lib/mac_address.rb', line 25 def to_i @mac_str.hex end |
#to_s ⇒ Object
21 22 23 |
# File 'lib/mac_address.rb', line 21 def to_s @mac_str end |