Module: IPArith

Defined in:
lib/iparith.rb,
lib/iparith/parse.rb

Class Method Summary collapse

Class Method Details

.addr_to_int(str) ⇒ Object



7
8
9
10
11
12
# File 'lib/iparith.rb', line 7

def self.addr_to_int(str)
  addr = ::IPAddr.new str
  addr.to_i
rescue ::IPAddr::InvalidAddressError, ::IPAddr::AddressFamilyError
  nil
end

.ipstr_valid?(ipstr) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
# File 'lib/iparith/parse.rb', line 26

def self.ipstr_valid?(ipstr)
  ::IPAddr.new ipstr
  true
rescue ::IPAddr::InvalidAddressError, ::IPAddr::AddressFamilyError
  nil
end

.macstr_valid?(macstr) ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
6
7
8
9
10
# File 'lib/iparith/parse.rb', line 3

def self.macstr_valid?(macstr)
  return nil unless macstr.is_a? String

  rg_raw = /\A([a-f0-9]{12})\z/
  mdata = rg_raw.match(macstr)

  mdata ? true : nil
end

.parse_macstr(mac_str) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/iparith/parse.rb', line 12

def self.parse_macstr(mac_str)
  return nil unless mac_str.is_a? String

  cleaned_str = mac_str.downcase
  cleaned_str.gsub!(/[-:.]/, '')
  cleaned_str.gsub!('с', 'c') # replace russian c

  rg_raw = /\A([a-f0-9]{12})\z/
  mdata = rg_raw.match(cleaned_str)
  return mdata[1] if mdata

  nil
end