Permalink
Browse files

Merge branch 'config'

Conflicts:
	VERSION
	lib/app/helpers/esi_helper.rb
	ventilation.gemspec
  • Loading branch information...
2 parents 05eb5f7 + 3cc54e8 commit 3e211f3ddd8c0e99cb52d34205382e0e91210f1c Blake Taylor committed Dec 4, 2010
Showing with 55 additions and 23 deletions.
  1. +9 −1 CHANGELOG.md
  2. +1 −1 VERSION
  3. +37 −20 lib/app/helpers/esi_helper.rb
  4. +7 −0 lib/ventilation.rb
  5. +1 −1 ventilation.gemspec
View
@@ -1 +1,9 @@
- * Fixes issue with urls without paths.
+ * Optimize code for production esi.
+ * Add expire helper for setting cache control headers.
+ * Always use varnish if its available.
+ * Turn off esi resolution for environment except for development.
+
+Version 0.2.3 - 12/3/2010
+-------------------------
+ * Turn off esi resolution for staging by default.
+ * Fixes issue with urls without paths.
View
@@ -1 +1 @@
-0.2.3
+0.2.3
@@ -6,31 +6,37 @@
module Ventilation
module EsiHelper
- # Render resource on the edge
def esi(resource, options = {})
- env = ENV['RAILS_ENV']
-
- # If we were passed a url...
if resource =~ URI.regexp
- # ...fetch and render an external resource...
- case env
- when 'production', 'staging'
- %%<esi:include src="#{resource}" />%
- else
+ %%<esi:include src="#{resource}" />%
+ else
+ %%<esi:include src="#{url_for options.merge(:action => resource)}" />%
+ end
+ end
+
+ # Only enable ventilation esi resolutioon features in development.
+ if 'development' == ENV['RAILS_ENV']
+
+ alias :esi_tag :esi
+
+ # Render resource on the edge
+ def esi(resource, options = {})
+
+ # Use esi if supported.
+ return esi_tag(resource, options) if esi_supported?
+
+ # If we were passed a url...
+ if resource =~ URI.regexp
+ # ...fetch and render an external resource...
url = URI.parse(resource)
res = Net::HTTP.start(url.host, url.port) {|http|
path = url.path.blank? ? "/" : url.path
path = url.query ? "#{path}?#{url.query}" : path
http.get(path)
}
res.body
- end
- else
- # ...otherwise render as an action.
- case env
- when 'production', 'staging'
- %%<esi:include src="#{url_for url_options.merge(:action => resource)}" />%
else
+ # ...otherwise render as an action.
if controller = options[:controller]
controller = "#{controller.to_s.camelcase}Controller".constantize
else
@@ -51,10 +57,21 @@ def esi(resource, options = {})
end
end
- end
-end
+ # Helper for setting expire headers.
+ #
+ # Examples:
+ # <%- expire 5.minutes -%>
+ # <%- expire 30.seconds -%>
+ def expire(duration)
+ headers['Cache-Control'] = 'max-age=#{duration.to_i}'
+ headers['Expires'] = duration.from_now.httpdate
+ end
+
+ private
+ def esi_supported?
+ # Enable esi if behind varnish.
+ request.env.has_key?('HTTP_X_VARNISH')
+ end
-# Include EsiHelper in the Application
-module ApplicationHelper
- include Ventilation::EsiHelper
+ end
end
View
@@ -1 +1,8 @@
require 'app/helpers/esi_helper'
+
+# Include EsiHelper in the Application
+module ApplicationHelper
+ include Ventilation::EsiHelper
+end
+
+
View
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Blake Taylor"]
- s.date = %q{2010-12-03}
+ s.date = %q{2010-12-04}
s.description = %q{Helper methods for building esi tags, simplifies development bypassing the need for varnish.}
s.email = %q{btaylor@agoragames.com}
s.extra_rdoc_files = [

0 comments on commit 3e211f3

Please sign in to comment.