Class: Cuprum::Rails::Request
- Inherits:
-
Object
- Object
- Cuprum::Rails::Request
- Defined in:
- lib/cuprum/rails/request.rb
Overview
Wraps a web request with a generic interface.
Instance Attribute Summary collapse
-
#action_name ⇒ Symbol
The name of the called action.
-
#authorization ⇒ String?
The authorization header, if any.
-
#body_params ⇒ Hash<String, Object>
(also: #body_parameters)
The parameters from the request body.
-
#context ⇒ Object
readonly
The controller or request context.
-
#controller_name ⇒ String
The name of the controller.
-
#headers ⇒ Hash<String, String>
The request headers.
-
#method ⇒ Symbol
The HTTP method used for the request.
-
#params ⇒ Hash<String, Object>
(also: #parameters)
The merged GET and POST parameters.
-
#path ⇒ String
The relative path of the request, including params.
-
#path_params ⇒ Hash<String, Object>
(also: #path_parameters)
The path parameters.
-
#properties ⇒ Hash<Symbol, Object>
readonly
The properties of the request.
-
#query_params ⇒ Hash<String, Object>
(also: #query_parameters)
The query parameters.
Class Method Summary collapse
-
.build(request:, **options) ⇒ Cuprum::Rails::Request
Generates a Request from a native Rails request.
Instance Method Summary collapse
-
#[](property_name) ⇒ Object
The value of the property.
- #[]=(property_name, value) ⇒ Object
-
#format(value) ⇒ Object
The request format, e.g.
-
#initialize(context: nil, **properties) ⇒ Request
constructor
A new instance of Request.
-
#member_action? ⇒ Boolean
True if the request is for a resource member action; otherwise false.
-
#native_session ⇒ ActionDispatch::Request::Session
The native session object.
Constructor Details
#initialize(context: nil, **properties) ⇒ Request
Returns a new instance of Request.
83 84 85 86 |
# File 'lib/cuprum/rails/request.rb', line 83 def initialize(context: nil, **properties) @context = context @properties = properties end |
Instance Attribute Details
#action_name ⇒ Symbol
Returns the name of the called action.
96 |
# File 'lib/cuprum/rails/request.rb', line 96 property :action_name |
#authorization ⇒ String?
Returns the authorization header, if any.
100 |
# File 'lib/cuprum/rails/request.rb', line 100 property :authorization |
#body_params ⇒ Hash<String, Object> Also known as: body_parameters
Returns the parameters from the request body.
104 |
# File 'lib/cuprum/rails/request.rb', line 104 property :body_params |
#context ⇒ Object (readonly)
Returns the controller or request context.
89 90 91 |
# File 'lib/cuprum/rails/request.rb', line 89 def context @context end |
#controller_name ⇒ String
Returns the name of the controller.
110 |
# File 'lib/cuprum/rails/request.rb', line 110 property :controller_name |
#headers ⇒ Hash<String, String>
Returns the request headers.
118 |
# File 'lib/cuprum/rails/request.rb', line 118 property :headers |
#method ⇒ Symbol
Returns the HTTP method used for the request.
122 |
# File 'lib/cuprum/rails/request.rb', line 122 property :http_method |
#params ⇒ Hash<String, Object> Also known as: parameters
Returns The merged GET and POST parameters.
126 |
# File 'lib/cuprum/rails/request.rb', line 126 property :params |
#path ⇒ String
Returns the relative path of the request, including params.
132 |
# File 'lib/cuprum/rails/request.rb', line 132 property :path |
#path_params ⇒ Hash<String, Object> Also known as: path_parameters
Returns the path parameters.
136 |
# File 'lib/cuprum/rails/request.rb', line 136 property :path_params |
#properties ⇒ Hash<Symbol, Object> (readonly)
Returns the properties of the request.
92 93 94 |
# File 'lib/cuprum/rails/request.rb', line 92 def properties @properties end |
#query_params ⇒ Hash<String, Object> Also known as: query_parameters
Returns the query parameters.
142 |
# File 'lib/cuprum/rails/request.rb', line 142 property :query_params |
Class Method Details
.build(request:, **options) ⇒ Cuprum::Rails::Request
Generates a Request from a native Rails request.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/cuprum/rails/request.rb', line 21 def build(request:, **) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength body_params = request.request_parameters query_params = request.query_parameters path_params = filter_path_parameters(request.path_parameters) new( action_name: request.params['action']&.intern, authorization: request., body_params: body_params, controller_name: request.params['controller'], format: request.format.symbol, headers: filter_headers(request.headers), http_method: request.request_method_symbol, params: body_params.merge(query_params).merge(path_params), path: request.fullpath, path_params: path_params, query_params: query_params, ** ) end |
Instance Method Details
#[](property_name) ⇒ Object
Returns the value of the property.
149 150 151 152 153 |
# File 'lib/cuprum/rails/request.rb', line 149 def [](property_name) validate_property_name!(property_name) @properties[property_name.intern] end |
#[]=(property_name, value) ⇒ Object
157 158 159 160 161 |
# File 'lib/cuprum/rails/request.rb', line 157 def []=(property_name, value) validate_property_name!(property_name) @properties[property_name.intern] = value end |
#format=(value) ⇒ Object
Returns the request format, e.g. :html or :json.
114 |
# File 'lib/cuprum/rails/request.rb', line 114 property :format |
#member_action? ⇒ Boolean
Returns true if the request is for a resource member action; otherwise false.
165 166 167 |
# File 'lib/cuprum/rails/request.rb', line 165 def member_action? !!@properties[:member_action] end |
#native_session ⇒ ActionDispatch::Request::Session
Returns the native session object.
170 171 172 |
# File 'lib/cuprum/rails/request.rb', line 170 def native_session context&.session end |