Class: VacuumCleaner::Normalizations::UrlNormalizer

Inherits:
VacuumCleaner::Normalizer show all
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

#options

Instance Method Summary collapse

Methods inherited from VacuumCleaner::Normalizer

#normalize

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(options = {})
  options = { :scheme => "http://", :unless => %r{\Ahttps?://}i } if options.nil? || options.empty?
  options = { :scheme => options, :unless => %r{\A#{options}}i } unless options.is_a?(Hash)
  super(options)
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 == options[:scheme]
  value =~ options[:unless] ? value : "#{options[:scheme]}#{value}" unless value.nil?
end