Class: TaskMaster::WebhookController

Inherits:
ApplicationController show all
Defined in:
app/controllers/task_master/webhook_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/controllers/task_master/webhook_controller.rb', line 4

def create
  response = JSON.parse(request.body.read)

  request_instance = nil

  if response["key"].nil?
    head :bad_request and return
  end

  # Fancy Hands uses a single URL for all webhooks.
  # We need to iterate through the request classes to find one
  # that has the key from the response.
  [CustomRequest].each do |request_class|
    break if request_instance = request_class.find_by(key: response["key"])
  end

  if request_instance
    request_instance.responses << response
    request_instance.save!
  end

  head :ok

rescue JSON::ParserError
  head :bad_request
end