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.



8
9
10
# File 'lib/rack/contrib/csshttprequest.rb', line 8

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



14
15
16
17
18
19
20
21
# File 'lib/rack/contrib/csshttprequest.rb', line 14

def call(env)
  status, headers, response = @app.call(env)
  if chr_request?(env)
    response = encode(response)
    modify_headers!(headers, response)
  end
  [status, headers, response]
end

#chr_request?(env) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
# File 'lib/rack/contrib/csshttprequest.rb', line 23

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

#encode(response, assembled_body = "") ⇒ Object



28
29
30
31
# File 'lib/rack/contrib/csshttprequest.rb', line 28

def encode(response, assembled_body="")
  response.each { |s| assembled_body << s } # call down the stack
  return ::CSSHTTPRequest.encode(assembled_body)
end

#modify_headers!(headers, encoded_response) ⇒ Object



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

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