Skip to content

Commit

Permalink
moving to metal
Browse files Browse the repository at this point in the history
  • Loading branch information
saturnflyer committed Jun 25, 2009
1 parent 2a62a8c commit 15add08
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 104 deletions.
72 changes: 72 additions & 0 deletions app/metal/vapor_flow.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
require 'cgi'

class VaporFlow
# Radiant must be restarted if the configuration changes for this setting
@@use_regexp = nil
class << self
def call(env)
url = env["PATH_INFO"].sub(/^\//,'') #clean off the first slash, like it is stored in the db
sql = "SELECT * FROM config where key = 'vapor.use_regexp'"
if @@use_regexp.nil?
config_key = Radiant::Config.connection.select_one(sql)
@@use_regexp = (config_key && config_key.value == 'true') ? true : false
end
if @@use_regexp
catch_with_regexp(url)
else
catch_without_regexp(url)
end
end

def match_substitute(string, match)
string.gsub(/\$([`&0-9'$])/) do |sub|
case $1
when "`": match.pre_match
when "&": match[0]
when "0".."9": puts $1.to_i; match[$1.to_i]
when "'": match.post_match
when "$": '$'
end
end
end

def radiant_path(redirect_url)
'/' + redirect_url
end

def local_or_external_path(path)
path.match(/https?:\/\//) ? path : self.radiant_path(path)
end

def catch_with_regexp(url)
FlowMeter.all.sort.reverse.each do |meter|
key = meter[0]
value = meter[1]
if (match = url.match(Regexp.new('^'+key)))
status = value[1].to_i
redirect_url = self.match_substitute(value[0], match)
[status, {"Location" => CGI.unescape(self.local_or_external_path(redirect_url))}, [status.to_s]]
break
else
self.send_to_radiant
break
end
end
end

def catch_without_regexp(url)
a_match = FlowMeter.all[url]
unless a_match.nil?
status = a_match[1].to_i
redirect_url = a_match[0]
[status, {"Location" => CGI.unescape(self.local_or_external_path(redirect_url))}, [status.to_s]]
else
self.send_to_radiant
end
end

def send_to_radiant
[404, {'Content-Type' => 'text/html'}, ['Off to Radiant we go!']]
end
end
end
2 changes: 0 additions & 2 deletions lib/vapor.rb

This file was deleted.

46 changes: 0 additions & 46 deletions lib/vapor/controller_extensions.rb

This file was deleted.

2 changes: 1 addition & 1 deletion spec/controllers/admin/flow_meters_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require File.dirname(__FILE__) + '/../../spec_helper'

describe Admin::FlowMetersController do
scenario :users
dataset :users

before(:each) do
login_as :developer
Expand Down
52 changes: 0 additions & 52 deletions spec/controllers/site_controller_spec.rb

This file was deleted.

5 changes: 2 additions & 3 deletions vapor_extension.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require_dependency 'application'
require_dependency 'application_controller'

class VaporExtension < Radiant::Extension
version "1.0"
version "2.0"
description "Manage redirects without creating useless pages"
url "http://saturnflyer.com/"

Expand All @@ -14,7 +14,6 @@ class VaporExtension < Radiant::Extension
def activate
admin.tabs.add "Redirects", "/admin/flow_meters", :after => "Layouts", :visibility => [:admin]
FlowMeter.initialize_all if ActiveRecord::Base.connection.tables.include?('flow_meters')
SiteController.send :include, Vapor::ControllerExtensions

if admin.respond_to? :help
admin.help.index.add :page_details, 'slug_redirect', :after => 'slug'
Expand Down

0 comments on commit 15add08

Please sign in to comment.