Class: SeoAssist

Inherits:
Object
  • Object
show all
Defined in:
lib/seo_assist.rb

Overview

Make redirects for SEO needs

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ SeoAssist

Returns a new instance of SeoAssist.



3
4
5
# File 'lib/seo_assist.rb', line 3

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/seo_assist.rb', line 7

def call(env)
  request = Rack::Request.new(env)
  params = request.params
  taxon_id = params['taxon']
  if !taxon_id.blank? && !taxon_id.is_a?(Hash) && @taxon = Taxon.find(taxon_id)
    params.delete('taxon')
    query = build_query(params)
    permalink = @taxon.permalink[0...-1] #ensures no trailing / for taxon urls
    return [301, { 'Location'=> "/t/#{permalink}?#{query}" }, []]
  elsif env["PATH_INFO"] =~ /^\/(t|products)(\/\S+)?\/$/
    #ensures no trailing / for taxon and product urls
    query = build_query(params)
    new_location = env["PATH_INFO"][0...-1]
    new_location += '?' + query unless query.blank?
    return [301, { 'Location'=> new_location }, []]
  end
  @app.call(env)
end