Class: Hooksmith::Request

Inherits:
Object
  • Object
show all
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.).

Examples:

Creating a request from Rails controller

request = Hooksmith::Request.new(
  headers: request.headers.to_h,
  body: request.raw_post,
  method: request.request_method,
  path: request.path
)

Creating a request from Rack env

request = Hooksmith::Request.from_rack_env(env)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers:, body:, method: 'POST', path: '/', payload: nil) ⇒ Request

Initializes a new Request.

Parameters:

  • headers (Hash)

    the request headers

  • body (String)

    the raw request body

  • method (String) (defaults to: 'POST')

    the HTTP method

  • path (String) (defaults to: '/')

    the request path

  • payload (Hash) (defaults to: nil)

    the parsed payload (optional)



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

#bodyString (readonly)

Returns the raw request body.

Returns:

  • (String)

    the raw request body



24
25
26
# File 'lib/hooksmith/request.rb', line 24

def body
  @body
end

#headersHash (readonly)

Returns the request headers.

Returns:

  • (Hash)

    the request headers



22
23
24
# File 'lib/hooksmith/request.rb', line 22

def headers
  @headers
end

#methodString (readonly)

Returns the HTTP method (GET, POST, etc.).

Returns:

  • (String)

    the HTTP method (GET, POST, etc.)



26
27
28
# File 'lib/hooksmith/request.rb', line 26

def method
  @method
end

#pathString (readonly)

Returns the request path.

Returns:

  • (String)

    the request path



28
29
30
# File 'lib/hooksmith/request.rb', line 28

def path
  @path
end

#payloadHash (readonly)

Returns the parsed payload (optional, for convenience).

Returns:

  • (Hash)

    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.

Parameters:

  • env (Hash)

    the Rack environment

Returns:

  • (Request)

    a new Request instance



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.

Parameters:

  • name (String)

    the header name

Returns:

  • (String, nil)

    the header value or nil if not found



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).

Parameters:

  • name (String)

    the header name

Returns:

  • (String, nil)

    the header value or nil if not found



68
69
70
71
# File 'lib/hooksmith/request.rb', line 68

def header(name)
  normalized_name = normalize_header_name(name)
  @headers[normalized_name]
end