Class: Aikido::Zen::Request

Inherits:
SimpleDelegator
  • Object
show all
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

Instance Method Summary collapse

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

#actorAikido::Zen::Actor?

The current user, if set by the host app.

Returns:

See Also:



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

def actor
  @actor
end

#frameworkString (readonly)

Returns identifier of the framework handling this HTTP request.

Returns:

  • (String)

    identifier of the framework handling this HTTP request.



9
10
11
# File 'lib/aikido/zen/request.rb', line 9

def framework
  @framework
end

#routerAikido::Zen::Router (readonly)

Returns:

  • (Aikido::Zen::Router)


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_jsonObject



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_ipString

Returns the IP address of the client making the request.

Returns:

  • (String)

    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_headersHash<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”.

Returns:

  • (Hash<String, String>)


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

#routeAikido::Zen::Route

Returns the framework route being requested.

Returns:



33
34
35
# File 'lib/aikido/zen/request.rb', line 33

def route
  @route ||= @router.recognize(self)
end

#schemaAikido::Zen::Request::Schema?



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

def schema
  @schema ||= Aikido::Zen::Request::Schema.build
end