Module: Syntropy::RequestExtensions
- Defined in:
- lib/syntropy/request_extensions.rb
Overview
Extensions for the Qeweney::Request class
Instance Attribute Summary collapse
-
#route ⇒ Object
Returns the value of attribute route.
-
#route_params ⇒ Object
readonly
Returns the value of attribute route_params.
Instance Method Summary collapse
-
#ctx ⇒ Object
Returns the request context.
-
#get_form_data ⇒ Hash
Reads the request body and returns form data.
-
#initialize(headers, adapter) ⇒ Object
Initializes request with additional fields.
-
#respond_by_http_method(map) ⇒ void
Responds according to the given map.
-
#respond_on_get(body, headers = {}) ⇒ void
Responds to GET requests with the given body and headers.
-
#respond_on_post(body, headers = {}) ⇒ void
Responds to POST requests with the given body and headers.
-
#setup_mock_request ⇒ Object
Sets up mock request additional fields.
-
#validate(value, *clauses) ⇒ any
Validates and optionally converts a value against the given clauses.
-
#validate_http_method(*accepted) ⇒ String
Checks the request’s HTTP method against the given accepted values.
-
#validate_param(name, *clauses) ⇒ any
Validates and optionally converts request parameter value for the given parameter name against the given clauses.
Instance Attribute Details
#route ⇒ Object
Returns the value of attribute route.
9 10 11 |
# File 'lib/syntropy/request_extensions.rb', line 9 def route @route end |
#route_params ⇒ Object (readonly)
Returns the value of attribute route_params.
8 9 10 |
# File 'lib/syntropy/request_extensions.rb', line 8 def route_params @route_params end |
Instance Method Details
#ctx ⇒ Object
Returns the request context
28 29 30 |
# File 'lib/syntropy/request_extensions.rb', line 28 def ctx @ctx ||= {} end |
#get_form_data ⇒ Hash
Reads the request body and returns form data.
146 147 148 149 150 151 152 153 154 155 |
# File 'lib/syntropy/request_extensions.rb', line 146 def get_form_data body = read if !body || body.empty? raise Syntropy::Error.new(Qeweney::Status::BAD_REQUEST, 'Missing form data') end Qeweney::Request.parse_form_data(body, headers) rescue Qeweney::BadRequestError raise Syntropy::Error.new(Qeweney::Status::BAD_REQUEST, 'Invalid form data') end |
#initialize(headers, adapter) ⇒ Object
Initializes request with additional fields
12 13 14 15 16 17 18 |
# File 'lib/syntropy/request_extensions.rb', line 12 def initialize(headers, adapter) @headers = headers @adapter = adapter @route = nil @route_params = {} @ctx = nil end |
#respond_by_http_method(map) ⇒ void
This method returns an undefined value.
Responds according to the given map. The given map defines the responses for each method. The value for each method is either an array containing the body and header values to use as response, or a proc returning such an array. For example:
req.respond_by_http_method(
'head' => [nil, headers],
'get' => -> { [IO.read(fn), headers] }
)
If the request’s method is not included in the given map, an exception is raised.
59 60 61 62 63 64 65 66 |
# File 'lib/syntropy/request_extensions.rb', line 59 def respond_by_http_method(map) value = map[self.method] raise Syntropy::Error.method_not_allowed if !value value = value.() if value.is_a?(Proc) (body, headers) = value respond(body, headers) end |
#respond_on_get(body, headers = {}) ⇒ void
This method returns an undefined value.
Responds to GET requests with the given body and headers. Otherwise raises an exception.
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/syntropy/request_extensions.rb', line 74 def respond_on_get(body, headers = {}) case self.method when 'head' respond(nil, headers) when 'get' respond(body, headers) else raise Syntropy::Error.method_not_allowed end end |
#respond_on_post(body, headers = {}) ⇒ void
This method returns an undefined value.
Responds to POST requests with the given body and headers. Otherwise raises an exception.
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/syntropy/request_extensions.rb', line 91 def respond_on_post(body, headers = {}) case self.method when 'head' respond(nil, headers) when 'post' respond(body, headers) else raise Syntropy::Error.method_not_allowed end end |
#setup_mock_request ⇒ Object
Sets up mock request additional fields
21 22 23 24 25 |
# File 'lib/syntropy/request_extensions.rb', line 21 def setup_mock_request @route = nil @route_params = {} @ctx = nil end |
#validate(value, *clauses) ⇒ any
Validates and optionally converts a value against the given clauses. If no clauses are given, verifies the parameter value is not nil. A clause can be a class, such as String, Integer, etc, in which case the value is converted into the corresponding value. A clause can also be a range, for verifying the value is within the range. A clause can also be an array of two or more clauses, at least one of which should match the value. If the validation fails, an exception is raised.
131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/syntropy/request_extensions.rb', line 131 def validate(value, *clauses) raise Syntropy::ValidationError, 'Validation error' if clauses.empty? && !value clauses.each do |c| valid = param_is_valid?(value, c) raise(Syntropy::ValidationError, 'Validation error') if !valid value = param_convert(value, c) end value end |
#validate_http_method(*accepted) ⇒ String
Checks the request’s HTTP method against the given accepted values. If not included in the accepted values, raises an exception. Otherwise, returns the request’s HTTP method.
38 39 40 41 42 |
# File 'lib/syntropy/request_extensions.rb', line 38 def validate_http_method(*accepted) raise Syntropy::Error.method_not_allowed if !accepted.include?(method) method end |
#validate_param(name, *clauses) ⇒ any
Validates and optionally converts request parameter value for the given parameter name against the given clauses. If no clauses are given, verifies the parameter value is not nil. A clause can be a class, such as String, Integer, etc, in which case the value is converted into the corresponding value. A clause can also be a range, for verifying the value is within the range. A clause can also be an array of two or more clauses, at least one of which should match the value. If the validation fails, an exception is raised. Example:
height = req.validate_param(:height, Integer, 1..100)
116 117 118 |
# File 'lib/syntropy/request_extensions.rb', line 116 def validate_param(name, *clauses) validate(query[name], *clauses) end |