class Rack::CSSHTTPRequest

A Rack middleware for providing CSSHTTPRequest responses.

Public Class Methods

new(app) click to toggle source
   # File lib/rack/contrib/csshttprequest.rb
10 def initialize(app)
11   @app = app
12 end

Public Instance Methods

call(env) click to toggle source

Proxies the request to the application then encodes the response with the CSSHTTPRequest encoder

   # File lib/rack/contrib/csshttprequest.rb
16 def call(env)
17   status, headers, response = @app.call(env)
18   headers = Utils::HeaderHash.new(headers)
19 
20   if chr_request?(env)
21     encoded_response = encode(response)
22     modify_headers!(headers, encoded_response)
23     response = [encoded_response]
24   end
25   [status, headers, response]
26 end
chr_request?(env) click to toggle source
   # File lib/rack/contrib/csshttprequest.rb
28 def chr_request?(env)
29   env['csshttprequest.chr'] ||=
30     !(/\.chr$/.match(env['PATH_INFO'])).nil? || Rack::Request.new(env).params['_format'] == 'chr'
31 end
encode(body) click to toggle source
   # File lib/rack/contrib/csshttprequest.rb
33 def encode(body)
34   ::CSSHTTPRequest.encode(body.to_enum.to_a.join)
35 end
modify_headers!(headers, encoded_response) click to toggle source
   # File lib/rack/contrib/csshttprequest.rb
37 def modify_headers!(headers, encoded_response)
38   headers['Content-Length'] = encoded_response.bytesize.to_s
39   headers['Content-Type'] = 'text/css'
40   nil
41 end