Class: Rack::Pagelime

Inherits:
Object
  • Object
show all
Extended by:
ClassMethods
Includes:
ClassMethods, Utils
Defined in:
lib/rack/pagelime.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

TOGGLE_PROCESSING_ENV_KEY =
"pagelime.toggle_processing"
ROUTE_RESPONSES =
{
  "index"                   => "working",
  "after_publish_callback"  => "cache cleared"
}

Instance Method Summary collapse

Methods included from ClassMethods

disable_processing_for_request, enable_processing_for_request, handle_publish_callback, handle_status_check, processing_enabled_for_request?

Constructor Details

#initialize(app) ⇒ Pagelime

Returns a new instance of Pagelime.



62
63
64
65
66
# File 'lib/rack/pagelime.rb', line 62

def initialize(app)
  @app = app
  
  ::Pagelime.logger.debug  "PAGELIME CMS RACK PLUGIN: Rack Plugin Initialized"
end

Instance Method Details

#call(env) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rack/pagelime.rb', line 68

def call(env)
  
  status, headers, response = @app.call(env)

  req     = Rack::Request.new(env)
  path    = req.path.gsub(/\A\/+|\/+\Z/, "")
  prefix  = ::Pagelime.config.url_path.gsub(/\A\/+|\/+\Z/, "")
  action  = path["#{prefix}/".size..-1].to_s
  
  # hijack response if a pagelime route, otherwise process output if so required
  if path.start_with?("#{prefix}/") || path == prefix
    case action
    # handle publish callback
    when "after_publish_callback"
      resp = handle_publish_callback(env)
    # handle "index"
    when ""
      resp = handle_status_check(env)
    else
      ::Pagelime.logger.debug  "PAGELIME CMS RACK PLUGIN: Unable to route action! (URL prefix: #{::Pagelime.config.url_path}, Request path: #{req.path})"
    end
  else
    ::Pagelime.logger.debug  "PAGELIME CMS RACK PLUGIN: Unable to route prefix! (URL prefix: #{::Pagelime.config.url_path}, Request path: #{req.path})"
  end
  
  # only process original output if routing wasn't handled
  unless resp
  
    ::Pagelime.logger.debug  "PAGELIME CMS RACK PLUGIN: Headers: #{headers}"
    ::Pagelime.logger.debug  "PAGELIME CMS RACK PLUGIN: Status: #{status.inspect}"
    ::Pagelime.logger.debug  "PAGELIME CMS RACK PLUGIN: Response: #{response}"
    
    ::Pagelime.logger.debug "enabled? (#{processing_enabled_for_request?(env)}) status (#{status == 200}) headers (#{headers["Content-Type"] != nil}) html (#{headers["Content-Type"]}) class (#{headers["Content-Type"].class})"
    
    if processing_enabled_for_request?(env) && status == 200 && 
       headers["Content-Type"] != nil && headers["Content-Type"].include?("text/html")
        
      html = ""
      response.each{|part| html << part}
      
      ::Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: Processing For Path: #{req.path}"
      ::Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: Processing Body (size:#{html.length})"
      ::Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: Processing Body: #{html.inspect}"
    
      html = ::Pagelime.process_page(html, req.path)
  
      headers['content-length'] = html.length.to_s
  
      body = [html]
      
    else
  
      ::Pagelime.logger.debug  "PAGELIME CMS RACK PLUGIN: Not touching this request"
  
      body = response
  
    end
    
    resp = [status, headers, body]
    
  end
  
  resp
end