Class: Addressable::URI

Inherits:
Object
  • Object
show all
Defined in:
lib/addressable/uri/canon.rb

Instance Method Summary collapse

Instance Method Details

#canonicalObject



7
8
9
# File 'lib/addressable/uri/canon.rb', line 7

def canonical
  dup.canonical!
end

#canonical!Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/addressable/uri/canon.rb', line 11

def canonical!
  fixup!

  response = fetch
  response.uri ||= dup
  response.uri.normalize!

  return replace_self(response.uri) if !response.is_a?(Net::HTTPOK) && response.content_type != 'text/html'

  document = Nokogiri.HTML(response.body)
  # <link rel="canonical"> takes priority over <meta property="og:url">
  replace_self(
    if (element = document.at_css('link[@rel="canonical"]'))
      self.class.parse(element['href'])
    elsif (element = document.at_css('meta[@property="og:url"]'))
      self.class.parse(element['content'])
    else
      response.uri
    end
  )

  # The spec allows the canonical link to be a relative URI
  replace_self(self.class.join(response.uri, self)) unless absolute?

  # Because you can't always trust sites to do things properly
  normalize!
end

#fixupObject



39
40
41
# File 'lib/addressable/uri/canon.rb', line 39

def fixup
  dup.fixup!
end

#fixup!Object



43
44
45
# File 'lib/addressable/uri/canon.rb', line 43

def fixup!
  normalize!
end