Class: Cuprum::Rails::Request

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

Overview

Wraps a web request with a generic interface.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context: nil, **properties) ⇒ Request

Returns a new instance of Request.

Parameters:

  • context (Object) (defaults to: nil)

    the controller or request context.

  • properties (Hash)

    a customizable set of options

Options Hash (**properties):

  • :action_name (Symbol)

    the name of the called action.

  • :authorization (String, nil)

    the authorization header, if any.

  • :body_params (Hash<String, Object>)

    the parameters from the request body.

  • :controller_name (String)

    the name of the controller.

  • :format (Symbol)

    the request format, e.g. :html or :json.

  • :headers (Hash<String, String>)

    the request headers.

  • :member_action (Boolean)

    true if the request is for a resource member action; otherwise false.

  • :method (Symbol)

    the HTTP method used for the request.

  • :params (Hash<String, Object>)

    the merged GET and POST parameters.

  • :query_params (Hash<String, Object>)

    the query parameters.



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_nameSymbol

Returns the name of the called action.

Returns:

  • (Symbol)

    the name of the called action.



96
# File 'lib/cuprum/rails/request.rb', line 96

property :action_name

#authorizationString?

Returns the authorization header, if any.

Returns:

  • (String, nil)

    the authorization header, if any.



100
# File 'lib/cuprum/rails/request.rb', line 100

property :authorization

#body_paramsHash<String, Object> Also known as: body_parameters

Returns the parameters from the request body.

Returns:

  • (Hash<String, Object>)

    the parameters from the request body.



104
# File 'lib/cuprum/rails/request.rb', line 104

property :body_params

#contextObject (readonly)

Returns the controller or request context.

Returns:

  • (Object)

    the controller or request context.



89
90
91
# File 'lib/cuprum/rails/request.rb', line 89

def context
  @context
end

#controller_nameString

Returns the name of the controller.

Returns:

  • (String)

    the name of the controller.



110
# File 'lib/cuprum/rails/request.rb', line 110

property :controller_name

#headersHash<String, String>

Returns the request headers.

Returns:

  • (Hash<String, String>)

    the request headers.



118
# File 'lib/cuprum/rails/request.rb', line 118

property :headers

#methodSymbol

Returns the HTTP method used for the request.

Returns:

  • (Symbol)

    the HTTP method used for the request.



122
# File 'lib/cuprum/rails/request.rb', line 122

property :http_method

#paramsHash<String, Object> Also known as: parameters

Returns The merged GET and POST parameters.

Returns:

  • (Hash<String, Object>)

    The merged GET and POST parameters.



126
# File 'lib/cuprum/rails/request.rb', line 126

property :params

#pathString

Returns the relative path of the request, including params.

Returns:

  • (String)

    the relative path of the request, including params.



132
# File 'lib/cuprum/rails/request.rb', line 132

property :path

#path_paramsHash<String, Object> Also known as: path_parameters

Returns the path parameters.

Returns:

  • (Hash<String, Object>)

    the path parameters.



136
# File 'lib/cuprum/rails/request.rb', line 136

property :path_params

#propertiesHash<Symbol, Object> (readonly)

Returns the properties of the request.

Returns:

  • (Hash<Symbol, Object>)

    the properties of the request.



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

def properties
  @properties
end

#query_paramsHash<String, Object> Also known as: query_parameters

Returns the query parameters.

Returns:

  • (Hash<String, Object>)

    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.

Parameters:

  • request (ActionDispatch::Request)

    The native request to build.

Returns:



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:, **options) # 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.authorization,
    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,
    **options
  )
end

Instance Method Details

#[](property_name) ⇒ Object

Returns the value of the property.

Parameters:

  • property_name (String, Symbol)

    the name of the property.

Returns:

  • (Object)

    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

Parameters:

  • property_name (String, Symbol)

    the name of the property.

  • value (Object)

    the value to assign to the property.



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.

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.

Returns:

  • (Boolean)

    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_sessionActionDispatch::Request::Session

Returns the native session object.

Returns:

  • (ActionDispatch::Request::Session)

    the native session object.



170
171
172
# File 'lib/cuprum/rails/request.rb', line 170

def native_session
  context&.session
end