Class: Rack::Sprockets::Response

Inherits:
Object
  • Object
show all
Includes:
Response::Helpers, Options
Defined in:
lib/rack/sprockets/response.rb

Overview

Given some generated js, mimicks a Rack::Response

> call to_rack to build standard rack response parameters

Constant Summary

Constants included from Options

Options::COLLECTION_OPTS, Options::RACK_ENV_NS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Options

included

Constructor Details

#initialize(env, js) ⇒ Response

Create a Response instance given the env and some generated js.



30
31
32
33
34
35
36
37
38
# File 'lib/rack/sprockets/response.rb', line 30

def initialize(env, js)
  @env = env
  @body = js
  @status = 200 # OK
  @headers = Rack::Utils::HeaderHash.new

  headers["Content-Type"] = Rack::Sprockets::MIME_TYPE
  headers["Content-Length"] = self.class.content_length(body).to_s
end

Instance Attribute Details

#bodyObject

Rack response tuple accessors.



13
14
15
# File 'lib/rack/sprockets/response.rb', line 13

def body
  @body
end

#headersObject

Rack response tuple accessors.



13
14
15
# File 'lib/rack/sprockets/response.rb', line 13

def headers
  @headers
end

#statusObject

Rack response tuple accessors.



13
14
15
# File 'lib/rack/sprockets/response.rb', line 13

def status
  @status
end

Class Method Details

.content_length(body) ⇒ Object

Calculate appropriate content_length



18
19
20
21
22
23
24
# File 'lib/rack/sprockets/response.rb', line 18

def content_length(body)
  if body.respond_to?(:bytesize)
    body.bytesize
  else
    body.size
  end
end

Instance Method Details

#to_rackObject



40
41
42
# File 'lib/rack/sprockets/response.rb', line 40

def to_rack
  [status, headers.to_hash, [body]]
end