Class: WebhookRecorder::Server
- Inherits:
-
Object
- Object
- WebhookRecorder::Server
- Defined in:
- lib/webhook_recorder/server.rb
Instance Attribute Summary collapse
-
#http_expose ⇒ Object
Returns the value of attribute http_expose.
-
#http_url ⇒ Object
Returns the value of attribute http_url.
-
#https_url ⇒ Object
Returns the value of attribute https_url.
-
#log_verbose ⇒ Object
Returns the value of attribute log_verbose.
-
#port ⇒ Object
Returns the value of attribute port.
-
#recorded_reqs ⇒ Object
Returns the value of attribute recorded_reqs.
-
#response_config ⇒ Object
Returns the value of attribute response_config.
Class Method Summary collapse
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(port, response_config, http_expose = true, log_verbose = false) ⇒ Server
constructor
A new instance of Server.
- #start ⇒ Object
- #stop ⇒ Object
- #wait ⇒ Object
Constructor Details
#initialize(port, response_config, http_expose = true, log_verbose = false) ⇒ Server
Returns a new instance of Server.
12 13 14 15 16 17 18 19 |
# File 'lib/webhook_recorder/server.rb', line 12 def initialize(port, response_config, http_expose = true, log_verbose = false) self.port = port self.response_config = response_config self.recorded_reqs = [] self.http_expose = http_expose self.log_verbose = log_verbose @started = false end |
Instance Attribute Details
#http_expose ⇒ Object
Returns the value of attribute http_expose.
9 10 11 |
# File 'lib/webhook_recorder/server.rb', line 9 def http_expose @http_expose end |
#http_url ⇒ Object
Returns the value of attribute http_url.
9 10 11 |
# File 'lib/webhook_recorder/server.rb', line 9 def http_url @http_url end |
#https_url ⇒ Object
Returns the value of attribute https_url.
9 10 11 |
# File 'lib/webhook_recorder/server.rb', line 9 def https_url @https_url end |
#log_verbose ⇒ Object
Returns the value of attribute log_verbose.
9 10 11 |
# File 'lib/webhook_recorder/server.rb', line 9 def log_verbose @log_verbose end |
#port ⇒ Object
Returns the value of attribute port.
9 10 11 |
# File 'lib/webhook_recorder/server.rb', line 9 def port @port end |
#recorded_reqs ⇒ Object
Returns the value of attribute recorded_reqs.
9 10 11 |
# File 'lib/webhook_recorder/server.rb', line 9 def recorded_reqs @recorded_reqs end |
#response_config ⇒ Object
Returns the value of attribute response_config.
9 10 11 |
# File 'lib/webhook_recorder/server.rb', line 9 def response_config @response_config end |
Class Method Details
.open(port, response_config, http_expose = true, log_verbose = false) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/webhook_recorder/server.rb', line 21 def self.open(port, response_config, http_expose = true, log_verbose=false) server = new(port, response_config, http_expose, log_verbose) server.start server.wait if server.http_expose Ngrok::Tunnel.start(port: port, authtoken: ENV['NGROK_AUTH_TOKEN']) server.http_url = Ngrok::Tunnel.ngrok_url server.https_url = Ngrok::Tunnel.ngrok_url_https end yield server ensure server.recorded_reqs.clear server.stop Ngrok::Tunnel.stop end |
Instance Method Details
#call(env) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/webhook_recorder/server.rb', line 54 def call(env) path = env['PATH_INFO'] request = Rack::Request.new(env) recorded_reqs << env.merge(request_body: request.body.string).deep_transform_keys(&:downcase).with_indifferent_access if response_config[path] res = response_config[path] [res[:code], res[:headers] || {}, [res[:body] || "Missing body in response_config"]] else warn "WebhookRecorder::Server: Missing response_config for path #{path}" [404, {}, ["WebhookRecorder::Server: Missing response_config for path #{path}"]] end end |
#start ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/webhook_recorder/server.rb', line 37 def start Thread.new do = { Port: @port, Logger: WEBrick::Log.new(self.log_verbose ? STDOUT : "/dev/null"), AccessLog: [], DoNotReverseLookup: true, StartCallback: -> { @started = true } } Rack::Handler::WEBrick.run(self, ) { |s| @server = s } end end |
#stop ⇒ Object
67 68 69 |
# File 'lib/webhook_recorder/server.rb', line 67 def stop @server.shutdown if @server end |
#wait ⇒ Object
50 51 52 |
# File 'lib/webhook_recorder/server.rb', line 50 def wait Timeout.timeout(10) { sleep 0.1 until @started } end |