zhulei-canonical-rails 0.1.1 → 0.2.4
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.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/Rakefile +1 -1
- data/app/helpers/canonical_rails/tag_helper.rb +77 -77
- data/lib/canonical-rails/engine.rb +2 -2
- data/lib/canonical-rails/version.rb +2 -2
- data/lib/canonical-rails.rb +2 -2
- data/lib/generators/canonical_rails/install/install_generator.rb +1 -1
- data/lib/generators/canonical_rails/install/templates/canonical_rails.rb +1 -1
- metadata +9 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: acc681c37ea0f9af61bec19afb7deda532ea20c4
|
4
|
+
data.tar.gz: de43c2f57a89718d3700a810c6a1b6bd7110121c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e684c3090e13c3240032fe2b6bc3c278f6a2fe781a1339b29fc1cb082ff1fb127543e2fbb4fae6e6f6c7113385e8414c3671cc5d1b46135593549043f5bd2ca4
|
7
|
+
data.tar.gz: 8711add62a7b3bdfe6e0bd1580b368bd21ab425718b582cc90b67497a31c3e9b32af82a95acc55ac74791bc58e9972160ed30b1add62f7f4d3225dce01a14a3e
|
data/README.md
CHANGED
data/Rakefile
CHANGED
@@ -1,77 +1,77 @@
|
|
1
|
-
module
|
2
|
-
module TagHelper
|
3
|
-
def trailing_slash_needed?
|
4
|
-
request.params.key?('action') &&
|
5
|
-
end
|
6
|
-
|
7
|
-
def trailing_slash_if_needed
|
8
|
-
"/" if trailing_slash_needed? && request.path != '/'
|
9
|
-
end
|
10
|
-
|
11
|
-
def path_without_html_extension
|
12
|
-
request.path.sub(/\.html$/, '')
|
13
|
-
end
|
14
|
-
|
15
|
-
def canonical_protocol
|
16
|
-
|
17
|
-
end
|
18
|
-
|
19
|
-
def canonical_host
|
20
|
-
|
21
|
-
end
|
22
|
-
|
23
|
-
def canonical_port
|
24
|
-
(
|
25
|
-
end
|
26
|
-
|
27
|
-
def canonical_href(host = canonical_host, port = canonical_port)
|
28
|
-
default_ports = { 'https://' => 443, 'http://' => 80 }
|
29
|
-
port = port.present? && port.to_i != default_ports[canonical_protocol] ? ":#{port}" : ''
|
30
|
-
raw "#{canonical_protocol}#{host}#{path_without_html_extension}#{whitelisted_query_string}"
|
31
|
-
end
|
32
|
-
|
33
|
-
def canonical_path
|
34
|
-
raw "#{path_without_html_extension}#{trailing_slash_if_needed}#{whitelisted_query_string}"
|
35
|
-
end
|
36
|
-
|
37
|
-
def canonical_tag(host = canonical_host, port = canonical_port)
|
38
|
-
canonical_url = canonical_href(host, port)
|
39
|
-
capture do
|
40
|
-
if
|
41
|
-
concat tag(:meta, property: 'og:url', content: canonical_url)
|
42
|
-
end
|
43
|
-
concat tag(:link, href: canonical_url, rel: :canonical)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
def whitelisted_params
|
48
|
-
selected_params = params.select do |key, value|
|
49
|
-
value.present? &&
|
50
|
-
end
|
51
|
-
|
52
|
-
selected_params.respond_to?(:to_unsafe_h) ? selected_params.to_unsafe_h : selected_params.to_h
|
53
|
-
end
|
54
|
-
|
55
|
-
def whitelisted_query_string
|
56
|
-
# Rack 1.4.5 fails to handle params that are not strings
|
57
|
-
# So if
|
58
|
-
# my_hash = { "a" => 1, "b" => 2}
|
59
|
-
# Rack::Utils.build_nested_query(my_hash) would return
|
60
|
-
# "a&b"
|
61
|
-
# Rack 1.4.5 did not have a test case for this scenario
|
62
|
-
# https://github.com/rack/rack/blob/9939d40a5e23dcb058751d1029b794aa2f551900/test/spec_utils.rb#L222
|
63
|
-
# Rack 1.6.0 has it
|
64
|
-
# https://github.com/rack/rack/blob/65a7104b6b3e9ecd8f33c63a478ab9a33a103507/test/spec_utils.rb#L251
|
65
|
-
|
66
|
-
wl_params = whitelisted_params
|
67
|
-
|
68
|
-
"?" + Rack::Utils.build_nested_query(convert_numeric_params(wl_params)) if wl_params.present?
|
69
|
-
end
|
70
|
-
|
71
|
-
private
|
72
|
-
|
73
|
-
def convert_numeric_params(params_hash)
|
74
|
-
Hash[params_hash.map { |k, v| v.is_a?(Numeric) ? [k, v.to_s] : [k, v] }]
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
1
|
+
module CanonicalRails
|
2
|
+
module TagHelper
|
3
|
+
def trailing_slash_needed?
|
4
|
+
request.params.key?('action') && CanonicalRails.sym_collection_actions.include?(request.params['action'].to_sym)
|
5
|
+
end
|
6
|
+
|
7
|
+
def trailing_slash_if_needed
|
8
|
+
"/" if trailing_slash_needed? && request.path != '/'
|
9
|
+
end
|
10
|
+
|
11
|
+
def path_without_html_extension
|
12
|
+
request.path.sub(/\.html$/, '')
|
13
|
+
end
|
14
|
+
|
15
|
+
def canonical_protocol
|
16
|
+
CanonicalRails.protocol || request.protocol
|
17
|
+
end
|
18
|
+
|
19
|
+
def canonical_host
|
20
|
+
CanonicalRails.host || request.host
|
21
|
+
end
|
22
|
+
|
23
|
+
def canonical_port
|
24
|
+
(CanonicalRails.port || request.port).to_i
|
25
|
+
end
|
26
|
+
|
27
|
+
def canonical_href(host = canonical_host, port = canonical_port)
|
28
|
+
default_ports = { 'https://' => 443, 'http://' => 80 }
|
29
|
+
port = port.present? && port.to_i != default_ports[canonical_protocol] ? ":#{port}" : ''
|
30
|
+
raw "#{canonical_protocol}#{host}#{path_without_html_extension}#{whitelisted_query_string}"
|
31
|
+
end
|
32
|
+
|
33
|
+
def canonical_path
|
34
|
+
raw "#{path_without_html_extension}#{trailing_slash_if_needed}#{whitelisted_query_string}"
|
35
|
+
end
|
36
|
+
|
37
|
+
def canonical_tag(host = canonical_host, port = canonical_port)
|
38
|
+
canonical_url = canonical_href(host, port)
|
39
|
+
capture do
|
40
|
+
if CanonicalRails.opengraph_url
|
41
|
+
concat tag(:meta, property: 'og:url', content: canonical_url)
|
42
|
+
end
|
43
|
+
concat tag(:link, href: canonical_url, rel: :canonical)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def whitelisted_params
|
48
|
+
selected_params = params.select do |key, value|
|
49
|
+
value.present? && CanonicalRails.sym_whitelisted_parameters.include?(key.to_sym)
|
50
|
+
end
|
51
|
+
|
52
|
+
selected_params.respond_to?(:to_unsafe_h) ? selected_params.to_unsafe_h : selected_params.to_h
|
53
|
+
end
|
54
|
+
|
55
|
+
def whitelisted_query_string
|
56
|
+
# Rack 1.4.5 fails to handle params that are not strings
|
57
|
+
# So if
|
58
|
+
# my_hash = { "a" => 1, "b" => 2}
|
59
|
+
# Rack::Utils.build_nested_query(my_hash) would return
|
60
|
+
# "a&b"
|
61
|
+
# Rack 1.4.5 did not have a test case for this scenario
|
62
|
+
# https://github.com/rack/rack/blob/9939d40a5e23dcb058751d1029b794aa2f551900/test/spec_utils.rb#L222
|
63
|
+
# Rack 1.6.0 has it
|
64
|
+
# https://github.com/rack/rack/blob/65a7104b6b3e9ecd8f33c63a478ab9a33a103507/test/spec_utils.rb#L251
|
65
|
+
|
66
|
+
wl_params = whitelisted_params
|
67
|
+
|
68
|
+
"?" + Rack::Utils.build_nested_query(convert_numeric_params(wl_params)) if wl_params.present?
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
def convert_numeric_params(params_hash)
|
74
|
+
Hash[params_hash.map { |k, v| v.is_a?(Numeric) ? [k, v.to_s] : [k, v] }]
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -1,8 +1,8 @@
|
|
1
|
-
module
|
1
|
+
module CanonicalRails
|
2
2
|
class Engine < ::Rails::Engine
|
3
3
|
|
4
4
|
initializer 'canonical_rails.add_helpers' do |app|
|
5
|
-
ActionView::Base.send :include,
|
5
|
+
ActionView::Base.send :include, CanonicalRails::TagHelper
|
6
6
|
end
|
7
7
|
|
8
8
|
end
|
@@ -1,3 +1,3 @@
|
|
1
|
-
module
|
2
|
-
VERSION = "0.
|
1
|
+
module CanonicalRails
|
2
|
+
VERSION = "0.2.4"
|
3
3
|
end
|
data/lib/canonical-rails.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
require "canonical-rails/engine"
|
2
2
|
|
3
|
-
module
|
3
|
+
module CanonicalRails
|
4
4
|
|
5
|
-
# Default way to setup
|
5
|
+
# Default way to setup CanonicalRails. Run `rails g canonical_rails:install` to create
|
6
6
|
# a fresh initializer with all configuration values.
|
7
7
|
#
|
8
8
|
# the config\setup concept politely observed at and borrowed from Devise: https://github.com/plataformatec/devise/blob/master/lib/devise.rb
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zhulei-canonical-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- Denis Ivanov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-09-
|
11
|
+
date: 2018-09-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -86,10 +86,11 @@ dependencies:
|
|
86
86
|
- - ">="
|
87
87
|
- !ruby/object:Gem::Version
|
88
88
|
version: '0'
|
89
|
-
description:
|
90
|
-
|
89
|
+
description: 'Configurable, but assumes a conservative strategy by default with a
|
90
|
+
goal to solve many search engine index problems: multiple hostnames, inbound links
|
91
|
+
with arbitrary parameters, trailing slashes. '
|
91
92
|
email:
|
92
|
-
-
|
93
|
+
- visible@jumph4x.net
|
93
94
|
executables: []
|
94
95
|
extensions: []
|
95
96
|
extra_rdoc_files: []
|
@@ -124,9 +125,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
124
125
|
version: '0'
|
125
126
|
requirements: []
|
126
127
|
rubyforge_project:
|
127
|
-
rubygems_version: 2.
|
128
|
+
rubygems_version: 2.5.2
|
128
129
|
signing_key:
|
129
130
|
specification_version: 4
|
130
|
-
summary:
|
131
|
-
lower-case method in the code
|
131
|
+
summary: Simple and configurable Rails canonical ref tag helper
|
132
132
|
test_files: []
|