Class: Waithook::Webhook
- Inherits:
-
Object
- Object
- Waithook::Webhook
- Defined in:
- lib/waithook.rb
Overview
Represent incoming request to waithook, that was send to client as JSON via websocket connection
Constant Summary collapse
- SKIP_FORWARD_HEADERS =
%w[host content-length connection accept-encoding accept content-encoding]
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Request body (for POST, PATCH, PUT).
-
#headers ⇒ Object
readonly
Hash of headers.
-
#message ⇒ Object
readonly
Raw message from waithook server.
-
#method ⇒ Object
readonly
Request method (“GET”, “POST”, “PATCH”, etc).
-
#url ⇒ Object
readonly
Original request URL, e.g.
Instance Method Summary collapse
-
#initialize(payload, logger: nil, forward_options: {}, enable_colors: true) ⇒ Webhook
constructor
A new instance of Webhook.
-
#json_body ⇒ Object
Returns Hash.
- #pretty_print(pp_arg = nil, *args) ⇒ Object
-
#send_to(url) ⇒ Object
Send webhook information to other HTTP server.
Constructor Details
#initialize(payload, logger: nil, forward_options: {}, enable_colors: true) ⇒ Webhook
Returns a new instance of Webhook.
192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/waithook.rb', line 192 def initialize(payload, logger: nil, forward_options: {}, enable_colors: true) @message = payload data = JSON.parse(@message) @url = data['url'] @headers = data['headers'] @body = data['body'] @method = data['method'] @logger = logger @forward_options = @enable_colors = enable_colors end |
Instance Attribute Details
#body ⇒ Object (readonly)
Request body (for POST, PATCH, PUT)
186 187 188 |
# File 'lib/waithook.rb', line 186 def body @body end |
#headers ⇒ Object (readonly)
Hash of headers
184 185 186 |
# File 'lib/waithook.rb', line 184 def headers @headers end |
#message ⇒ Object (readonly)
Raw message from waithook server
190 191 192 |
# File 'lib/waithook.rb', line 190 def @message end |
#method ⇒ Object (readonly)
Request method (“GET”, “POST”, “PATCH”, etc)
188 189 190 |
# File 'lib/waithook.rb', line 188 def method @method end |
#url ⇒ Object (readonly)
Original request URL, e.g. ‘/my-notification-endpoint?aaa=bbb’
182 183 184 |
# File 'lib/waithook.rb', line 182 def url @url end |
Instance Method Details
#json_body ⇒ Object
Returns Hash. Access request body encoded as JSON (for POST, PATCH, PUT)
238 239 240 241 242 |
# File 'lib/waithook.rb', line 238 def json_body if @body @json_body ||= JSON.parse(@body) end end |
#pretty_print(pp_arg = nil, *args) ⇒ Object
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/waithook.rb', line 204 def pretty_print(pp_arg = nil, *args) return super if pp_arg && defined?(super) # method from 'pp' library has same name if !@body @logger&.debug("Error: Waithook::Webhook has no @body") return @message end if @body.start_with?('{') && @body.end_with?('}') || @body.start_with?('[') && @body.end_with?(']') begin body_data = JSON.parse(body) pretty_body = JSON.pretty_generate(body_data) data_without_body = JSON.parse(@message) data_without_body.delete('body') if @enable_colors begin require 'coderay' pretty_body = CodeRay.scan(pretty_body, :json).term rescue => error @logger&.debug("Error while trying to use CodeRay: #{error.}") end end return "#{JSON.pretty_generate(data_without_body)}\nBody:\n#{pretty_body}" rescue JSON::ParserError => error @logger&.debug("Error while parsing json body: #{error.}") end end return @message end |
#send_to(url) ⇒ Object
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/waithook.rb', line 251 def send_to(url) uri = URI.parse(url) response = nil = {use_ssl: uri.scheme == 'https'}.merge(@forward_options || {}) Net::HTTP.start(uri.host, uri.port, ) do |http| http_klass = case method when "GET" then Net::HTTP::Get when "POST" then Net::HTTP::Post when "PUT" then Net::HTTP::Put when "PATCH" then Net::HTTP::Patch when "HEAD" then Net::HTTP::Head when "DELETE" then Net::HTTP::Delete when "MOVE" then Net::HTTP::Move when "COPY" then Net::HTTP::Copy else Net::HTTP::Post end request = http_klass.new(uri) headers.each do |key, value| if !SKIP_FORWARD_HEADERS.include?(key.to_s.downcase) request[key] = value end end if body request.body = body end response = http.request(request) end response end |