Class: Attio::WebhookServer
- Inherits:
-
Object
- Object
- Attio::WebhookServer
- Defined in:
- lib/attio/webhooks.rb
Overview
Webhook server for development/testing
Instance Attribute Summary collapse
- #events ⇒ Object readonly
- #port ⇒ Object readonly
- #webhooks ⇒ Object readonly
Instance Method Summary collapse
-
#initialize(port: 3001, secret: "test_secret") ⇒ WebhookServer
constructor
A new instance of WebhookServer.
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(port: 3001, secret: "test_secret") ⇒ WebhookServer
Returns a new instance of WebhookServer.
177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/attio/webhooks.rb', line 177 def initialize(port: 3001, secret: "test_secret") @port = port @webhooks = Webhooks.new(secret: secret) @events = [] @server = nil begin require "webrick" rescue LoadError raise "Please add 'webrick' to your Gemfile to use WebhookServer" end end |
Instance Attribute Details
#events ⇒ Object (readonly)
175 176 177 |
# File 'lib/attio/webhooks.rb', line 175 def events @events end |
#port ⇒ Object (readonly)
175 176 177 |
# File 'lib/attio/webhooks.rb', line 175 def port @port end |
#webhooks ⇒ Object (readonly)
175 176 177 |
# File 'lib/attio/webhooks.rb', line 175 def webhooks @webhooks end |
Instance Method Details
#start ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/attio/webhooks.rb', line 190 def start @server = WEBrick::HTTPServer.new(Port: @port, Logger: WEBrick::Log.new(File::NULL)) @server.mount_proc "/webhooks" do |req, res| if req.request_method == "POST" begin event = @webhooks.process(req.body, req.header) @events << event res.status = 200 res.body = JSON.generate(status: "ok", event_id: event.id) rescue StandardError => e res.status = 400 res.body = JSON.generate(error: e.) end else res.status = 405 res.body = JSON.generate(error: "Method not allowed") end end trap("INT") { stop } puts "Webhook server listening on http://localhost:#{@port}/webhooks" @server.start end |
#stop ⇒ Object
216 217 218 |
# File 'lib/attio/webhooks.rb', line 216 def stop @server&.shutdown end |