canonical 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/addressable/uri/canon.rb +47 -0
  3. metadata +73 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 964fc2b3c1c154a28dcdab2fe3fc772b2f007226
4
+ data.tar.gz: d3ca1b0d4814cf1738c17eb775b8bdd764fd1205
5
+ SHA512:
6
+ metadata.gz: 9c0469c9e35812402ce6fa1328772c12d0f61e6f293504134811ee161a6cf40325c2a6fd6fca5236fce5493254712cc80f3ffbb7e1ef3e9f60a2d84f19aa7659
7
+ data.tar.gz: 73b387e362bf90adc455927617bb89da712c294364f7418e84477f5690c4d8dce4aab869345395793d061b7c9b266f119a36c9e27a2388bd4e33d98469fa347b
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+ require 'addressable/uri/fetch'
3
+ require 'nokogiri'
4
+
5
+ module Addressable
6
+ class URI
7
+ def canonical
8
+ dup.canonical!
9
+ end
10
+
11
+ def canonical!
12
+ fixup!
13
+
14
+ response = fetch
15
+ response.uri ||= dup
16
+ response.uri.normalize!
17
+
18
+ return replace_self(response.uri) if !response.is_a?(Net::HTTPOK) && response.content_type != 'text/html'
19
+
20
+ document = Nokogiri.HTML(response.body)
21
+ # <link rel="canonical"> takes priority over <meta property="og:url">
22
+ replace_self(
23
+ if (element = document.at_css('link[@rel="canonical"]'))
24
+ self.class.parse(element['href'])
25
+ elsif (element = document.at_css('meta[@property="og:url"]'))
26
+ self.class.parse(element['content'])
27
+ else
28
+ response.uri
29
+ end
30
+ )
31
+
32
+ # The spec allows the canonical link to be a relative URI
33
+ replace_self(self.class.join(response.uri, self)) unless absolute?
34
+
35
+ # Because you can't always trust sites to do things properly
36
+ normalize!
37
+ end
38
+
39
+ def fixup
40
+ dup.fixup!
41
+ end
42
+
43
+ def fixup!
44
+ normalize!
45
+ end
46
+ end
47
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: canonical
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - DragonMaus
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: fetchable-uri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ description:
42
+ email:
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/addressable/uri/canon.rb
48
+ homepage: https://github.com/dragonmaus/canonical
49
+ licenses:
50
+ - MIT
51
+ metadata: {}
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 2.5.1
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: URI canonicalization extension for Addressable::URI
72
+ test_files: []
73
+ has_rdoc: