Class: Doze::Response

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/doze/response.rb

Constant Summary

Constants included from Utils

Utils::URI_SCHEMES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

#escape, #quote, #request_base_uri, #unescape

Constructor Details

#initialize(status = STATUS_OK, headers = {}, body = '') ⇒ Response

Returns a new instance of Response.



4
5
6
7
8
9
# File 'lib/doze/response.rb', line 4

def initialize(status=STATUS_OK, headers={}, body='')
  @headers = Rack::Utils::HeaderHash.new(headers)
  @status = status
  @body = body
  @head_only = false
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



12
13
14
# File 'lib/doze/response.rb', line 12

def body
  @body
end

#head_onlyObject

Returns the value of attribute head_only.



12
13
14
# File 'lib/doze/response.rb', line 12

def head_only
  @head_only
end

#headersObject (readonly)

Returns the value of attribute headers.



11
12
13
# File 'lib/doze/response.rb', line 11

def headers
  @headers
end

#statusObject

Returns the value of attribute status.



12
13
14
# File 'lib/doze/response.rb', line 12

def status
  @status
end

Class Method Details

.new_empty(status = STATUS_NO_CONTENT, headers = {}) ⇒ Object



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

def self.new_empty(status=STATUS_NO_CONTENT, headers={})
  new(status, headers)
end

.new_from_entity(entity, status = STATUS_OK) ⇒ Object



38
39
40
41
42
# File 'lib/doze/response.rb', line 38

def self.new_from_entity(entity, status=STATUS_OK)
  result = new(status)
  result.entity = entity
  result
end

.new_redirect(resource, request, status = STATUS_SEE_OTHER) ⇒ Object



60
61
62
63
64
# File 'lib/doze/response.rb', line 60

def self.new_redirect(resource, request, status=STATUS_SEE_OTHER)
  result = new
  result.set_redirect(resource, request, status)
  result
end

Instance Method Details

#add_header_values(header, *values) ⇒ Object



66
67
68
69
# File 'lib/doze/response.rb', line 66

def add_header_values(header, *values)
  values.unshift(@headers[header])
  @headers[header] = values.compact.join(', ')
end

#content_lengthObject



20
21
22
# File 'lib/doze/response.rb', line 20

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


75
76
77
# File 'lib/doze/response.rb', line 75

def delete_cookie(key, value={})
  Rack::Utils.delete_cookie_header!(@headers, key, value)
end

#entity=(entity) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/doze/response.rb', line 24

def entity=(entity)
  content_type = entity.media_type.output_name
  content_type = "#{content_type}; charset=#{entity.encoding}" if entity.encoding
  language = entity.language
  etag = entity.etag

  @headers['Content-Type'] = content_type
  @headers['Content-Language'] = language if language
  @headers['ETag'] = quote(etag) if etag
  @headers.merge!(entity.extra_content_headers)

  @body = entity.binary_data
end

#finish(head_only = @head_only) ⇒ Object



14
15
16
17
18
# File 'lib/doze/response.rb', line 14

def finish(head_only=@head_only)
  @headers["Content-Length"] ||= content_length.to_s unless @status == STATUS_NO_CONTENT || @status == STATUS_NOT_MODIFIED
  @headers['Date'] = Time.now.httpdate
  [@status, @headers.to_hash, head_only ? [] : [@body]]
end


71
72
73
# File 'lib/doze/response.rb', line 71

def set_cookie(key, value)
  Rack::Utils.set_cookie_header!(@headers, key, value)
end

#set_location(resource, request) ⇒ Object



55
56
57
58
# File 'lib/doze/response.rb', line 55

def set_location(resource, request)
  base_uri = request.app.config[:base_uri] || request_base_uri(request)
  @headers['Location'] = base_uri.merge(resource.uri).to_s
end

#set_redirect(resource, request, status = STATUS_SEE_OTHER) ⇒ Object



48
49
50
51
52
53
# File 'lib/doze/response.rb', line 48

def set_redirect(resource, request, status=STATUS_SEE_OTHER)
  raise 'Resource specified as a representation must have a uri in order to redirect to it' unless resource.uri
  @status = status
  set_location(resource, request)
  @body ||= ''
end