class Canonical::Middleware

Public Class Methods

canonical_tag(env, canonical_host) click to toggle source
# File lib/rack-canonical-header/middleware.rb, line 18
def self.canonical_tag(env, canonical_host)
  url_scheme = env['HTTP_X_FORWARDED_PROTO'] || env['rack.url_scheme']
  canonical = "#{url_scheme}://#{canonical_host}#{env['ORIGINAL_FULLPATH']}"
  "<#{canonical}>; rel=\"canonical\""
end
new(app) click to toggle source
# File lib/rack-canonical-header/middleware.rb, line 3
def initialize(app)
  @app = app
  @status, @headers, @body = nil
end

Public Instance Methods

call(env) click to toggle source
# File lib/rack-canonical-header/middleware.rb, line 8
def call(env)
  @status, @headers, @body = @app.call(env)
  canonical_host = ENV['CANONICAL_HOST']
  return [@status, @headers, @body] unless html? && canonical_host

  @headers['Link'] = Middleware.canonical_tag(env, canonical_host)

  [@status, @headers, @body]
end

Private Instance Methods

html?() click to toggle source
# File lib/rack-canonical-header/middleware.rb, line 26
def html?
  @headers['Content-Type'] =~ /html/
end