Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

moving to metal

  • Loading branch information...
commit 15add081b72046bf02e65286aa458d37b9a3c66f 1 parent 2a62a8c
@saturnflyer saturnflyer authored
View
72 app/metal/vapor_flow.rb
@@ -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
View
2  lib/vapor.rb
@@ -1,2 +0,0 @@
-module Vapor
-end
View
46 lib/vapor/controller_extensions.rb
@@ -1,46 +0,0 @@
-module Vapor::ControllerExtensions
- def self.included(base)
- base.class_eval { before_filter :apply_flow_meters }
- end
-
- def apply_flow_meters
- url = params[:url]
- if Array === url
- url = url.join('/')
- else
- url = url.to_s
- end
- if config['vapor.use_regexp'] == 'true'
- FlowMeter.all.sort.reverse.each do |meter|
- key = meter[0]
- value = meter[1]
- if (match = url.match(Regexp.new('^'+key)))
- redirect_url = match_substitute(value[0], match)
- location = redirect_url.match('http://') ? redirect_url : url_for(:controller => 'site', :action => 'show_page', :url => redirect_url)
- redirect_to CGI.unescape(location), :status => value[1].to_s and return
- end
- end
- else
- a_match = FlowMeter.all[url]
- unless a_match.nil?
- redirect_url = a_match[0]
- location = url.match('http://') ? redirect_url : url_for(:controller => 'site', :action => 'show_page', :url => redirect_url)
- redirect_to CGI.unescape(location), :status => a_match[1].to_s and return
- end
- end
- end
-
-private
-
- 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
-end
View
2  spec/controllers/admin/flow_meters_controller_spec.rb
@@ -1,7 +1,7 @@
require File.dirname(__FILE__) + '/../../spec_helper'
describe Admin::FlowMetersController do
- scenario :users
+ dataset :users
before(:each) do
login_as :developer
View
52 spec/controllers/site_controller_spec.rb
@@ -1,52 +0,0 @@
-require File.dirname(__FILE__) + '/../spec_helper'
-
-describe SiteController do
- scenario :users_and_pages
-
- before(:each) do
- login_as :admin
- controller.cache.clear
- @flow_meters = {"this" => ["that", "307"], "this2" => ["blog/2005/01/01/some-post", "307"], "this/(.+)" => ["http://that.org/$1", "301"]}
- @flow_meter = FlowMeter.create!(:catch_url => 'this', :redirect_url => 'that')
- @flow_meter = FlowMeter.create!(:catch_url => 'this2', :redirect_url => 'blog/2005/01/01/some-post')
- @flow_meter = FlowMeter.create!(:catch_url => 'this/(.+)', :redirect_url => 'http://that.org/$1')
- FlowMeter.initialize_all
-
- # @wildcard = mock_model(FlowMeter)
- FlowMeter.stub!(:all).and_return(@flow_meters)
- end
-
- describe "requesting Vapor URL" do
- before(:each) do
- controller.config['vapor.use_regexp'] = nil
- end
-
- it "should redirect to the given Redirect URL" do
- get :show_page, :url => 'this'
- response.should be_redirect
- end
- it "should redirect to the given Redirect URL" do
- get :show_page, :url => 'this2'
- response.should redirect_to('blog/2005/01/01/some-post')
- end
- it "should set the response status to the given Status" do
- get :show_page, :url => 'this'
- response.headers["Status"].should =~ /307/
- end
- end
-
- describe "requesting Vapor URL when use_regexp is on" do
- before(:each) do
- controller.config['vapor.use_regexp'] = 'true'
- end
-
- it "should catch URLs that begin with the regexp" do
- get :show_page, :url => 'this/page'
- response.redirect?.should == true
- end
- it "should do substitutions in the redirect URL" do
- get :show_page, :url => 'this/page'
- response.should redirect_to('http://that.org/page')
- end
- end
-end
View
5 vapor_extension.rb
@@ -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/"
@@ -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'
Please sign in to comment.
Something went wrong with that request. Please try again.