zhulei-canonical-rails 0.2.4 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- 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: d8fa34924f6d0b05caad59dfddb9a5e92b3fc112
|
4
|
+
data.tar.gz: ac6632b6ede8ae189931afdfba57f158c79445eb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b0b21472b360672d223af326bce3b810bd0a17de5a2ca82ada770b5704cb50f1468387e3a10bf9d5ec5308697adc85b14086990d7337e850fb020ed092470f61
|
7
|
+
data.tar.gz: be9530a917c98de4f8126f83cf76d0d2a3a7ca3fdac908e794065f7ed6c3827ab65fdf1568c7ac0c1de665211d496784f5c68b3bf00708dccc3edef0b5337f95
|
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 ZhuleiCanonicalRails
|
2
|
+
module TagHelper
|
3
|
+
def trailing_slash_needed?
|
4
|
+
request.params.key?('action') && ZhuleiCanonicalRails.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
|
+
ZhuleiCanonicalRails.protocol || request.protocol
|
17
|
+
end
|
18
|
+
|
19
|
+
def canonical_host
|
20
|
+
ZhuleiCanonicalRails.host || request.host
|
21
|
+
end
|
22
|
+
|
23
|
+
def canonical_port
|
24
|
+
(ZhuleiCanonicalRails.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 ZhuleiCanonicalRails.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? && ZhuleiCanonicalRails.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 ZhuleiCanonicalRails
|
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, ZhuleiCanonicalRails::TagHelper
|
6
6
|
end
|
7
7
|
|
8
8
|
end
|
@@ -1,3 +1,3 @@
|
|
1
|
-
module
|
2
|
-
VERSION = "0.
|
1
|
+
module ZhuleiCanonicalRails
|
2
|
+
VERSION = "1.0.0"
|
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 ZhuleiCanonicalRails
|
4
4
|
|
5
|
-
# Default way to setup
|
5
|
+
# Default way to setup ZhuleiCanonicalRails. 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: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- Zhu Lei
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-09-
|
11
|
+
date: 2018-09-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -86,11 +86,10 @@ dependencies:
|
|
86
86
|
- - ">="
|
87
87
|
- !ruby/object:Gem::Version
|
88
88
|
version: '0'
|
89
|
-
description:
|
90
|
-
|
91
|
-
with arbitrary parameters, trailing slashes. '
|
89
|
+
description: This gem is based on canonical-rails by Denis Ivanov,I only delete the
|
90
|
+
port in the url and delete the lower-case method in the code.
|
92
91
|
email:
|
93
|
-
-
|
92
|
+
- zhuleichina@qq.com
|
94
93
|
executables: []
|
95
94
|
extensions: []
|
96
95
|
extra_rdoc_files: []
|
@@ -125,8 +124,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
125
124
|
version: '0'
|
126
125
|
requirements: []
|
127
126
|
rubyforge_project:
|
128
|
-
rubygems_version: 2.
|
127
|
+
rubygems_version: 2.6.14
|
129
128
|
signing_key:
|
130
129
|
specification_version: 4
|
131
|
-
summary:
|
130
|
+
summary: canonical-rails by Denis Ivanov,delete the port in the url and delete the
|
131
|
+
lower-case method in the code
|
132
132
|
test_files: []
|