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
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.
20 21 22 23 24 25 |
# File 'lib/aikido/zen/request.rb', line 20 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 |
Instance Method Details
#__setobj__(delegate) ⇒ Object
:nodoc:
27 28 29 30 |
# File 'lib/aikido/zen/request.rb', line 27 def __setobj__(delegate) # :nodoc: super @route = @normalized_header = nil end |
#as_json ⇒ Object
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/aikido/zen/request.rb', line 73 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.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/aikido/zen/request.rb', line 43 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”.
64 65 66 67 68 69 70 71 |
# File 'lib/aikido/zen/request.rb', line 64 def normalized_headers @normalized_headers ||= env.slice(*BLESSED_CGI_HEADERS) .merge(env.select { |key, _| key.start_with?("HTTP_") }) .transform_keys { |header| name = header.sub(/^HTTP_/, "").downcase name.split("_").map { |part| part[0].upcase + part[1..] }.join("-") } end |
#route ⇒ Aikido::Zen::Route
Returns the framework route being requested.
33 34 35 |
# File 'lib/aikido/zen/request.rb', line 33 def route @route ||= @router.recognize(self) end |