Class: Cannon::Response

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Views, Signature
Defined in:
lib/cannon/response.rb

Constant Summary collapse

HTTP_STATUS =
{
  continue:                      100,
  switching_protocols:           101,
  ok:                            200,
  created:                       201,
  accepted:                      202,
  non_authoritative_information: 203,
  no_content:                    204,
  reset_content:                 205,
  partial_content:               206,
  multiple_choices:              300,
  moved_permanently:             301,
  found:                         302,
  see_other:                     303,
  not_modified:                  304,
  use_proxy:                     305,
  temporary_redirect:            307,
  bad_request:                   400,
  unauthorized:                  401,
  payment_required:              402,
  forbidden:                     403,
  not_found:                     404,
  method_not_allowed:            405,
  not_acceptable:                406,
  proxy_authentication_required: 407,
  request_timeout:               408,
  conflict:                      409,
  gone:                          410,
  length_required:               411,
  precondition_failed:           412,
  request_entity_too_large:      413,
  request_uri_too_long:          414,
  unsupported_media_type:        415,
  requested_range_not_satisfied: 416,
  expectation_failed:            417,
  internal_server_error:         500,
  not_implemented:               501,
  bad_gateway:                   502,
  service_unavailable:           503,
  gateway_timeout:               504,
  http_version_not_supported:    505,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Signature

#digest, #signature

Methods included from Views

#view

Methods included from PathCache

included

Constructor Details

#initialize(http_server, app) ⇒ Response

Returns a new instance of Response.



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cannon/response.rb', line 58

def initialize(http_server, app)
  @app = app
  @delegated_response = EventMachine::DelegatedHttpResponse.new(http_server)
  @flushed = false
  @headers = {}
  @cookies = {}

  initialize_views

  self.status = :ok
end

Instance Attribute Details

#delegated_responseObject (readonly)

Returns the value of attribute delegated_response.



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

def delegated_response
  @delegated_response
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#statusObject

Returns the value of attribute status.



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

def status
  @status
end

Instance Method Details



120
121
122
123
124
125
126
# File 'lib/cannon/response.rb', line 120

def cookie(cookie, value:, expires: nil, httponly: nil, signed: false)
  cookie_options = {:value => value}
  cookie_options[:expires] = expires unless expires.nil?
  cookie_options[:httponly] = httponly unless httponly.nil?
  cookie_options[:signed] = signed
  @cookies[cookie] = cookie_options
end

#flushObject



81
82
83
84
85
86
87
88
# File 'lib/cannon/response.rb', line 81

def flush
  unless flushed?
    set_cookie_headers
    delegated_response.headers = self.headers
    delegated_response.send_response
    @flushed = true
  end
end

#flushed?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/cannon/response.rb', line 70

def flushed?
  @flushed
end

#header(key, value) ⇒ Object



90
91
92
# File 'lib/cannon/response.rb', line 90

def header(key, value)
  headers[key] = value
end

#internal_server_error(title:, content:) ⇒ Object



114
115
116
117
118
# File 'lib/cannon/response.rb', line 114

def internal_server_error(title:, content:)
  html = "<html><head><title>Internal Server Error: #{title}</title></head><body><h1>#{title}</h1><p>#{content}</p></body></html>"
  header('Content-Type', 'text/html')
  send(html, status: :internal_server_error)
end

#location_header(location) ⇒ Object



94
95
96
# File 'lib/cannon/response.rb', line 94

def location_header(location)
  header('Location', location)
end

#not_foundObject



110
111
112
# File 'lib/cannon/response.rb', line 110

def not_found
  send('Not Found', status: :not_found)
end

#permanent_redirect(location) ⇒ Object



98
99
100
101
102
# File 'lib/cannon/response.rb', line 98

def permanent_redirect(location)
  location_header(location)
  self.status = :moved_permanently
  flush
end

#send(content, status: self.status) ⇒ Object



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

def send(content, status: self.status)
  self.content ||= ''
  self.status = status
  delegated_response.status = converted_status(status)
  delegated_response.content += content
end

#temporary_redirect(location) ⇒ Object



104
105
106
107
108
# File 'lib/cannon/response.rb', line 104

def temporary_redirect(location)
  location_header(location)
  self.status = :found
  flush
end