Class: BinStruct::OUI

Inherits:
Struct
  • Object
show all
Includes:
Structable
Defined in:
lib/bin_struct/oui.rb

Overview

OUI type, defined as a set of 3 bytes

oui = OUI.new
oui.from_human('00:01:02')
oui.to_human   # => "00:01:02"
oui.to_s       # => "\x00\x01\x03"

Author:

  • Sylvain Daubert (2016-2024)

  • LemonTree55

Constant Summary

Constants inherited from Struct

Struct::FMT_ATTR

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Structable

#format_inspect, #read, #sz, #to_s, #type_name

Methods inherited from Struct

#[], #[]=, attributes, #attributes, #bits_on, define_attr, define_attr_after, define_attr_before, define_bit_attrs_on, inherited, #initialize, #inspect, #offset_of, #optional?, #optional_attributes, #present?, #read, remove_attr, remove_bit_attrs_on, #sz, #to_h, #to_s, update_attr

Constructor Details

This class inherits a constructor from BinStruct::Struct

Instance Attribute Details

#b0Integer

Returns right-most byte.

Returns:

  • (Integer)

    right-most byte



28
# File 'lib/bin_struct/oui.rb', line 28

define_attr :b0, Int8

#b1Integer

Returns center byte.

Returns:

  • (Integer)

    center byte



25
# File 'lib/bin_struct/oui.rb', line 25

define_attr :b1, Int8

#b2Integer

Returns left-most byte.

Returns:

  • (Integer)

    left-most byte



22
# File 'lib/bin_struct/oui.rb', line 22

define_attr :b2, Int8

Instance Method Details

#from_human(str) ⇒ self

Read a human-readable string to populate object

Parameters:

  • str (::String)

Returns:

  • (self)

Raises:

  • (ArgumentError)

    OUI cannot be recognized from str



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/bin_struct/oui.rb', line 34

def from_human(str)
  return self if str.nil?

  bytes = str.split(':')
  raise ArgumentError, 'not a OUI' unless bytes.size == 3

  self[:b2].from_human(bytes[0].to_i(16))
  self[:b1].from_human(bytes[1].to_i(16))
  self[:b0].from_human(bytes[2].to_i(16))
  self
end

#to_human::String

Get OUI in human readable form (colon-separated bytes)

Returns:

  • (::String)


48
49
50
# File 'lib/bin_struct/oui.rb', line 48

def to_human
  attributes.map { |m| '%02x' % self[m] }.join(':')
end