Class: VacuumCleaner::Normalizations::UrlNormalizer
- Inherits:
-
VacuumCleaner::Normalizer
- Object
- VacuumCleaner::Normalizer
- VacuumCleaner::Normalizations::UrlNormalizer
- Defined in:
- lib/vacuum_cleaner/normalizations/url.rb
Overview
Normalizer which is used to prefix strings with a scheme, if missing. This is useful to ensure, that an input field always has e.g. the “http://” scheme added. Please note, that this normalizer does not validate a URL in any way.
normalizes :homepage, :url => true
Accepts a string as input, so to normalize for instance FTP URLs.
normalizes :download_url, :url => "ftp://"
To make further customizations, the constructor accepts a hash.
normalizes, :contact_url, :url => { :scheme => "http://",
:unless => %r{\A(https?://|xmpp:|gtalk:|mailto:)} }
The key :scheme is always used as the prefix, when the input does not a match the regex in :unless.
Instance Attribute Summary
Attributes inherited from VacuumCleaner::Normalizer
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ UrlNormalizer
constructor
Accepts either a hash or a string.
-
#normalize_value(value) ⇒ Object
Prefixes input with
options[:scheme]if it doesn’t matchesoptions[:unless].
Methods inherited from VacuumCleaner::Normalizer
Constructor Details
#initialize(options = {}) ⇒ UrlNormalizer
Accepts either a hash or a string.
25 26 27 28 29 |
# File 'lib/vacuum_cleaner/normalizations/url.rb', line 25 def initialize( = {}) = { :scheme => "http://", :unless => %r{\Ahttps?://}i } if .nil? || .empty? = { :scheme => , :unless => %r{\A#{options}}i } unless .is_a?(Hash) super() end |
Instance Method Details
#normalize_value(value) ⇒ Object
Prefixes input with options[:scheme] if it doesn’t matches options[:unless].
33 34 35 36 37 |
# File 'lib/vacuum_cleaner/normalizations/url.rb', line 33 def normalize_value(value) value = super # just ensure that default stripping/cleaning is done already return nil if value == [:scheme] value =~ [:unless] ? value : "#{options[:scheme]}#{value}" unless value.nil? end |