Module: Handler301

Defined in:
lib/handler301.rb

Constant Summary collapse

Hash301 =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load_301_file(file) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/handler301.rb', line 50

def self.load_301_file(file)
  h = YAML.load_file(file) rescue {}
  if Hash === h and h.size > 0
    Hash301.merge!(h)
  else
    puts "[Handler301] Error in loading '#{file}', this file is empty or not valid."
  end
end

Instance Method Details

#handle_301(path, query_params = {}) ⇒ Object

Handle 301 redirections Make the redirection if an url match Return true if a redirection has been made



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/handler301.rb', line 15

def handle_301(path, query_params = {})
  return nil if !path
  
  path = path.gsub(/^\//, '').split('?', 2).first

  if r = Hash301[path]
    query_params.delete('action')
    query_params.delete('controller')

    redirect_path = nil
    # Standard case, use the simple route name
    if !r.index(' ')
      redirect_path = self.send(handle_301_auto_add_path(r).to_sym, query_params)
    else # Blank case / Works only with hashes

      r = r.split(' ', 2)
      rhash = r.last
      rhash = "{#{rhash}}" unless rhash =~ /^\{.*\}$/

      # Must do an eval here ?
      hash = eval(rhash) rescue {}

      redirect_path = send(handle_301_auto_add_path(r[0]).to_sym, hash.merge(query_params))
    end

    if redirect_path
      redirect_to(redirect_path, :status => 301)
      return true
    end
  end

  # No redirection has been made
  return nil
end