Class: Dassets::Server::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, asset_file) ⇒ Response

Returns a new instance of Response.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dassets/server/response.rb', line 11

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, [] ]
  elsif !@asset_file.exists?
    [ 404, Rack::Utils::HeaderHash.new, ["Not Found"] ]
  else
    @asset_file.digest!
    [ 200,
      Rack::Utils::HeaderHash.new.tap do |h|
        h["Content-Type"]   = @asset_file.mime_type.to_s
        h["Content-Length"] = @asset_file.size.to_s
        h["Last-Modified"]  = mtime
      end,
      env["REQUEST_METHOD"] == "HEAD" ? [] : [ @asset_file.content ]
    ]
  end
end

Instance Attribute Details

#asset_fileObject (readonly)

Returns the value of attribute asset_file.



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

def asset_file
  @asset_file
end

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#statusObject (readonly)

Returns the value of attribute status.



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

def status
  @status
end

Instance Method Details

#to_rackObject



32
33
34
# File 'lib/dassets/server/response.rb', line 32

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