Page Menu
Home
GRNET
Search
Configure Global Search
Log In
Files
F448956
ipv4_validator.rb
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Thu, Apr 24, 12:18 PM
Size
562 B
Mime Type
text/x-ruby
Expires
Sat, Apr 26, 12:18 PM (1 d, 13 h)
Engine
blob
Format
Raw Data
Handle
212638
Attached To
rWEBDNS WebDNS (edet4)
ipv4_validator.rb
View Options
require
'ipaddr'
require
'socket'
class
Ipv4Validator
<
ActiveModel
::
EachValidator
# Returns true if addr is a valid IPv4 address.
def
valid_v4?
(
addr
)
return
false
if
addr
[
'/'
]
# IPAddr accepts addr/mask format
IPAddr
.
new
(
addr
,
Socket
::
AF_INET
)
true
rescue
IPAddr
::
AddressFamilyError
,
IPAddr
::
InvalidAddressError
false
end
# Add an attribute error if this is not a valid IPv4 address.
def
validate_each
(
record
,
attribute
,
value
)
return
if
valid_v4?
(
value
)
record
.
errors
[
attribute
]
<<
'is not a valid IPv4 address'
end
end
Event Timeline
Log In to Comment