publicious 0.2.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,18 +1,74 @@
1
1
  if defined?(Rails)
2
+ require 'publicious/railtie'
3
+ require 'publicious/mime_types'
4
+ end
5
+
6
+ class Publicious
7
+ FILE_METHODS = %w(GET HEAD).freeze
8
+
9
+ def initialize(app)
10
+ @app = app
11
+ yield if block_given?
12
+ end
13
+
14
+ def klass
15
+ self.class
16
+ end
17
+
18
+ def call(env)
19
+ puts "we're in Publicious"
20
+ request = Rack::Request.new(env)
21
+ return @app.call(env) unless FILE_METHODS.include?(request.request_method)
22
+
23
+ if request.path_info =~ %r{^\/(#{klass.allowed_dirs.join("|")})}
24
+ file_name = nil
25
+ path = nil
26
+
27
+ klass.public_paths.detect do |pub_path|
28
+ path = pub_path
29
+ fp = File.join(pub_path, request.path_info)
30
+ file_name = fp if File.file?(fp)
31
+ end
32
+
33
+ return respond_not_found! unless file_name
2
34
 
3
- Mime::Type.register "image/jpeg", :jpg
4
- Mime::Type.register "image/gif", :gif
5
- Mime::Type.register "image/png", :png
6
-
7
- module Publicious
8
- class Engine < Rails::Engine
35
+ # Make sure pricks aren't ../../config/database.yml ing us
36
+ respond_not_found! unless file_name.gsub(%r[^#{path}], "") == request.path_info
37
+
38
+ Rack::Response.new(
39
+ File.open(file_name),
40
+ 200,'Content-Type' => content_type_for_file(file_name)
41
+ ).finish
42
+ else
43
+ @app.call(env)
9
44
  end
10
45
  end
11
-
12
- end
13
46
 
47
+ def respond_not_found!
48
+ Rack::Response.new("Not Found", 404).finish
49
+ end
50
+
51
+ def content_type_for_file(name)
52
+ file_name = File.basename(name).split(".").last.to_s
53
+ Mime::Type.lookup_by_extension(file_name).to_s
54
+ end
14
55
 
56
+ def self.allowed_dirs
57
+ %w(stylesheets javascripts images)
58
+ end
15
59
 
16
- require "publicious/responder"
60
+ def self.public_paths
61
+ @public_paths ||= begin
62
+ paths = []
63
+ paths += add_engines_public_paths! if defined?(::Rails::Engine)
64
+ paths
65
+ end
66
+ end
17
67
 
68
+ def self.add_engines_public_paths!
69
+ ::Rails::Engine.subclasses.map { |klass|
70
+ klass.config.paths.public.paths.first
71
+ }.flatten.compact.reject { |path| path =~ /publicious/}
72
+ end
73
+ end
18
74
 
@@ -0,0 +1,3 @@
1
+ Mime::Type.register "image/jpeg", :jpg
2
+ Mime::Type.register "image/gif", :gif
3
+ Mime::Type.register "image/png", :png
@@ -0,0 +1,9 @@
1
+ #class PubliciousRailtie < Rails::Railtie
2
+ # config.app.middelware.insert_before(Rack::Lock, Publicious)
3
+ #end
4
+
5
+ class MyRailtie < Rails::Railtie
6
+ initializer "my_railtie.configure_rails_initialization" do |app|
7
+ app.middleware.insert_before(Rack::Lock, Publicious)
8
+ end
9
+ end
@@ -1,10 +1,10 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = %q{publicious}
3
- s.version = "0.2.3"
3
+ s.version = "0.3.0"
4
4
 
5
5
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
6
6
  s.authors = ["Justin French", "Daniel Neighman"]
7
- s.date = %q{2010-07-21}
7
+ s.date = %q{2010-07-22}
8
8
  s.description = %q{A Rails 3 gem plugin for plugins to serve images, javascripts and stylesheets from thier own public directory}
9
9
  s.summary = %q{A Rails gem plugin for plugins to serve images, javascripts and stylesheets from thier own public directory}
10
10
  s.email = %q{justin@indent.com.au}
@@ -7,20 +7,22 @@ require "publicious"
7
7
 
8
8
  RAILS_ROOT = "/tmp"
9
9
 
10
+ NOT_FOUND_APP = lambda{|e| Rack::Response.new("NOT FOUND", 404).finish}
11
+
10
12
  module TestHelper
11
13
  include Rack::Test::Methods
12
-
14
+
13
15
  def app
14
- Publicious::Responder
16
+ Publicious.new(NOT_FOUND_APP)
15
17
  end
16
-
18
+
17
19
  def setup_vendor_dir
18
20
  unless @vendor_dir
19
21
  @vendor_dir = "/tmp/vendor"
20
22
  FileUtils.mkdir(@vendor_dir)
21
23
  end
22
24
  end
23
-
25
+
24
26
  def teardown_vendor_dir
25
27
  if @vendor_dir
26
28
  begin
@@ -30,25 +32,25 @@ module TestHelper
30
32
  end
31
33
  end
32
34
  end
33
-
35
+
34
36
  def setup_standard_view_paths
35
37
  ActionController::Base.view_paths = ['/tmp/app/views']
36
38
  end
37
-
39
+
38
40
  def setup
39
41
  setup_vendor_dir
40
42
  setup_standard_view_paths
41
43
  end
42
-
44
+
43
45
  def teardown
44
46
  teardown_vendor_dir
45
47
  setup_standard_view_paths
46
48
  end
47
-
49
+
48
50
  def setup_plugin(*plugin_names)
49
51
  plugin_names.each do |plugin_name|
50
52
  plugin_name = plugin_name.to_s
51
-
53
+
52
54
  class_eval "
53
55
  module ::#{plugin_name.classify}
54
56
  class Engine < ::Rails::Engine
@@ -56,9 +58,9 @@ module TestHelper
56
58
  paths.public = '#{@vendor_dir}/#{plugin_name}/public'
57
59
  end
58
60
  end"
59
-
61
+
60
62
  #ActionController::Base.view_paths << File.join(@vendor_dir, plugin_name, 'app', 'views')
61
-
63
+
62
64
  FileUtils.mkdir(File.join(@vendor_dir, plugin_name))
63
65
  FileUtils.mkdir(File.join(@vendor_dir, plugin_name, 'public'))
64
66
  FileUtils.mkdir(File.join(@vendor_dir, plugin_name, 'public', 'stylesheets'))
@@ -66,5 +68,5 @@ module TestHelper
66
68
  FileUtils.mkdir(File.join(@vendor_dir, plugin_name, 'public', 'javascripts'))
67
69
  end
68
70
  end
69
-
71
+
70
72
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
8
7
  - 3
9
- version: 0.2.3
8
+ - 0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Justin French
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-07-21 00:00:00 +10:00
18
+ date: 2010-07-22 00:00:00 +10:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -28,10 +28,11 @@ extensions: []
28
28
  extra_rdoc_files:
29
29
  - README.textile
30
30
  files:
31
- - config/routes.rb
32
- - lib/publicious/responder.rb
31
+ - lib/publicious/mime_types.rb
32
+ - lib/publicious/railtie.rb
33
33
  - lib/publicious.rb
34
34
  - MIT-LICENSE
35
+ - publicious-0.3.0.gem
35
36
  - publicious.gemspec
36
37
  - Rakefile
37
38
  - README.textile
@@ -1,5 +0,0 @@
1
- Rails::Application.routes.draw do |map|
2
- match "/stylesheets/*path" => Publicious::Responder
3
- match "/images/*path" => Publicious::Responder
4
- match "/javascripts/*path" => Publicious::Responder
5
- end
@@ -1,51 +0,0 @@
1
- module Publicious
2
- class Responder
3
-
4
- def self.call(env)
5
- request = Rack::Request.new(env)
6
- if request.path_info =~ %r{^\/(#{allowed_dirs.join("|")})}
7
- file_name = nil
8
- path = nil
9
-
10
- public_paths.detect do |pub_path|
11
- path = pub_path
12
- fp = File.join(pub_path, request.path_info)
13
- file_name = fp if File.file?(fp)
14
- end
15
-
16
- return respond_not_found! unless file_name
17
-
18
- # Make sure pricks aren't ../../config/database.yml ing us
19
- respond_not_found! unless file_name.gsub(%r[^#{path}], "") == request.path_info
20
-
21
- Rack::Response.new(
22
- File.open(file_name),
23
- 200,'Content-Type' => content_type_for_file(file_name)
24
- ).finish
25
- else
26
- respond_not_found!
27
- end
28
- end
29
-
30
- def self.respond_not_found!
31
- Rack::Response.new("Not Found", 404).finish
32
- end
33
-
34
- def self.allowed_dirs
35
- %w(stylesheets javascripts images)
36
- end
37
-
38
- def self.content_type_for_file(name)
39
- file_name = File.basename(name).split(".").last.to_s
40
- Mime::Type.lookup_by_extension(file_name).to_s
41
- end
42
-
43
- def self.public_paths
44
- ::Rails::Engine.subclasses.map { |klass|
45
- klass.config.paths.public.paths.first
46
- }.flatten.compact.reject { |path| path =~ /publicious/}
47
- end
48
-
49
- end
50
-
51
- end