Class: Rack::CSSHTTPRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/contrib/csshttprequest.rb

Overview

A Rack middleware for providing CSSHTTPRequest responses.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ CSSHTTPRequest

Returns a new instance of CSSHTTPRequest.



12
13
14
# File 'lib/rack/contrib/csshttprequest.rb', line 12

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object

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



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rack/contrib/csshttprequest.rb', line 18

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

  if chr_request?(env)
    encoded_response = encode(response)
    modify_headers!(headers, encoded_response)
    response = [encoded_response]
  end
  [status, headers, response]
end

#chr_request?(env) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
# File 'lib/rack/contrib/csshttprequest.rb', line 30

def chr_request?(env)
  env['csshttprequest.chr'] ||=
    !(/\.chr$/.match(env['PATH_INFO'])).nil? || Rack::Request.new(env).params['_format'] == 'chr'
end

#encode(body) ⇒ Object



35
36
37
# File 'lib/rack/contrib/csshttprequest.rb', line 35

def encode(body)
  ::CSSHTTPRequest.encode(body.to_enum.to_a.join)
end

#modify_headers!(headers, encoded_response) ⇒ Object



39
40
41
42
43
# File 'lib/rack/contrib/csshttprequest.rb', line 39

def modify_headers!(headers, encoded_response)
  headers['Content-Length'] = encoded_response.bytesize.to_s
  headers['Content-Type'] = 'text/css'
  nil
end