Class: Notion::Webhooks::RackMiddleware
- Inherits:
-
Object
- Object
- Notion::Webhooks::RackMiddleware
- Defined in:
- lib/notion/webhooks/rack_middleware.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, secret:, path: "/notion/events", &handler) ⇒ RackMiddleware
constructor
A new instance of RackMiddleware.
Constructor Details
#initialize(app, secret:, path: "/notion/events", &handler) ⇒ RackMiddleware
Returns a new instance of RackMiddleware.
8 9 10 11 12 13 |
# File 'lib/notion/webhooks/rack_middleware.rb', line 8 def initialize(app, secret:, path: "/notion/events", &handler) @app = app @secret = secret @path = path @handler = handler end |
Instance Method Details
#call(env) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/notion/webhooks/rack_middleware.rb', line 15 def call(env) return @app.call(env) unless env["PATH_INFO"] == @path && env["REQUEST_METHOD"] == "POST" payload = env.fetch("rack.input").read data = JSON.parse(payload) return verify_subscription(data) if data.key?("verification_token") valid = Signature.valid?( secret: @secret, payload: payload, signature: env["HTTP_X_NOTION_SIGNATURE"] || env["HTTP_NOTION_SIGNATURE"] ) return [401, { "content-type" => "text/plain" }, ["invalid signature"]] unless valid @handler&.call(Event.new(data)) [200, {}, []] rescue JSON::ParserError [400, { "content-type" => "text/plain" }, ["invalid JSON"]] end |