Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Changes out the responder and removes the engine

  • Loading branch information...
commit 8c1870f53aedd91486312b22af3dd80436655443 1 parent c74bf9a
@hassox hassox authored committed
View
5 config/routes.rb
@@ -1,5 +0,0 @@
-Rails::Application.routes.draw do |map|
- match "/stylesheets/*path" => Publicious::Responder
- match "/images/*path" => Publicious::Responder
- match "/javascripts/*path" => Publicious::Responder
-end
View
68 lib/publicious.rb
@@ -2,11 +2,71 @@
Mime::Type.register "image/gif", :gif
Mime::Type.register "image/png", :png
-module Publicious
- class Engine < Rails::Engine
+class Publicious
+ FILE_METHODS = %w(GET HEAD).freeze
+
+ def initialize(app)
+ @app = app
+ yield if block_given?
+ end
+
+ def klass
+ self.class
+ end
+
+ def call(env)
+ request = Rack::Request.new(env)
+ return @app.call(env) unless FILE_METHODS.include?(request.request_method)
+
+ if request.path_info =~ %r{^\/(#{klass.allowed_dirs.join("|")})}
+ file_name = nil
+ path = nil
+
+ klass.public_paths.detect do |pub_path|
+ path = pub_path
+ fp = File.join(pub_path, request.path_info)
+ file_name = fp if File.file?(fp)
+ end
+
+ return respond_not_found! unless file_name
+
+ # Make sure pricks aren't ../../config/database.yml ing us
+ respond_not_found! unless file_name.gsub(%r[^#{path}], "") == request.path_info
+
+ Rack::Response.new(
+ File.open(file_name),
+ 200,'Content-Type' => content_type_for_file(file_name)
+ ).finish
+ else
+ @app.call(env)
+ end
+ end
+
+ def respond_not_found!
+ Rack::Response.new("Not Found", 404).finish
+ end
+
+ def content_type_for_file(name)
+ file_name = File.basename(name).split(".").last.to_s
+ Mime::Type.lookup_by_extension(file_name).to_s
end
-end
-require "publicious/responder"
+ def self.allowed_dirs
+ %w(stylesheets javascripts images)
+ end
+
+ def self.public_paths
+ @public_paths ||= begin
+ paths = []
+ paths += add_engines_public_paths! if defined?(::Rails::Engine)
+ paths
+ end
+ end
+ def self.add_engines_public_paths!
+ ::Rails::Engine.subclasses.map { |klass|
+ klass.config.paths.public.paths.first
+ }.flatten.compact.reject { |path| path =~ /publicious/}
+ end
+end
View
51 lib/publicious/responder.rb
@@ -1,51 +0,0 @@
-module Publicious
- class Responder
-
- def self.call(env)
- request = Rack::Request.new(env)
- if request.path_info =~ %r{^\/(#{allowed_dirs.join("|")})}
- file_name = nil
- path = nil
-
- public_paths.detect do |pub_path|
- path = pub_path
- fp = File.join(pub_path, request.path_info)
- file_name = fp if File.file?(fp)
- end
-
- return respond_not_found! unless file_name
-
- # Make sure pricks aren't ../../config/database.yml ing us
- respond_not_found! unless file_name.gsub(%r[^#{path}], "") == request.path_info
-
- Rack::Response.new(
- File.open(file_name),
- 200,'Content-Type' => content_type_for_file(file_name)
- ).finish
- else
- respond_not_found!
- end
- end
-
- def self.respond_not_found!
- Rack::Response.new("Not Found", 404).finish
- end
-
- def self.allowed_dirs
- %w(stylesheets javascripts images)
- end
-
- def self.content_type_for_file(name)
- file_name = File.basename(name).split(".").last.to_s
- Mime::Type.lookup_by_extension(file_name).to_s
- end
-
- def self.public_paths
- ::Rails::Engine.subclasses.map { |klass|
- klass.config.paths.public.paths.first
- }.flatten.compact.reject { |path| path =~ /publicious/}
- end
-
- end
-
-end
View
26 test/test_helper.rb
@@ -7,20 +7,22 @@
RAILS_ROOT = "/tmp"
+NOT_FOUND_APP = lambda{|e| Rack::Response.new("NOT FOUND", 404).finish}
+
module TestHelper
include Rack::Test::Methods
-
+
def app
- Publicious::Responder
+ Publicious.new(NOT_FOUND_APP)
end
-
+
def setup_vendor_dir
unless @vendor_dir
@vendor_dir = "/tmp/vendor"
FileUtils.mkdir(@vendor_dir)
end
end
-
+
def teardown_vendor_dir
if @vendor_dir
begin
@@ -30,25 +32,25 @@ def teardown_vendor_dir
end
end
end
-
+
def setup_standard_view_paths
ActionController::Base.view_paths = ['/tmp/app/views']
end
-
+
def setup
setup_vendor_dir
setup_standard_view_paths
end
-
+
def teardown
teardown_vendor_dir
setup_standard_view_paths
end
-
+
def setup_plugin(*plugin_names)
plugin_names.each do |plugin_name|
plugin_name = plugin_name.to_s
-
+
class_eval "
module ::#{plugin_name.classify}
class Engine < ::Rails::Engine
@@ -56,9 +58,9 @@ class Engine < ::Rails::Engine
paths.public = '#{@vendor_dir}/#{plugin_name}/public'
end
end"
-
+
#ActionController::Base.view_paths << File.join(@vendor_dir, plugin_name, 'app', 'views')
-
+
FileUtils.mkdir(File.join(@vendor_dir, plugin_name))
FileUtils.mkdir(File.join(@vendor_dir, plugin_name, 'public'))
FileUtils.mkdir(File.join(@vendor_dir, plugin_name, 'public', 'stylesheets'))
@@ -66,5 +68,5 @@ class Engine < ::Rails::Engine
FileUtils.mkdir(File.join(@vendor_dir, plugin_name, 'public', 'javascripts'))
end
end
-
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.