Class: MetaInspector::URL

Inherits:
Object
  • Object
show all
Defined in:
lib/meta_inspector/url.rb

Constant Summary collapse

WELL_KNOWN_TRACKING_PARAMS =
%w( utm_source utm_medium utm_term utm_content utm_campaign )

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial_url, options = {}) ⇒ URL

Returns a new instance of URL.


7
8
9
10
11
12
13
# File 'lib/meta_inspector/url.rb', line 7

def initialize(initial_url, options = {})
  options        = self.class.defaults.merge(options)

  @normalize     = options[:normalize]

  self.url = initial_url
end

Instance Attribute Details

#urlObject

Returns the value of attribute url


5
6
7
# File 'lib/meta_inspector/url.rb', line 5

def url
  @url
end

Class Method Details

.absolutify(url, base_url, options = {}) ⇒ Object

Converts a relative URL to an absolute URL, like:

"/faq" => "http://example.com/faq"

Respecting already absolute URLs like the ones starting with

http:, ftp:, telnet:, mailto:, javascript: ...

Protocol-relative URLs are also resolved to use the same schema as the base_url


59
60
61
62
63
64
65
66
67
68
69
# File 'lib/meta_inspector/url.rb', line 59

def self.absolutify(url, base_url, options = {})
  options = defaults.merge(options)
  if url =~ /^\w*\:/i
    MetaInspector::URL.new(url, options).url
  else
    uri = Addressable::URI.join(base_url, url)
    options[:normalize] ? uri.normalize.to_s : uri.to_s
  end
rescue MetaInspector::ParserError, Addressable::URI::InvalidURIError, ArgumentError
  nil
end

Instance Method Details

#hostObject


19
20
21
# File 'lib/meta_inspector/url.rb', line 19

def host
  parsed(url) ? parsed(url).host : nil
end

#root_urlObject


23
24
25
# File 'lib/meta_inspector/url.rb', line 23

def root_url
  "#{scheme}://#{host}/"
end

#schemeObject


15
16
17
# File 'lib/meta_inspector/url.rb', line 15

def scheme
  parsed(url) ? parsed(url).scheme : nil
end

#tracked?Boolean

Returns:

  • (Boolean)

29
30
31
32
33
34
# File 'lib/meta_inspector/url.rb', line 29

def tracked?
  u = parsed(url)
  return false unless u.query_values
  found_tracking_params = WELL_KNOWN_TRACKING_PARAMS & u.query_values.keys
  return found_tracking_params.any?
end

#untrack!Object


44
45
46
# File 'lib/meta_inspector/url.rb', line 44

def untrack!
  self.url = untracked_url if tracked?
end

#untracked_urlObject


36
37
38
39
40
41
42
# File 'lib/meta_inspector/url.rb', line 36

def untracked_url
  u = parsed(url)
  return url unless u.query_values
  query_values = u.query_values.delete_if { |key, _| WELL_KNOWN_TRACKING_PARAMS.include? key }
  u.query_values = query_values.length > 0 ? query_values : nil
  u.to_s
end