Class: Utopia::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/request.rb

Overview

Utopia's application-facing request wrapper.

Protocol request methods are delegated to the underlying request; parsing and application conveniences live here rather than on protocol-http itself.

Constant Summary collapse

QUERY_PARSER =
Protocol::URL::FormData::Parser.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(delegate) ⇒ Request

Initialize the request.



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/utopia/request.rb', line 26

def initialize(delegate)
	@delegate = delegate
	
	@url = nil
	@session = nil
	@variables = nil
	@localization = nil
	@exception = nil
	
	@query_parameters = nil
	@cookies = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object (private)



190
191
192
193
194
195
196
# File 'lib/utopia/request.rb', line 190

def method_missing(name, ...)
	if @delegate.respond_to?(name)
		@delegate.public_send(name, ...)
	else
		super
	end
end

Instance Attribute Details

#delegateObject (readonly)

The underlying protocol request.



40
41
42
# File 'lib/utopia/request.rb', line 40

def delegate
  @delegate
end

#exceptionObject

The exception associated with this request, if any.



52
53
54
# File 'lib/utopia/request.rb', line 52

def exception
  @exception
end

#localizationObject

The immutable localization preferences associated with this request, if installed.



49
50
51
# File 'lib/utopia/request.rb', line 49

def localization
  @localization
end

#sessionObject

The session associated with this request, if installed.



43
44
45
# File 'lib/utopia/request.rb', line 43

def session
  @session
end

#variablesObject

The controller variables associated with this request, if installed.



46
47
48
# File 'lib/utopia/request.rb', line 46

def variables
  @variables
end

Class Method Details

.[](*arguments) ⇒ Object

Build a Utopia request from the given protocol request arguments.



20
21
22
# File 'lib/utopia/request.rb', line 20

def self.[](*arguments)
	self.new(Protocol::HTTP::Request[*arguments])
end

Instance Method Details

#cookiesObject

Decoded request cookies.



140
141
142
# File 'lib/utopia/request.rb', line 140

def cookies
	@cookies ||= parse_cookies(self.headers["cookie"])
end

#ipObject

The remote peer IP address, if available.



155
156
157
# File 'lib/utopia/request.rb', line 155

def ip
	@delegate.peer&.ip_address
end

#pathObject



71
72
73
# File 'lib/utopia/request.rb', line 71

def path
	Path.new(self.url.path.components(Protocol::URL::Encoding::System))
end

#path=(value) ⇒ Object

Assign the decoded application path while preserving the current query string.



77
78
79
80
81
82
83
# File 'lib/utopia/request.rb', line 77

def path= value
	if value.is_a?(Protocol::URL::Path)
		self.url.path = value
	else
		self.url.path = Path[value].to_url_path
	end
end

#post?Boolean

Whether the request method is POST.

Returns:

  • (Boolean)


92
93
94
# File 'lib/utopia/request.rb', line 92

def post?
	@delegate.method == "POST"
end

#query?Boolean

Whether the request method is QUERY.

Returns:

  • (Boolean)


87
88
89
# File 'lib/utopia/request.rb', line 87

def query?
	@delegate.method == "QUERY"
end

#query_parametersObject

Decoded query arguments.



116
117
118
# File 'lib/utopia/request.rb', line 116

def query_parameters
	@query_parameters ||= parse_query_parameters(self.url.query)
end

#referrerObject

The request referrer.



150
151
152
# File 'lib/utopia/request.rb', line 150

def referrer
	@delegate.headers["referer"]
end

#request_pathObject

The normalized original request path, before any internal request rewrites.



98
99
100
101
102
103
# File 'lib/utopia/request.rb', line 98

def request_path
	path = @delegate.path.split("?", 2).first
	path = Protocol::URL::Path[path].normalize.simplify
	
	return Path.new(path.components(Protocol::URL::Encoding::System))
end

#urlObject

The structured request URL.



66
67
68
# File 'lib/utopia/request.rb', line 66

def url
	@url ||= self.parse_url.normalize!
end

#url=(url) ⇒ Object

Assign the structured request URL.



161
162
163
164
# File 'lib/utopia/request.rb', line 161

def url= url
	@url = Protocol::URL[url]
	@query_parameters = nil
end

#user_agentObject

The request user agent.



145
146
147
# File 'lib/utopia/request.rb', line 145

def user_agent
	@delegate.headers["user-agent"]
end

#with(method: nil, path: self.path) ⇒ Object

Build a derived request with updated protocol fields.



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/utopia/request.rb', line 167

def with(method: nil, path: self.path)
	delegate = @delegate
	
	if method
		delegate = @delegate.dup
		delegate.method = method
	end
	
	request = self.class.new(delegate)
	request.session = @session
	request.variables = @variables
	request.localization = @localization
	request.exception = @exception
	request.path = path
	
	return request
end