pagelime-rack 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 962d445dd40119959ee05bf58956c5e1f35fcdf2
4
- data.tar.gz: ffacd3eb81878110192113a19baaf5c8c900bce4
3
+ metadata.gz: 31190639b8f777f1cb45f8686a62fcba12cf3471
4
+ data.tar.gz: 2bedf88cb41d6e5e84433a3e5558189674e53803
5
5
  SHA512:
6
- metadata.gz: 1035b00408158783bd980eaf86e8a23842eb3d582ef119f17ae5cb25bc44ddd044c514d6b721a87106a34265830fd817268856c2d346e85dc425797d9f4c5fbe
7
- data.tar.gz: 93d89782ea417292705d445866eb11d920a6785c4b892f51cc9c478c6fd8aac4f0226967505ba6f346ca9cc50a5b299f37d34b342f25e333b4d3904c39e43e4c
6
+ metadata.gz: 6680f8a6043ad9b09d19269f4402085265ee86fec4f6f10c055f2ebe04d5a3d1f1fbe4bd457b515801b5f87aebc922f4e60bbcdb68b3501a07e24a0dfef37745
7
+ data.tar.gz: 45f0641b2217676ad2d6c066cbb0ee0ed856c8d79d46e6ccc5a2bff09132e2713de066be836be6b8d47ed1e0a93b1020dd498422d044f226f0675d7d62265b72
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.4.0
data/config.ru ADDED
@@ -0,0 +1,12 @@
1
+ require 'rack'
2
+ require 'rack/lobster'
3
+ require './lib/rack/pagelime'
4
+
5
+ use Rack::Pagelime
6
+
7
+ Pagelime.configure do |config|
8
+ config.toggle_processing = "per_request"
9
+ config.url_path = "bogus"
10
+ end
11
+
12
+ run Rack::Lobster.new#lambda{|env| [200, {}, [':-(']] }
@@ -4,12 +4,15 @@ module Pagelime
4
4
  class Configuration
5
5
 
6
6
  # only allow getter access unless using configure block
7
- attr_accessor :logger, :storage, :cache, :processor
7
+ attr_accessor :logger, :storage, :cache, :processor, :url_path, :toggle_processing
8
8
  attr_accessor :generate_region_cache_key, :static_shared_cache_key, :cache_fetch_options
9
9
 
10
10
  # pass in a configure block to write new values
11
11
  def initialize(defaults = {}, &block)
12
- @logger = Logger.new(STDOUT)
12
+ @logger = Logger.new(STDOUT)
13
+ @url_path = "/pagelime"
14
+ # on, per_request, off
15
+ @toggle_processing = "on"
13
16
 
14
17
  configure(&block)
15
18
  end
data/lib/rack/pagelime.rb CHANGED
@@ -6,29 +6,107 @@ require_relative '../pagelime'
6
6
  module Rack
7
7
  class Pagelime
8
8
  include Rack::Utils
