Class: Hooksmith::Request
- Inherits:
-
Object
- Object
- Hooksmith::Request
- Defined in:
- lib/hooksmith/request.rb
Overview
Wrapper for incoming webhook request data.
This class provides a consistent interface for accessing request data regardless of the underlying web framework (Rails, Rack, etc.).
Instance Attribute Summary collapse
-
#body ⇒ String
readonly
The raw request body.
-
#headers ⇒ Hash
readonly
The request headers.
-
#method ⇒ String
readonly
The HTTP method (GET, POST, etc.).
-
#path ⇒ String
readonly
The request path.
-
#payload ⇒ Hash
readonly
The parsed payload (optional, for convenience).
Class Method Summary collapse
-
.from_rack_env(env) ⇒ Request
Creates a Request from a Rack environment hash.
Instance Method Summary collapse
-
#[](name) ⇒ String?
Alias for #header.
-
#header(name) ⇒ String?
Gets a header value by name (case-insensitive).
-
#initialize(headers:, body:, method: 'POST', path: '/', payload: nil) ⇒ Request
constructor
Initializes a new Request.
Constructor Details
#initialize(headers:, body:, method: 'POST', path: '/', payload: nil) ⇒ Request
Initializes a new Request.
39 40 41 42 43 44 45 |
# File 'lib/hooksmith/request.rb', line 39 def initialize(headers:, body:, method: 'POST', path: '/', payload: nil) @headers = normalize_headers(headers) @body = body.to_s @method = method.to_s.upcase @path = path.to_s @payload = payload end |
Instance Attribute Details
#body ⇒ String (readonly)
Returns the raw request body.
24 25 26 |
# File 'lib/hooksmith/request.rb', line 24 def body @body end |
#headers ⇒ Hash (readonly)
Returns the request headers.
22 23 24 |
# File 'lib/hooksmith/request.rb', line 22 def headers @headers end |
#method ⇒ String (readonly)
Returns the HTTP method (GET, POST, etc.).
26 27 28 |
# File 'lib/hooksmith/request.rb', line 26 def method @method end |
#path ⇒ String (readonly)
Returns the request path.
28 29 30 |
# File 'lib/hooksmith/request.rb', line 28 def path @path end |
#payload ⇒ Hash (readonly)
Returns the parsed payload (optional, for convenience).
30 31 32 |
# File 'lib/hooksmith/request.rb', line 30 def payload @payload end |
Class Method Details
.from_rack_env(env) ⇒ Request
Creates a Request from a Rack environment hash.
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/hooksmith/request.rb', line 51 def self.from_rack_env(env) headers = extract_headers_from_rack(env) body = env['rack.input']&.read || '' env['rack.input']&.rewind new( headers:, body:, method: env['REQUEST_METHOD'], path: env['PATH_INFO'] ) end |
Instance Method Details
#[](name) ⇒ String?
Alias for #header.
77 78 79 |
# File 'lib/hooksmith/request.rb', line 77 def [](name) header(name) end |
#header(name) ⇒ String?
Gets a header value by name (case-insensitive).
68 69 70 71 |
# File 'lib/hooksmith/request.rb', line 68 def header(name) normalized_name = normalize_header_name(name) @headers[normalized_name] end |