Class: Webhookdb::WebhookResponse
- Inherits:
-
TypedStruct
- Object
- TypedStruct
- Webhookdb::WebhookResponse
- Defined in:
- lib/webhookdb/webhook_response.rb
Constant Summary collapse
- SECRET_HEADER_RFC =
The standard WebhookDB Secret header, in RFC header format (My-Hdr).
"Whdb-Webhook-Secret"
- SECRET_HEADER_RACK =
The standard WebhookDB secret header, in Rack header format (HTTP_MY_HDR).
"HTTP_WHDB_WEBHOOK_SECRET"
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#reason ⇒ Object
readonly
Returns the value of attribute reason.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Class Method Summary collapse
- .error(reason, status: 401) ⇒ Object
-
.for_standard_secret(request, webhook_secret, ok_status: 202) ⇒ Webhookdb::WebhookResponse
Compare the value of the SECRET_HEADER_RACK in the request header.
- .ok(json: {o: "k"}, status: 202) ⇒ Object
Instance Method Summary collapse
-
#initialize(status:, body: nil, json: nil, reason: nil, headers: {}) ⇒ WebhookResponse
constructor
A new instance of WebhookResponse.
- #to_rack ⇒ Array<Integer, Hash, String>
Methods inherited from TypedStruct
#[], #_apply, #_defaults, #as_json, #change
Constructor Details
#initialize(status:, body: nil, json: nil, reason: nil, headers: {}) ⇒ WebhookResponse
Returns a new instance of WebhookResponse.
36 37 38 39 40 41 42 43 44 |
# File 'lib/webhookdb/webhook_response.rb', line 36 def initialize(status:, body: nil, json: nil, reason: nil, headers: {}) raise "Reason must be provided if returning an error" if !reason && status >= 400 if json body = json.to_json headers["Content-Type"] = "application/json" end raise ":body or :json must be provided" if body.nil? super(status:, body:, headers:, reason:) end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
23 24 25 |
# File 'lib/webhookdb/webhook_response.rb', line 23 def body @body end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
23 24 25 |
# File 'lib/webhookdb/webhook_response.rb', line 23 def headers @headers end |
#reason ⇒ Object (readonly)
Returns the value of attribute reason.
23 24 25 |
# File 'lib/webhookdb/webhook_response.rb', line 23 def reason @reason end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
23 24 25 |
# File 'lib/webhookdb/webhook_response.rb', line 23 def status @status end |
Class Method Details
.error(reason, status: 401) ⇒ Object
28 29 30 |
# File 'lib/webhookdb/webhook_response.rb', line 28 def self.error(reason, status: 401) return self.new(status:, json: {message: reason}, reason:) end |
.for_standard_secret(request, webhook_secret, ok_status: 202) ⇒ Webhookdb::WebhookResponse
Compare the value of the SECRET_HEADER_RACK in the request header.
15 16 17 18 19 20 21 |
# File 'lib/webhookdb/webhook_response.rb', line 15 def self.for_standard_secret(request, webhook_secret, ok_status: 202) hdr_secret = request.env[SECRET_HEADER_RACK] return self.error("missing secret header") if hdr_secret.nil? matches = ActiveSupport::SecurityUtils.secure_compare(webhook_secret, hdr_secret) return self.error("secret mismatch") unless matches return self.ok(status: ok_status) end |
.ok(json: {o: "k"}, status: 202) ⇒ Object
32 33 34 |
# File 'lib/webhookdb/webhook_response.rb', line 32 def self.ok(json: {o: "k"}, status: 202) return self.new(status:, json:) end |
Instance Method Details
#to_rack ⇒ Array<Integer, Hash, String>
47 48 49 |
# File 'lib/webhookdb/webhook_response.rb', line 47 def to_rack return [self.status, self.headers, self.body] end |