Class: Dassets::Server::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/dassets/server/response.rb

Defined Under Namespace

Classes: Body

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, asset_file) ⇒ Response

Returns a new instance of Response.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/dassets/server/response.rb', line 12

def initialize(env, asset_file)
  @asset_file = asset_file

  mtime = @asset_file.mtime.to_s
  @status, @headers, @body = if env["HTTP_IF_MODIFIED_SINCE"] == mtime
    [
      304,
      Rack::Utils::HeaderHash.new("Last-Modified" => mtime),
      [],
    ]
  elsif !@asset_file.exists?
    [
      404,
      Rack::Utils::HeaderHash.new,
      ["Not Found"],
    ]
  else
    @asset_file.digest!
    body = Body.new(env, @asset_file)
    [
      body.partial? ? 206 : 200,
      Rack::Utils::HeaderHash.new.merge(@asset_file.response_headers).tap do |h|
        h["Last-Modified"]  = mtime.to_s
        h["Content-Type"]   = @asset_file.mime_type.to_s
        h["Content-Length"] = body.size.to_s
        h["Content-Range"]  = body.content_range if body.partial?
      end,
      env["REQUEST_METHOD"] == "HEAD" ? [] : body
    ]
  end
end

Instance Attribute Details

#asset_fileObject (readonly)

Returns the value of attribute asset_file.



10
11
12
# File 'lib/dassets/server/response.rb', line 10

def asset_file
  @asset_file
end

#bodyObject (readonly)

Returns the value of attribute body.



10
11
12
# File 'lib/dassets/server/response.rb', line 10

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



10
11
12
# File 'lib/dassets/server/response.rb', line 10

def headers
  @headers
end

#statusObject (readonly)

Returns the value of attribute status.



10
11
12
# File 'lib/dassets/server/response.rb', line 10

def status
  @status
end

Instance Method Details

#to_rackObject



44
45
46
# File 'lib/dassets/server/response.rb', line 44

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