Class: Predicates::Domain

Inherits:
Base
  • Object
show all
Defined in:
lib/predicates/domain.rb

Overview

Defines a field as a simple domain (not URL).

Instance Attribute Summary

Attributes inherited from Base

#full_message, #or_empty, #validate_if, #validate_on

Instance Method Summary collapse

Methods inherited from Base

#allow_empty?, #error, #error_binds, #initialize, #to_human

Constructor Details

This class inherits a constructor from Predicates::Base

Instance Method Details

#error_messageObject



5
6
7
# File 'lib/predicates/domain.rb', line 5

def error_message
  @error_message || :domain
end

#normalize(v) ⇒ Object



20
21
22
23
24
# File 'lib/predicates/domain.rb', line 20

def normalize(v)
  URI.parse(with_protocol(v)).host || v
rescue URI::InvalidURIError
  v
end

#validate(value, record) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/predicates/domain.rb', line 9

def validate(value, record)
  url = URI.parse(with_protocol(value))
  valid = (url.host == value)
  valid &&= (value.match /\..+\Z/) # to catch "http://example" or similar
  valid &&= (!value.match /^([0-9]{1,3}\.){3}[0-9]{1,3}$/) # to catch ip addresses

  valid
rescue URI::InvalidURIError
  false
end