Class: NormalizeUrl::Normalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/normalize_url/normalizer.rb

Constant Summary collapse

TRACKING_QUERY_PARAMS =
%w[
  utm_source
  utm_medium
  utm_term
  utm_content
  utm_campaign
  sms_ss
  awesm
  xtor
  PHPSESSID
].to_set.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(original_uri, options = {}) ⇒ Normalizer

Returns a new instance of Normalizer.



20
21
22
23
24
25
26
27
# File 'lib/normalize_url/normalizer.rb', line 20

def initialize(original_uri, options={})
  @uri = Addressable::URI.parse(original_uri).normalize
  @options = options
  fail_uri "only absolute URLs can be normalized" unless uri.absolute?
  fail_uri "only HTTP/HTTPS URLs can be normalized" unless uri.scheme =~ /https?/
rescue Addressable::URI::InvalidURIError
  fail_uri "#{original_uri.inspect} is not a URL"
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/normalize_url/normalizer.rb', line 6

def options
  @options
end

#uriObject (readonly)

Returns the value of attribute uri.



6
7
8
# File 'lib/normalize_url/normalizer.rb', line 6

def uri
  @uri
end

Instance Method Details

#normalizeObject



29
30
31
32
33
34
35
# File 'lib/normalize_url/normalizer.rb', line 29

def normalize
  process :remove_trailing_slash
  process :remove_repeating_slashes
  process :remove_hash
  process_query
  uri.to_s
end