Class: Slimmer::App
- Inherits:
-
Object
- Object
- Slimmer::App
- Defined in:
- lib/slimmer.rb
Instance Method Summary collapse
- #admin(request, body) ⇒ Object
- #call(env) ⇒ Object
- #filter_headers(header_hash) ⇒ Object
-
#initialize(app, *args, &block) ⇒ App
constructor
A new instance of App.
- #on_404(request, body) ⇒ Object
- #on_error(request, status, body) ⇒ Object
- #on_success(request, body) ⇒ Object
- #rewrite_response(env, triplet) ⇒ Object
- #s(body) ⇒ Object
Constructor Details
#initialize(app, *args, &block) ⇒ App
Returns a new instance of App.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/slimmer.rb', line 15 def initialize(app, *args, &block) = args.first || {} @app = app if .key? :template_path raise "Template path should not be used. Use asset_host instead." end unless [:asset_host] [:asset_host] = Plek.current.find("assets") end @skin = Skin.new [:asset_host], [:cache_templates] end |
Instance Method Details
#admin(request, body) ⇒ Object
43 44 45 |
# File 'lib/slimmer.rb', line 43 def admin(request,body) @skin.admin(request,body) end |
#call(env) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/slimmer.rb', line 30 def call(env) response_array = @app.call(env) if response_array[1][SKIP_HEADER] response_array else rewrite_response(env, response_array) end end |
#filter_headers(header_hash) ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/slimmer.rb', line 88 def filter_headers(header_hash) valid_keys = ['vary', 'set-cookie', 'location', 'content-type', 'expires', 'cache-control'] header_hash.keys.each do |key| header_hash.delete(key) unless valid_keys.include?(key.downcase) end header_hash end |
#on_404(request, body) ⇒ Object
51 52 53 |
# File 'lib/slimmer.rb', line 51 def on_404(request,body) @skin.error(request, '404', body) end |
#on_error(request, status, body) ⇒ Object
47 48 49 |
# File 'lib/slimmer.rb', line 47 def on_error(request, status, body) @skin.error(request, '500', body) end |
#on_success(request, body) ⇒ Object
39 40 41 |
# File 'lib/slimmer.rb', line 39 def on_success(request,body) @skin.success(request, body) end |
#rewrite_response(env, triplet) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/slimmer.rb', line 62 def rewrite_response(env,triplet) status, headers, app_body = triplet source_request = Rack::Request.new(env) request = Rack::Request.new(headers) if headers['Content-Type'] =~ /text\/html/ || headers['content-type'] =~ /text\/html/ case status.to_i when 200 if headers[TEMPLATE_HEADER] == 'admin' || source_request.path =~ /^\/admin(\/|$)/ rewritten_body = admin(request,s(app_body)) else rewritten_body = on_success(request,s(app_body)) end when 301, 302, 304 rewritten_body = app_body when 404 rewritten_body = on_404(request,s(app_body)) else rewritten_body = on_error(request,status,s(app_body)) end else rewritten_body = app_body end rewritten_body = [rewritten_body] unless rewritten_body.respond_to?(:each) [status, filter_headers(headers), rewritten_body] end |
#s(body) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/slimmer.rb', line 55 def s(body) return body.to_s unless body.respond_to?(:each) b = "" body.each {|a| b << a } b end |