Class: Syntropy::Request

Inherits:
Object
  • Object
show all
Extended by:
RequestInfoClassMethods
Includes:
RequestInfoMethods, RequestValidationMethods, ResponseMethods, TestRequestExtensions
Defined in:
lib/syntropy/request.rb

Overview

Syntropy::Request represents an HTTP request. By interacting with the request, the app can extract request information and respond to the request.

Constant Summary collapse

EMPTY_HEADERS =
{}.freeze

Constants included from RequestInfoClassMethods

Syntropy::RequestInfoClassMethods::MAX_PARAMETER_NAME_SIZE, Syntropy::RequestInfoClassMethods::MAX_PARAMETER_VALUE_SIZE, Syntropy::RequestInfoClassMethods::PARAMETER_RE

Constants included from ResponseMethods

Syntropy::ResponseMethods::WEBSOCKET_GUID

Constants included from RequestInfoMethods

Syntropy::RequestInfoMethods::COOKIE_RE, Syntropy::RequestInfoMethods::QUERY_KV_REGEXP, Syntropy::RequestInfoMethods::SEMICOLON

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RequestInfoClassMethods

parse_form_data, parse_multipart_form_data, parse_multipart_form_data_part, parse_multipart_form_data_part_headers, parse_urlencoded_form_data

Methods included from TestRequestExtensions

#response_body, #response_content_type, #response_cookie, #response_headers, #response_json, #response_status

Methods included from ResponseMethods

#file_full_path, #json_pretty_response, #redirect, #redirect_to_host, #redirect_to_https, #respond_by_http_method, #respond_html, #respond_json, #respond_on_get, #respond_on_post, #respond_with_static_file, #serve_file, #serve_io, #set_cookie, #set_response_headers, #upgrade, #upgrade_to_websocket, #validate_static_file_cache

Methods included from RequestValidationMethods

#validate, #validate_cache, #validate_content_type, #validate_http_method, #validate_param

Methods included from RequestInfoMethods

#accept?, #accept_encoding, #auth_bearer_token, #browser?, #connection, #content_type, #cookies, #forwarded_for, #full_uri, #get_form_data, #host, #method, #parse_cookies, #parse_query, #path, #protocol, #query, #query_string, #request_id, #rewrite!, #scheme, #upgrade_protocol, #uri, #websocket_version

Constructor Details

#initialize(headers, adapter) ⇒ void

Initializes the request.

Parameters:

  • headers (Hash)

    request headers

  • adapter (Object)

    connection adapter



26
27
28
29
30
31
32
33
# File 'lib/syntropy/request.rb', line 26

def initialize(headers, adapter)
  @headers  = headers
  @adapter  = adapter
  @start_stamp = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
  @route = nil
  @route_params = {}
  @ctx = nil
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



18
19
20
# File 'lib/syntropy/request.rb', line 18

def adapter
  @adapter
end

#headersObject (readonly)

Returns the value of attribute headers.



18
19
20
# File 'lib/syntropy/request.rb', line 18

def headers
  @headers
end

#routeObject

Returns the value of attribute route.



19
20
21
# File 'lib/syntropy/request.rb', line 19

def route
  @route
end

#route_paramsObject (readonly)

Returns the value of attribute route_params.



18
19
20
# File 'lib/syntropy/request.rb', line 18

def route_params
  @route_params
end

#start_stampObject (readonly)

Returns the value of attribute start_stamp.



18
19
20
# File 'lib/syntropy/request.rb', line 18

def start_stamp
  @start_stamp
end

Instance Method Details

#complete?bool

Returns true if the request body has been consumed.

Returns:

  • (bool)


70
71
72
# File 'lib/syntropy/request.rb', line 70

def complete?
  @adapter.complete?(self)
end

#ctxHash

Returns the request context, used to store auxiliary information.

Returns:

  • (Hash)

    request context hash



38
39
40
# File 'lib/syntropy/request.rb', line 38

def ctx
  @ctx ||= {}
end

#each_chunkvoid

This method returns an undefined value.

Reads request body chunks until the entire body is consumed, yielding each chunk to the given block.



53
54
55
56
57
# File 'lib/syntropy/request.rb', line 53

def each_chunk
  while (chunk = @adapter.get_body_chunk(self))
    yield chunk
  end
end

#finishvoid

This method returns an undefined value.

Finish response.



113
114
115
116
117
# File 'lib/syntropy/request.rb', line 113

def finish
  send_headers({}) unless @headers_sent

  @adapter.finish(self)
end

#flashSyntropy::Session::Flash

Returns the request flash session storage.

Returns:

  • (Syntropy::Session::Flash)


136
137
138
# File 'lib/syntropy/request.rb', line 136

def flash
  session.flash
end

#headers_sent?bool

Returns true if response headers were sent.

Returns:

  • (bool)


122
123
124
# File 'lib/syntropy/request.rb', line 122

def headers_sent?
  @headers_sent
end

#next_chunkString?

Returns the next request body chunk.

Returns:

  • (String, nil)


45
46
47
# File 'lib/syntropy/request.rb', line 45

def next_chunk
  @adapter.get_body_chunk(self)
end

#readString? Also known as: body

Reads the request body.

Returns:

  • (String, nil)

    request body



62
63
64
# File 'lib/syntropy/request.rb', line 62

def read
  @adapter.get_body(self)
end

#respond(body, headers = EMPTY_HEADERS) ⇒ void

This method returns an undefined value.

Sends a response.

Parameters:

  • body (String, nil)

    response body

  • headers (Hash) (defaults to: EMPTY_HEADERS)

    response headers



81
82
83
84
# File 'lib/syntropy/request.rb', line 81

def respond(body, headers = EMPTY_HEADERS)
  @adapter.respond(self, body, headers)
  @headers_sent = true
end

#send_chunk(body, done: false) ⇒ void Also known as: <<

This method returns an undefined value.

Sends a response body chunk.

Parameters:

  • body (String)

    response body chunk

  • done (bool) (defaults to: false)

    body is complete



103
104
105
106
107
# File 'lib/syntropy/request.rb', line 103

def send_chunk(body, done: false)
  send_headers({}) unless @headers_sent

  @adapter.send_chunk(self, body, done: done)
end

#send_headers(headers = EMPTY_HEADERS, empty_response = false) ⇒ void

This method returns an undefined value.

Sends response headers.

Parameters:

  • headers (Hash) (defaults to: EMPTY_HEADERS)

    response headers

  • empty_response (bool) (defaults to: false)

    body should be sent



91
92
93
94
95
96
# File 'lib/syntropy/request.rb', line 91

def send_headers(headers = EMPTY_HEADERS, empty_response = false)
  return if @headers_sent

  @headers_sent = true
  @adapter.send_headers(self, headers, empty_response: empty_response)
end

#sessionSyntropy::Session

Returns the request session.

Returns:



129
130
131
# File 'lib/syntropy/request.rb', line 129

def session
  @session ||= Session.new(self)
end