9
-
10
- def initialize(app, options = {})
11
- @app = app
12
- @options = options
9
+
10
+ TOGGLE_PROCESSING_ENV_KEY = "pagelime.toggle_processing"
11
+
12
+ module ClassMethods
13
+ def enable_processing_for_request(env)
14
+ env[TOGGLE_PROCESSING_ENV_KEY] = "on"
15
+ end
16
+
17
+ def disable_processing_for_request(env)
18
+ env[TOGGLE_PROCESSING_ENV_KEY] = "off"
19
+ end
20
+
21
+ def processing_enabled_for_request?(env)
22
+ config_option = ::Pagelime.config.toggle_processing
23
+ config_option = env[TOGGLE_PROCESSING_ENV_KEY] if config_option == "per_request"
24
+
25
+ ::Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: Procesing enabled for request? (config: #{::Pagelime.config.toggle_processing}, env: #{env[TOGGLE_PROCESSING_ENV_KEY]}, evaluated as: #{config_option})"
26
+
27
+ return config_option == "on"
28
+ end
29
+ end
30
+
31
+ include ClassMethods
32
+ extend ClassMethods
33
+
34
+ def initialize(app)
35
+ @app = app
13
36
 
14
37
  ::Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: Rack Plugin Initialized"
15
38
  end
16
39
 
17
40
  def call(env)
41
+
42
+ status, headers, response = @app.call(env)
43
+
44
+ req = Rack::Request.new(env)
45
+ path = req.path.gsub(/\A\/+|\/+\Z/, "")
46
+ prefix = ::Pagelime.config.url_path.gsub(/\A\/+|\/+\Z/, "")
47
+ action = path["#{prefix}/".size..-1].to_s
48
+
49
+ # hijack response if a pagelime route, otherwise process output if so required
50
+ if path.start_with?("#{prefix}/") || path == prefix
51
+ case action
52
+ # handle publish callback
53
+ when "after_publish_callback"
54
+ resp = handle_publish_callback(status, headers, response, env)
55
+ # handle "index"
56
+ when ""
57
+ resp = handle_status_check(status, headers, response, env)
58
+ else
59
+ ::Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: Unable to route action! (URL prefix: #{::Pagelime.config.url_path}, Request path: #{req.path})"
60
+ end
61
+ else
62
+ ::Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: Unable to route prefix! (URL prefix: #{::Pagelime.config.url_path}, Request path: #{req.path})"
63
+ end
64
+
65
+ # only process original output if routing wasn't handled
66
+ resp ||= handle_html_processing(status, headers, response, env)
67
+
68
+ resp
69
+ end
70
+
71
+ # responses
72
+
73
+ def handle_publish_callback(status, headers, response, env)
74
+
75
+ req = Rack::Request.new(env)
76
+
77
+ ::Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: Route for publish callback called!"
78
+
79
+ ::Pagelime.cache.clear_page(req.params["path"].to_s)
80
+ ::Pagelime.cache.clear_shared
81
+
82
+ [200, {"Content-Type" => "text/html"}, ["cache cleared"]]
83
+ end
84
+
85
+ def handle_status_check(status, headers, response, env)
86
+
87
+ req = Rack::Request.new(env)
88
+
89
+ ::Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: Route for index called!"
90
+
91
+ [200, {"Content-Type" => "text/html"}, ["working"]]
92
+ end
93
+
94
+ def handle_html_processing(status, headers, response, env)
95
+
96
+ req = Rack::Request.new(env)
18
97
 
19
98
  status, headers, response = @app.call(env)
20
99
 
21
100
  ::Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: Headers: #{headers}"
22
101
  ::Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: Status: #{status}"
23
102
  ::Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: Response: #{response}"
24
-
25
- if status == 200 && headers["content-type"].include?("text/html")
103
+
104
+ if processing_enabled_for_request?(env) && status == 200 &&
105
+ headers["content-type"] && headers["content-type"].include?("text/html")
26
106
 
27
- body_content = StringIO.new
28
- response.each{|part| body_content << part}
29
-
30
- req = Rack::Request.new(env)
31
-
107
+ body_content = StringIO.new(response)
108
+ #response.each{|part| body_content << part}
109
+
32
110
  ::Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: Processing For Path: #{req.path}"
33
111
  ::Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: Processing Body (size:#{body_content.length})"
34
112
 
@@ -36,16 +114,18 @@ module Rack
36
114
 
37
115
  headers['content-length'] = body.length.to_s
38
116
 
39
- return [status, headers, [body]]
117
+ body = [body]
40
118
 
41
119
  else
42
120
 
43
121
  ::Pagelime.logger.debug "PAGELIME CMS RACK PLUGIN: Not touching this request"
44
122
 
45
- return [status, headers, response]
123
+ body = response
46
124
 
47
125
  end
48
-
126
+
127
+ [status, headers, body]
128
+
49
129
  end
50
130
 
51
131
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "pagelime-rack"
8
- s.version = "0.3.0"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Emil Anticevic", "Joel Van Horn"]
12
- s.date = "2013-09-11"
12
+ s.date = "2013-09-24"
13
13
  s.description = "The Pagelime Rack Middleware will process outgoing HTML, look for editable areas, and replace the content with the appropriate HTML from the Pagelime CDN and cache it in memory if possible."
14
14
  s.email = "emil@pagelime.com"
15
15
  s.extra_rdoc_files = [
@@ -24,6 +24,7 @@ Gem::Specification.new do |s|
24
24
  "README.md",
25
25
  "Rakefile",
26
26
  "VERSION",
27
+ "config.ru",
27
28
  "lib/pagelime-rack.rb",
28
29
  "lib/pagelime.rb",
29
30
  "lib/pagelime/cache_engine.rb",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pagelime-rack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emil Anticevic
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-09-11 00:00:00.000000000 Z
12
+ date: 2013-09-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
@@ -112,6 +112,7 @@ files:
112
112
  - README.md
113
113
  - Rakefile
114
114
  - VERSION
115
+ - config.ru
115
116
  - lib/pagelime-rack.rb
116
117
  - lib/pagelime.rb
117
118
  - lib/pagelime/cache_engine.rb