Class: Rack::CanonicalLink

Inherits:
Object
  • Object
show all
Defined in:
lib/rack-canonical_link.rb,
lib/rack-canonical_link/version.rb

Constant Summary collapse

VERSION =
"0.0.2"

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ CanonicalLink

Returns a new instance of CanonicalLink.



5
6
7
# File 'lib/rack-canonical_link.rb', line 5

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rack-canonical_link.rb', line 9

def call(env)
  @status, @headers, @body = @app.call(env)
  
  if env['rack.url_scheme'] != 'https' && (@headers['Content-Type'] =~ /application\/html/ || @headers['Content-Type'] =~ /text\/html/)
    request = Rack::Request.new env
    new_body = []
    
    @body.each {|fragement| new_body << fragement.gsub(%r{</head>}, '<link href="' + request.url.gsub(/\/$/,'') + '" rel="canonical" /></head>') } 
    @body = new_body
  end

  [@status, @headers, @body]
end