class Takana::Middleware
Attributes
app[R]
debug[RW]
TODO allow this to be toggled from configure block
debug?[RW]
TODO allow this to be toggled from configure block
host[R]
kind[R]
name[R]
path[R]
paths[R]
Public Class Methods
new(app, options={})
click to toggle source
# File lib/rack-takana/middleware.rb, line 10 def initialize(app, options={}) @path = Dir.pwd.to_s @name = Pathname.new(@path).basename.to_s @app = app @kind, @paths = detect_kind! @debug = false @host = "localhost" @port = 48626 if Takana.db if defined?(Rails) Rails.logger.debug "[Takana] #{@name} at #{@path} #{@kind} #{@paths}" end @project = Takana::Project[name: name] || Takana::Project.create(name: name, path: path, activated: false) @paths.each do |path| unless @project.load_paths.find { |_| _.path == path.to_s }.present? @project.add_load_path(path: path.to_s, source: 'rack-takana') end end if defined?(Compass) && defined?(Rails) && Rails.application.config.compass.present? Rails.logger.debug "[Takana] detected compass. Attempting to load configuration" begin config = Rails.application.config.compass.serialize if config.present? attributes = {} Compass::Configuration::ATTRIBUTES.each do |k| attributes[k] = Rails.application.config.compass.send(k) end @project.compass_configs.each(&:delete) @project.add_compass_config( config: config, attributes: Marshal.dump(attributes) ) end # tell projects to reload begin url = URI.parse("http://#{@host}:#{@port}/projects/load") req = Net::HTTP::Get.new(url.to_s) res = Net::HTTP.start(url.host, url.port) { |http| http.request(req) } rescue => e Rails.logger.debug "[Takana] project reload request failed, perhaps takana isn't running #{e.inspect}" end rescue => e Rails.logger.debug "[Takana] failed to load compass configuration" end end end end
Public Instance Methods
call(env)
click to toggle source
# File lib/rack-takana/middleware.rb, line 72 def call(env) @env = env @status, @headers, @response = @app.call(env) if is_takana_compatible_response? update_response! update_content_length! end [@status, @headers, @response] end
Private Instance Methods
detect_kind!()
click to toggle source
# File lib/rack-takana/middleware.rb, line 87 def detect_kind! if defined?(Rails) paths = (Rails.application.config.sass[:load_paths] rescue []) + Rails.application.config.assets[:paths] ["rails", paths] else ["rack", []] end end
is_ajax_request?()
click to toggle source
# File lib/rack-takana/middleware.rb, line 118 def is_ajax_request? @env.has_key?("HTTP_X_REQUESTED_WITH") && @env["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest" end
is_html_response?()
click to toggle source
# File lib/rack-takana/middleware.rb, line 122 def is_html_response? @headers["Content-Type"].include?("text/html") if @headers.has_key?("Content-Type") end
is_regular_request?()
click to toggle source
# File lib/rack-takana/middleware.rb, line 114 def is_regular_request? !is_ajax_request? end
is_takana_compatible_response?()
click to toggle source
# File lib/rack-takana/middleware.rb, line 126 def is_takana_compatible_response? return false if @status == 302 return false if @env.has_key?("HTTP_SKIP_TAKANA_MIDDLEWARE") is_html_response? end
render_takana_scripts()
click to toggle source
# File lib/rack-takana/middleware.rb, line 132 def render_takana_scripts <<-EOT <script type="text/javascript" src="http://#{host}:48626/javascripts/edge.js" data-project="#{name}" data-debug="#{debug?}"></script> EOT end
update_content_length!()
click to toggle source
# File lib/rack-takana/middleware.rb, line 108 def update_content_length! new_size = 0 @response.each { |part| new_size += part.bytesize } @headers.merge!("Content-Length" => new_size.to_s) end
update_response!()
click to toggle source
# File lib/rack-takana/middleware.rb, line 97 def update_response! @response.each do |part| if is_regular_request? && is_html_response? insert_at = part.index('</body') unless insert_at.nil? part.insert(insert_at, render_takana_scripts) end end end end