Class: RedisRedirect::Routes

Inherits:
Object
  • Object
show all
Defined in:
lib/redis-redirect/routes.rb

Class Method Summary collapse

Class Method Details

.build_url(host, relative_path) ⇒ Object



26
27
28
# File 'lib/redis-redirect/routes.rb', line 26

def self.build_url(host, relative_path)
  "http://" + host + relative_path 
end

.call(env) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/redis-redirect/routes.rb', line 3

def self.call(env)
  relative_path = env["PATH_INFO"]

  return continue_to_rails_stack if relative_path.nil?

  begin
    new_location_relative_path = RedisRedirect.redis.get(relative_path)
  rescue Redis::CannotConnectError => exception
    new_location_relative_path = nil
  end
  
  if new_location_relative_path.nil?
    continue_to_rails_stack
  else
    host = env["HTTP_X_FORWARDED_HOST"] || env["HTTP_HOST"]
    redirect_to build_url(host, new_location_relative_path)
  end
end

.continue_to_rails_stackObject



30
31
32
# File 'lib/redis-redirect/routes.rb', line 30

def self.continue_to_rails_stack
  [404, {"Content-Type" => "text/html", "X-Cascade" => "pass"}, ["Not Found"]]
end

.redirect_to(location) ⇒ Object



22
23
24
# File 'lib/redis-redirect/routes.rb', line 22

def self.redirect_to(location) 
  [302, {"Content-Type" => "text/html", "Location" => location}, ["Redirecting..."]]    
end