class Spree::Frontend::Middleware::SeoAssist

Public Class Methods

new(app) click to toggle source
# File lib/spree/frontend/middleware/seo_assist.rb, line 6
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# File lib/spree/frontend/middleware/seo_assist.rb, line 10
def call(env)
  request = Rack::Request.new(env)
  params = request.params

  taxon_id = params['taxon']

  # redirect requests using taxon id's to their permalinks
  if !taxon_id.blank? && !taxon_id.is_a?(Hash) && taxon = Taxon.find(taxon_id)
    params.delete('taxon')

    return build_response(params, "#{request.script_name}t/#{taxon.permalink}")
  elsif env['PATH_INFO'] =~ /^\/(t|products)(\/\S+)?\/$/
    # ensures no trailing / for taxon and product urls

    return build_response(params, env['PATH_INFO'][0...-1])
  end

  @app.call(env)
end

Private Instance Methods

build_query(params) click to toggle source
# File lib/spree/frontend/middleware/seo_assist.rb, line 38
def build_query(params)
  params.map do |k, v|
    if v.class == Array
      build_query(v.map { |x| ["#{k}[]", x] })
    else
      k + '=' + Rack::Utils.escape(v)
    end
  end.join('&')
end
build_response(params, location) click to toggle source
# File lib/spree/frontend/middleware/seo_assist.rb, line 32
def build_response(params, location)
  query = build_query(params)
  location += '?' + query unless query.blank?
  [301, { 'Location' => location }, []]
end