Module: Sisimai::RFC791

Defined in:
lib/sisimai/rfc791.rb

Overview

Sisimai::RFC791 is a class related to the Internet host

Class Method Summary collapse

Class Method Details

.find(argv0) ⇒ Array

Find an IPv4 address from the given string

Parameters:

  • argv1 (String)

    String including an IPv4 address

Returns:

  • (Array)

    List of IPv4 addresses

Since:

  • v5.0.0



25
26
27
28
29
30
31
# File 'lib/sisimai/rfc791.rb', line 25

def find(argv0)
  return nil if argv0.to_s.empty?
  return []  if argv0.size < 7

  given = argv0.dup.tr('()[],', ' ').squeeze(' ')
  return given.split(' ').select { |e| is_ipv4address(e) }
end

.is_ipv4address(argv0) ⇒ Bool

Returns 1 if the argument is an IPv4 address

Parameters:

  • argv1 (String)

    IPv4 address like “192.0.2.25”

Returns:

  • (Bool)

    1: is an IPv4 address

Since:

  • v5.2.0



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/sisimai/rfc791.rb', line 9

def is_ipv4address(argv0)
  return false if argv0.nil? || argv0.size < 7
  octet = argv0.split(/[.]/); return false if octet.size != 4

  octet.each do |e|
    # Check each octet is between 0 and 255
    return false unless e =~ /\A[0-9]{1,3}\z/; v = e.to_i
    return false if v < 0 || v > 255
  end
  return true
end