Class: Predicates::Url
Overview
Defines a field as a URL.
Options
:domains [array, default nil] - a whitelist of allowed domains (e.g. ['com', 'net', 'org']). set to nil to allow all domains.
:schemes [array, default ['http', 'https']] - a whitelist of allowed schemes. set to nil to allow all schemes.
:ports [array, default nil] - a whitelist of allowed ports. set to nil to allow all ports.
:allow_ip_address [boolean, default true] - whether to allow ip addresses instead to domain names.
:implied_scheme [string, symbol, default 'http'] - what scheme to assume if none is present.
Examples
# if you need an ftp url
field_is_an_url :schemes => ['ftp']
# if you want to require https
field_is_an_url :schemes => ['https'], :implied_scheme => 'https', :ports => [443]
Instance Attribute Summary collapse
-
#allow_ip_address ⇒ Object
Returns the value of attribute allow_ip_address.
-
#domains ⇒ Object
Returns the value of attribute domains.
-
#implied_scheme ⇒ Object
Returns the value of attribute implied_scheme.
-
#ports ⇒ Object
Returns the value of attribute ports.
-
#schemes ⇒ Object
Returns the value of attribute schemes.
Attributes inherited from Base
#full_message, #or_empty, #validate_if, #validate_on
Instance Method Summary collapse
- #error_message ⇒ Object
-
#initialize(attr, options = {}) ⇒ Url
constructor
A new instance of Url.
- #normalize(v) ⇒ Object
- #validate(value, record) ⇒ Object
Methods inherited from Base
#allow_empty?, #error, #error_binds, #to_human
Constructor Details
#initialize(attr, options = {}) ⇒ Url
Returns a new instance of Url.
25 26 27 28 29 30 31 32 33 |
# File 'lib/predicates/url.rb', line 25 def initialize(attr, = {}) defaults = { :allow_ip_address => true, :schemes => ['http', 'https'], :implied_scheme => 'http' } super attr, defaults.merge() end |
Instance Attribute Details
#allow_ip_address ⇒ Object
Returns the value of attribute allow_ip_address.
20 21 22 |
# File 'lib/predicates/url.rb', line 20 def allow_ip_address @allow_ip_address end |
#domains ⇒ Object
Returns the value of attribute domains.
19 20 21 |
# File 'lib/predicates/url.rb', line 19 def domains @domains end |
#implied_scheme ⇒ Object
Returns the value of attribute implied_scheme.
23 24 25 |
# File 'lib/predicates/url.rb', line 23 def implied_scheme @implied_scheme end |
#ports ⇒ Object
Returns the value of attribute ports.
22 23 24 |
# File 'lib/predicates/url.rb', line 22 def ports @ports end |
#schemes ⇒ Object
Returns the value of attribute schemes.
21 22 23 |
# File 'lib/predicates/url.rb', line 21 def schemes @schemes end |
Instance Method Details
#error_message ⇒ Object
35 36 37 |
# File 'lib/predicates/url.rb', line 35 def @error_message || :url end |
#normalize(v) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/predicates/url.rb', line 55 def normalize(v) url = URI.parse(v) url = URI.parse("#{self.implied_scheme}://#{v}") if self.implied_scheme and not (url.scheme and url.host) url.to_s rescue URI::InvalidURIError, URI::InvalidComponentError v end |
#validate(value, record) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/predicates/url.rb', line 39 def validate(value, record) url = URI.parse(value) tld = (url.host && url.host.match(/\..+\Z/)) ? url.host.split('.').last : nil valid = true valid &&= (!tld.blank?) valid &&= (!self.schemes or self.schemes.include? url.scheme) valid &&= (!self.domains or self.domains.include? tld) valid &&= (!self.ports or self.ports.include? url.port) valid &&= (self.allow_ip_address or not url.host =~ /^([0-9]{1,3}\.){3}[0-9]{1,3}$/) valid rescue URI::InvalidURIError, URI::InvalidComponentError false end |