Class: Rubino::API::Request
- Inherits:
-
Object
- Object
- Rubino::API::Request
- Defined in:
- lib/rubino/api/request.rb
Overview
Operation-facing view over the Rack env: URL captures, parsed JSON body, query string, headers, and a dry-schema validation helper.
Body comes from env (set by JsonParser middleware), so operations never touch rack.input directly.
request.params # URL captures (e.g. { "id" => "abc" })
request.body # parsed JSON body (Hash)
request.validate!(schema) # runs dry-schema, raises ValidationError on fail
request.header("X-Foo") # case-insensitive header lookup
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Instance Method Summary collapse
-
#body ⇒ Hash
Parsed JSON body, or {} when none.
-
#header(name) ⇒ Object
Case-insensitive header lookup; “X-Foo” becomes HTTP_X_FOO.
-
#initialize(env, params) ⇒ Request
constructor
A new instance of Request.
- #query ⇒ Object
-
#validate!(schema) ⇒ Hash
Runs the body through a dry-schema and returns the coerced hash.
Constructor Details
#initialize(env, params) ⇒ Request
Returns a new instance of Request.
18 19 20 21 |
# File 'lib/rubino/api/request.rb', line 18 def initialize(env, params) @env = env @params = params end |
Instance Attribute Details
#env ⇒ Object (readonly)
Returns the value of attribute env.
23 24 25 |
# File 'lib/rubino/api/request.rb', line 23 def env @env end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
23 24 25 |
# File 'lib/rubino/api/request.rb', line 23 def params @params end |
Instance Method Details
#body ⇒ Hash
Returns parsed JSON body, or {} when none.
26 27 28 |
# File 'lib/rubino/api/request.rb', line 26 def body @env.fetch("rubino.json", {}) end |
#header(name) ⇒ Object
Case-insensitive header lookup; “X-Foo” becomes HTTP_X_FOO.
31 32 33 34 |
# File 'lib/rubino/api/request.rb', line 31 def header(name) key = "HTTP_#{name.upcase.tr("-", "_")}" @env[key] end |
#query ⇒ Object
36 37 38 |
# File 'lib/rubino/api/request.rb', line 36 def query @query ||= Rack::Utils.parse_nested_query(@env["QUERY_STRING"].to_s) end |
#validate!(schema) ⇒ Hash
Runs the body through a dry-schema and returns the coerced hash. dry-schema is used only at the HTTP boundary; internals trust their types.
46 47 48 49 50 51 |
# File 'lib/rubino/api/request.rb', line 46 def validate!(schema) result = schema.call(body) raise ValidationError.new("invalid request body", details: { errors: result.errors.to_h }) if result.failure? result.to_h end |