webtranslateit-canonical-rails 0.2.18

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9392738a9c762d39bbac1cf1b40715829b98b0130eca09d66ec664169179b7d7
4
+ data.tar.gz: e31be240b86b6b9bf3f5741cb42328c55c3ade167611a3c6d90d6ae3d2771230
5
+ SHA512:
6
+ metadata.gz: b79d1172ef7d6087b7e8b42c43cf804c21bfd47adc8de70105797bd9fcf6c942c7d825f2180fff694f1f4dad1cfbc278c90a74eee3a5d83a5111c8e2639624b7
7
+ data.tar.gz: 857b99423f534e8ce350058c9e191ba7ce95930d9cacfb391a05ecefbdb212254fd1fbb706c4494e2fdbc64796fa76a759b26b53578d457db9904c8faa47bd55
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 Denis Ivanov
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ CanonicalRails
2
+ ==============
3
+ [![latest builds](https://github.com/jumph4x/canonical-rails/actions/workflows/ruby.yml/badge.svg)](https://github.com/jumph4x/canonical-rails/actions/workflows/ruby.yml)
4
+
5
+
6
+
7
+ A number of articles exist explaining the issue concisely and at length:
8
+
9
+ * [Google Webmaster Blog Page About Specifying Canonical](http://googlewebmastercentral.blogspot.com/2009/02/specify-your-canonical.html)
10
+ * [Google Support About rel="canonical"](http://support.google.com/webmasters/bin/answer.py?hl=en&answer=139394)
11
+ * [Google Support About Canonicalization](http://support.google.com/webmasters/bin/answer.py?hl=en&answer=139066)
12
+
13
+ ## Guide
14
+
15
+ Take a look at this blog post that can guide you through the idea and the setup: [Easily add canonical URLs to your Rails app](http://blog.planetargon.com/entries/2014/4/4/easily-add-canonical-urls-to-your-rails-app)
16
+
17
+ ## Challenge
18
+
19
+ I've seen a lot of folks do more harm by neglecting canonicalization altogether than by applying too narrowly and conservatively, so here is an attempt to let people start modestly without spending too much time on it and whitelist parameters as they need to.
20
+
21
+ ## Install
22
+
23
+ gem 'canonical-rails', github: 'jumph4x/canonical-rails'
24
+
25
+ ## Usage
26
+
27
+ First, generate the config
28
+
29
+ rails g canonical_rails:install
30
+
31
+ Then find it in config/initializers/ as canonical_rails.rb
32
+
33
+ Finally, include the canonical_tag helper inside the `head` portion of
34
+ your HTML views:
35
+ ```ruby
36
+ <%= canonical_tag -%>
37
+ ```
38
+
39
+ ## Cred
40
+
41
+ Originally developed for [FCP Euro - High Quality European Car Parts](https://www.fcpeuro.com).
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'CanonicalRails'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ Bundler::GemHelper.install_tasks
24
+
25
+ require 'rspec/core/rake_task'
26
+
27
+ RSpec::Core::RakeTask.new(:spec)
28
+ task :default => :spec
@@ -0,0 +1,86 @@
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
+ # Leave force_trailing_slash as nil to get the original behavior of trailing_slash_if_needed
8
+ def trailing_slash_config(force_trailing_slash = nil)
9
+ if force_trailing_slash
10
+ "/"
11
+ elsif force_trailing_slash.nil?
12
+ trailing_slash_if_needed
13
+ end
14
+ end
15
+
16
+ def trailing_slash_if_needed
17
+ "/" if trailing_slash_needed?
18
+ end
19
+
20
+ def path_without_extension
21
+ return '' if request.path == '/'
22
+
23
+ request.path.sub(/\.\w{3,4}$/, '')
24
+ end
25
+
26
+ def canonical_protocol
27
+ CanonicalRails.protocol || request.protocol
28
+ end
29
+
30
+ def canonical_host
31
+ CanonicalRails.host || request.host
32
+ end
33
+
34
+ def canonical_port
35
+ (CanonicalRails.port || request.port).to_i
36
+ end
37
+
38
+ def canonical_href(host = canonical_host, port = canonical_port, force_trailing_slash = nil)
39
+ default_ports = { 'https://' => 443, 'http://' => 80 }
40
+ port = port.present? && port.to_i != default_ports[canonical_protocol] ? ":#{port}" : ''
41
+ raw "#{canonical_protocol}#{host}#{port}#{path_without_extension}#{trailing_slash_config(force_trailing_slash)}#{allowed_query_string}"
42
+ end
43
+
44
+ def canonical_path(force_trailing_slash = nil)
45
+ raw "#{path_without_extension}#{trailing_slash_config(force_trailing_slash)}#{allowed_query_string}"
46
+ end
47
+
48
+ def canonical_tag(host = canonical_host, port = canonical_port, force_trailing_slash = nil)
49
+ canonical_url = canonical_href(host, port, force_trailing_slash)
50
+ capture do
51
+ if CanonicalRails.opengraph_url
52
+ concat tag(:meta, property: 'og:url', content: canonical_url)
53
+ end
54
+ concat tag(:link, href: canonical_url, rel: :canonical)
55
+ end
56
+ end
57
+
58
+ def allowed_params
59
+ selected_params = params.select do |key, value|
60
+ value.present? && CanonicalRails.sym_allowed_parameters.include?(key.to_sym)
61
+ end
62
+
63
+ selected_params.respond_to?(:to_unsafe_h) ? selected_params.to_unsafe_h : selected_params.to_h
64
+ end
65
+
66
+ def allowed_query_string
67
+ # Rack 1.4.5 fails to handle params that are not strings
68
+ # So if
69
+ # my_hash = { "a" => 1, "b" => 2}
70
+ # Rack::Utils.build_nested_query(my_hash) would return
71
+ # "a&b"
72
+ # Rack 1.4.5 did not have a test case for this scenario
73
+ # https://github.com/rack/rack/blob/9939d40a5e23dcb058751d1029b794aa2f551900/test/spec_utils.rb#L222
74
+ # Rack 1.6.0 has it
75
+ # https://github.com/rack/rack/blob/65a7104b6b3e9ecd8f33c63a478ab9a33a103507/test/spec_utils.rb#L251
76
+ parameters = allowed_params
77
+ "?" + Rack::Utils.build_nested_query(convert_numeric_params(parameters)) if parameters.present?
78
+ end
79
+
80
+ private
81
+
82
+ def convert_numeric_params(params_hash)
83
+ Hash[params_hash.map { |k, v| v.is_a?(Numeric) ? [k, v.to_s] : [k, v] }]
84
+ end
85
+ end
86
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ Rails.application.routes.draw do
2
+
3
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/deprecation'
4
+
5
+ module CanonicalRails
6
+ Deprecation = ActiveSupport::Deprecation.new('1.0', 'CanonicalRails')
7
+ end
@@ -0,0 +1,11 @@
1
+ require_relative '../../app/helpers/canonical_rails/tag_helper'
2
+
3
+ module CanonicalRails
4
+ class Engine < ::Rails::Engine
5
+
6
+ initializer 'canonical_rails.add_helpers' do |app|
7
+ ActionView::Base.send :include, CanonicalRails::TagHelper
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module CanonicalRails
2
+ VERSION = "0.2.18"
3
+ end
@@ -0,0 +1,49 @@
1
+ require "canonical-rails/engine"
2
+ require "canonical-rails/deprecation"
3
+
4
+ module CanonicalRails
5
+
6
+ # Default way to setup CanonicalRails. Run `rails g canonical_rails:install` to create
7
+ # a fresh initializer with all configuration values.
8
+ #
9
+ # the config\setup concept politely observed at and borrowed from Devise: https://github.com/plataformatec/devise/blob/master/lib/devise.rb
10
+
11
+ def self.setup
12
+ yield self
13
+ end
14
+
15
+ mattr_accessor :host
16
+ @@host = nil
17
+
18
+ mattr_accessor :port
19
+ @@port = nil
20
+
21
+ mattr_accessor :protocol
22
+ @@protocol = nil
23
+
24
+ mattr_accessor :collection_actions
25
+ @@collection_actions = [:index]
26
+
27
+ # @deprecated: use config.allowed_parameters instead
28
+ mattr_accessor :whitelisted_parameters
29
+ @@whitelisted_parameters = []
30
+
31
+ mattr_accessor :allowed_parameters
32
+ @@allowed_parameters = []
33
+
34
+ mattr_accessor :opengraph_url
35
+ @@opengraph_url = false
36
+
37
+ def self.sym_collection_actions
38
+ @@sym_collection_actions ||= self.collection_actions.map(&:to_sym)
39
+ end
40
+
41
+ def self.sym_allowed_parameters
42
+ @@sym_allowed_parameters ||= if self.whitelisted_parameters.empty?
43
+ self.allowed_parameters.map(&:to_sym)
44
+ else
45
+ CanonicalRails::Deprecation.warn('config.whitelisted_parameters is deprecated, please use config.allowed_parameters instead.')
46
+ self.whitelisted_parameters.map(&:to_sym)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,17 @@
1
+ module CanonicalRails
2
+ class InstallGenerator < Rails::Generators::Base
3
+
4
+ def self.source_paths
5
+ paths = []
6
+ paths << File.expand_path('../templates', "../../#{__FILE__}")
7
+ paths << File.expand_path('../templates', "../#{__FILE__}")
8
+ paths << File.expand_path('../templates', __FILE__)
9
+ paths.flatten
10
+ end
11
+
12
+ def add_files
13
+ template 'canonical_rails.rb', 'config/initializers/canonical_rails.rb'
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,29 @@
1
+ # Do yourself a favor and set these up right when you install the engine.
2
+
3
+ CanonicalRails.setup do |config|
4
+
5
+ # Force the protocol. If you do not specify, the protocol will be based on the incoming request's protocol.
6
+
7
+ config.protocol#= 'https://'
8
+
9
+ # This is the main host, not just the TLD, omit slashes and protocol. If you have more than one, pick the one you want to rank in search results.
10
+
11
+ config.host# = 'www.mywebstore.com'
12
+ config.port# = '3000'
13
+
14
+ # http://en.wikipedia.org/wiki/URL_normalization
15
+ # Trailing slash represents semantics of a directory, ie a collection view - implying an :index get route;
16
+ # otherwise we have to assume semantics of an instance of a resource type, a member view - implying a :show get route
17
+ #
18
+ # Acts as a whitelist for routes to have trailing slashes
19
+
20
+ config.collection_actions# = [:index]
21
+
22
+ # Parameter spamming can cause index dilution by creating seemingly different URLs with identical or near-identical content.
23
+ # Unless allowed, these parameters will be omitted
24
+
25
+ config.allowed_parameters# = []
26
+
27
+ # Output a matching OpenGraph URL meta tag (og:url) with the canonical URL, as recommended by Facebook et al
28
+ config.opengraph_url#= true
29
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :canonical-rails do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: webtranslateit-canonical-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.18
5
+ platform: ruby
6
+ authors:
7
+ - Denis Ivanov
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: actionview
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '4.1'
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: '8.2'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '4.1'
29
+ - - "<"
30
+ - !ruby/object:Gem::Version
31
+ version: '8.2'
32
+ - !ruby/object:Gem::Dependency
33
+ name: actionpack
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '4.1'
39
+ - - "<"
40
+ - !ruby/object:Gem::Version
41
+ version: '8.2'
42
+ type: :development
43
+ prerelease: false
44
+ version_requirements: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '4.1'
49
+ - - "<"
50
+ - !ruby/object:Gem::Version
51
+ version: '8.2'
52
+ - !ruby/object:Gem::Dependency
53
+ name: appraisal
54
+ requirement: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ type: :development
60
+ prerelease: false
61
+ version_requirements: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ - !ruby/object:Gem::Dependency
67
+ name: rspec-rails
68
+ requirement: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - "~>"
71
+ - !ruby/object:Gem::Version
72
+ version: 4.0.1
73
+ type: :development
74
+ prerelease: false
75
+ version_requirements: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: 4.0.1
80
+ - !ruby/object:Gem::Dependency
81
+ name: pry
82
+ requirement: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ type: :development
88
+ prerelease: false
89
+ version_requirements: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: 'Configurable, but assumes a conservative strategy by default with a
95
+ goal to solve many search engine index problems: multiple hostnames, inbound links
96
+ with arbitrary parameters, trailing slashes. '
97
+ email:
98
+ - visible@jumph4x.net
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - MIT-LICENSE
104
+ - README.md
105
+ - Rakefile
106
+ - app/helpers/canonical_rails/tag_helper.rb
107
+ - config/routes.rb
108
+ - lib/canonical-rails.rb
109
+ - lib/canonical-rails/deprecation.rb
110
+ - lib/canonical-rails/engine.rb
111
+ - lib/canonical-rails/version.rb
112
+ - lib/generators/canonical_rails/install/install_generator.rb
113
+ - lib/generators/canonical_rails/install/templates/canonical_rails.rb
114
+ - lib/tasks/canonical-rails_tasks.rake
115
+ homepage: https://github.com/jumph4x/canonical-rails
116
+ licenses:
117
+ - MIT
118
+ metadata: {}
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubygems_version: 3.6.9
134
+ specification_version: 4
135
+ summary: Simple and configurable Rails canonical ref tag helper
136
+ test_files: []