Class: Jets::Controller::Response

Inherits:
Rack::Response
  • Object
show all
Includes:
ActionDispatch::Http::Cache::Response, ActionDispatch::Http::FilterRedirect, Compat::Response, MonitorMixin
Defined in:
lib/jets/controller/response.rb

Overview

The response object. See Rack::Response and Rack::Response::Helpers for more info: rubydoc.info/github/rack/rack/master/Rack/Response rubydoc.info/github/rack/rack/master/Rack/Response/Helpers

Defined Under Namespace

Modules: Compat

Constant Summary collapse

NO_CONTENT_CODES =
[100, 101, 102, 103, 204, 205, 304]

Constants included from Compat::Response

Compat::Response::CONTENT_TYPE, Compat::Response::CONTENT_TYPE_PARSER, Compat::Response::NullContentTypeHeader

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Compat::Response

#charset, #charset=, #parse_content_type, #parsed_content_type_header, #set_content_type

Constructor Details

#initializeResponse

Returns a new instance of Response.



21
22
23
24
# File 'lib/jets/controller/response.rb', line 21

def initialize(*)
  super
  prepare_cache_control!
end

Instance Attribute Details

#requestObject

Returns the value of attribute request.



19
20
21
# File 'lib/jets/controller/response.rb', line 19

def request
  @request
end

Instance Method Details

#assign_default_content_type_and_charset!Object



73
74
75
76
77
78
79
# File 'lib/jets/controller/response.rb', line 73

def assign_default_content_type_and_charset!
  return if media_type

  ct = parsed_content_type_header
  set_content_type(ct.mime_type || Mime[:html].to_s,
                   ct.charset || self.class.default_charset)
end

#before_committedObject



66
67
68
69
70
71
# File 'lib/jets/controller/response.rb', line 66

def before_committed
  assign_default_content_type_and_charset!
  merge_and_normalize_cache_control!(@cache_control)
  handle_conditional_get!
  handle_no_content!
end

#body=(body) ⇒ Object

What Rack::Response#initialize does.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/jets/controller/response.rb', line 27

def body=(body)
  if body.nil?
    @body = []
    @buffered = true
    @length = 0
  elsif body.respond_to?(:to_str)
    @body = [body]
    @buffered = true
    @length = body.to_str.bytesize
  else
    @body = body
    @buffered = false
    @length = 0
  end
  @body
end

#commit!Object



59
60
61
62
63
64
# File 'lib/jets/controller/response.rb', line 59

def commit!
  synchronize do
    before_committed
    @committed = true
  end
end

#committed?Boolean

Returns:

  • (Boolean)


54
# File 'lib/jets/controller/response.rb', line 54

def committed?; synchronize { @committed }; end

#eachObject



96
97
98
# File 'lib/jets/controller/response.rb', line 96

def each
  block_given? ? super : enum_for(:each)
end

#finishObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/jets/controller/response.rb', line 100

def finish
  result = body

  if 
    headers.delete "Content-Length"
    headers.delete "Content-Type"
  end

  if drop_body?
    close
    result = []
  end

  if calculate_content_length?
    # if some other code has already set Content-Length, don't muck with it
    # currently, this would be the static file-handler
    headers["Content-Length"] = body.inject(0) { |l, p| l + p.bytesize }.to_s
  end

  [status.to_i, headers, result]
end

#handle_no_content!Object



81
82
83
84
85
# File 'lib/jets/controller/response.rb', line 81

def handle_no_content!
  if NO_CONTENT_CODES.include?(@status)
    headers.delete "Content-Length"
  end
end

#media_typeObject



125
126
127
# File 'lib/jets/controller/response.rb', line 125

def media_type
  headers["Content-Type"]
end

#rack_response(status, header) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/jets/controller/response.rb', line 88

def rack_response(status, header)
  if NO_CONTENT_CODES.include?(status)
    [status, header, []]
  else
    [status, header, body]
  end
end

#sending?Boolean

sending? and send? are for compatibility but not used for Jets

Returns:

  • (Boolean)


56
# File 'lib/jets/controller/response.rb', line 56

def sending?;   synchronize { @sending };   end

#sent?Boolean

Returns:

  • (Boolean)


57
# File 'lib/jets/controller/response.rb', line 57

def sent?;      synchronize { @sent };      end

#status=(status) ⇒ Object

Sets the HTTP status code.



45
46
47
# File 'lib/jets/controller/response.rb', line 45

def status=(status)
  @status = Rack::Utils.status_code(status)
end

#to_aObject



49
50
51
52
# File 'lib/jets/controller/response.rb', line 49

def to_a
  commit!
  rack_response status, headers.to_hash
end