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) ⇒ Webhook

Returns a new instance of Webhook.



187
188
189
190
191
192
193
194
195
# File 'lib/waithook.rb', line 187

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

Instance Attribute Details

#bodyObject (readonly)

Request body (for POST, PATCH, PUT)



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

def body
  @body
end

#headersObject (readonly)

Hash of headers



179
180
181
# File 'lib/waithook.rb', line 179

def headers
  @headers
end

#messageObject (readonly)

Raw message from waithook server



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

def message
  @message
end

#methodObject (readonly)

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



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

def method
  @method
end

#urlObject (readonly)

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



177
178
179
# File 'lib/waithook.rb', line 177

def url
  @url
end

Instance Method Details

#json_bodyObject

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



222
223
224
225
226
# File 'lib/waithook.rb', line 222

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

#pretty_printObject



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/waithook.rb', line 197

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


235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/waithook.rb', line 235

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

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