Class: Aikido::Zen::Request
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Aikido::Zen::Request
- Defined in:
- lib/aikido/zen/request.rb
Overview
Wrapper around Rack::Request-like objects to add some behavior.
Defined Under Namespace
Classes: HeuristicRouter, RailsRouter, Schema
Constant Summary collapse
- BLESSED_CGI_HEADERS =
%w[CONTENT_TYPE CONTENT_LENGTH]
Instance Attribute Summary collapse
-
#actor ⇒ Aikido::Zen::Actor?
The current user, if set by the host app.
-
#framework ⇒ String
readonly
Identifier of the framework handling this HTTP request.
- #router ⇒ Aikido::Zen::Router readonly
-
#tenant_id ⇒ Integer, ...
The current tenant, if set by the host app.
Instance Method Summary collapse
-
#__setobj__(delegate) ⇒ Object
:nodoc:.
- #as_json ⇒ Object
-
#client_ip ⇒ String
The IP address of the client making the request.
-
#initialize(delegate, config = Aikido::Zen.config, framework:, router:) ⇒ Request
constructor
A new instance of Request.
-
#normalized_headers ⇒ Hash<String, String>
Map the CGI-style env Hash into “pretty-looking” headers, preserving the values as-is.
-
#route ⇒ Aikido::Zen::Route
The framework route being requested.
- #schema ⇒ Aikido::Zen::Request::Schema?
Constructor Details
#initialize(delegate, config = Aikido::Zen.config, framework:, router:) ⇒ Request
Returns a new instance of Request.
26 27 28 29 30 31 |
# File 'lib/aikido/zen/request.rb', line 26 def initialize(delegate, config = Aikido::Zen.config, framework:, router:) super(delegate) @config = config @framework = framework @router = router end |
Instance Attribute Details
#actor ⇒ Aikido::Zen::Actor?
The current user, if set by the host app.
18 19 20 |
# File 'lib/aikido/zen/request.rb', line 18 def actor @actor end |
#framework ⇒ String (readonly)
Returns identifier of the framework handling this HTTP request.
9 10 11 |
# File 'lib/aikido/zen/request.rb', line 9 def framework @framework end |
#router ⇒ Aikido::Zen::Router (readonly)
12 13 14 |
# File 'lib/aikido/zen/request.rb', line 12 def router @router end |
#tenant_id ⇒ Integer, ...
The current tenant, if set by the host app.
24 25 26 |
# File 'lib/aikido/zen/request.rb', line 24 def tenant_id @tenant_id end |
Instance Method Details
#__setobj__(delegate) ⇒ Object
:nodoc:
33 34 35 36 |
# File 'lib/aikido/zen/request.rb', line 33 def __setobj__(delegate) # :nodoc: super @route = @normalized_headers = nil end |
#as_json ⇒ Object
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/aikido/zen/request.rb', line 83 def as_json { method: request_method.upcase, url: url, ipAddress: client_ip, userAgent: user_agent, source: framework, route: route&.path } end |
#client_ip ⇒ String
Returns the IP address of the client making the request.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/aikido/zen/request.rb', line 49 def client_ip return @client_ip if @client_ip if @config.client_ip_header value = env[@config.client_ip_header] if Resolv::AddressRegex.match?(value) @client_ip = value else @config.logger.warn("Invalid IP address in custom client IP header `#{@config.client_ip_header}`: `#{value}`") end end @client_ip ||= respond_to?(:remote_ip) ? remote_ip : ip end |
#normalized_headers ⇒ Hash<String, String>
Map the CGI-style env Hash into “pretty-looking” headers, preserving the values as-is. For example, HTTP_ACCEPT turns into “Accept”, CONTENT_TYPE turns into “Content-Type”, and HTTP_X_FORWARDED_FOR turns into “X-Forwarded-For”.
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/aikido/zen/request.rb', line 70 def normalized_headers @normalized_headers ||= env.slice(*BLESSED_CGI_HEADERS) .merge(env.select { |key, _| key.start_with?("HTTP_") }) .transform_keys do |header| header .delete_prefix("HTTP_") .downcase .split("_") .map(&:capitalize) .join("-") end end |
#route ⇒ Aikido::Zen::Route
Returns the framework route being requested.
39 40 41 |
# File 'lib/aikido/zen/request.rb', line 39 def route @route ||= @router.recognize(self) end |