Class: Waithook::Webhook

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(payload, logger: nil, forward_options: {}) ⇒ Webhook

Returns a new instance of Webhook.



191
192
193
194
195
196
197
198
199
200
# File 'lib/waithook.rb', line 191

def initialize(payload, logger: nil, forward_options: {})
  @message = payload
  data = JSON.parse(@message)
  @url = data['url']
  @headers = data['headers']
  @body = data['body']
  @method = data['method']
  @logger = logger
  @forward_options = forward_options
end

Instance Attribute Details

#bodyObject (readonly)

Request body (for POST, PATCH, PUT)



185
186
187
# File 'lib/waithook.rb', line 185

def body
  @body
end

#headersObject (readonly)

Hash of headers



183
184
185
# File 'lib/waithook.rb', line 183

def headers
  @headers
end

#messageObject (readonly)

Raw message from waithook server



189
190
191
# File 'lib/waithook.rb', line 189

def message
  @message
end

#methodObject (readonly)

Request method (“GET”, “POST”, “PATCH”, etc)



187
188
189
# File 'lib/waithook.rb', line 187

def method
  @method
end

#urlObject (readonly)

Original request URL, e.g. ‘/my-notification-endpoint?aaa=bbb’



181
182
183
# File 'lib/waithook.rb', line 181

def url
  @url
end

Instance Method Details

#json_bodyObject

Returns Hash. Access request body encoded as JSON (for POST, PATCH, PUT)



234
235
236
237
238
# File 'lib/waithook.rb', line 234

def json_body
  if @body
    @json_body ||= JSON.parse(@body)
  end
end

#pretty_print(pp_arg = nil, *args) ⇒ Object



202
203
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
# File 'lib/waithook.rb', line 202

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')

      begin
        require 'coderay'
        pretty_body = CodeRay.scan(pretty_body, :json).term
      rescue => error
        @logger&.debug("Error while trying to use CodeRay: #{error.message}")
      end
      return "#{JSON.pretty_generate(data_without_body)}\nBody:\n#{pretty_body}"
    rescue JSON::ParserError => error
      @logger&.debug("Error while parsing json body: #{error.message}")
    end
  end

  return @message
end

#send_to(url) ⇒ Object

Send webhook information to other HTTP server

webhook = Waithook.subscribe(path: 'my-webhooks').wait_message
webhook.send_to('http://localhost:3000/notification')


247
248
249
250
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
# File 'lib/waithook.rb', line 247

def send_to(url)
  uri = URI.parse(url)
  response = nil

  http_options = {use_ssl: uri.scheme == 'https'}.merge(@forward_options || {})
  Net::HTTP.start(uri.host, uri.port, http_options) 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