Sha256: afdff07cfc6f56aaf0bd1f22ef6721694c26e36d13833dc0fccd46bb8c4066e2

Contents?: true

Size: 1.16 KB

Versions: 9

Compression:

Stored size: 1.16 KB

Contents

# Allow the metal piece to run in isolation
require(File.dirname(__FILE__) + "/../../config/environment") unless defined?(Rails)

# Make redirects for SEO needs
class SeoAssist

  def self.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
    [404, {"Content-Type" => "text/html"}, "Not Found"]
  end

  private

  def self.build_query(params)
    params.map { |k, v|
      if v.class == Array
        build_query(v.map { |x| ["#{k}[]", x] })
      else
        k + "=" + Rack::Utils.escape(v)
      end
    }.join("&")
  end

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
spree-0.11.4 app/metal/seo_assist.rb
spree-0.11.3 app/metal/seo_assist.rb
spree-0.11.2 app/metal/seo_assist.rb
spree-0.11.1 app/metal/seo_assist.rb
spree-0.11.0 app/metal/seo_assist.rb
spree-0.10.2 app/metal/seo_assist.rb
spree-0.10.1 app/metal/seo_assist.rb
spree-0.10.0 app/metal/seo_assist.rb
spree-0.10.0.beta app/metal/seo_assist.rb