Class: Moogle::Commands::PushWebhookPing

Inherits:
Object
  • Object
show all
Includes:
Serf::Command
Defined in:
lib/moogle/commands/push_webhook_ping.rb

Overview

Posts a webhook callback.

Instance Method Summary collapse

Instance Method Details

#callObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/moogle/commands/push_webhook_ping.rb', line 20

def call
  # Options pulled from the delegated object
  ssl_opt = opts :ssl_opt

  webhook_uri = Addressable::URI.parse request.webhook_uri
  secret = request.secret

  # Build request and make it.
  conn = Faraday.new url: webhook_uri.origin, ssl: ssl_opt do |b|
    # Signs the post body, adds 'sig' to query parameters.
    b.use Rafaday::BodySigningMiddleware, secret: secret if secret

    b.response :raise_error
    b.adapter  :net_http
  end
  results = conn.post webhook_uri.path, request.data

  # Checks for errors that didn't get caught in the response :raise_error.
  case results.status
  when 200..299
  else
    raise "PushWebhookPing (#{request.uuid}) failed: #{results.status}"
  end

  event_class = opts :event_class, Moogle::Events::WebhookPingPushed
  return event_class.new(
    request.create_child_uuids.merge(
      message_origin: request.message_origin,
      target_id: request.target_id,
      webhook_uri: request.webhook_uri))
rescue => e
  e.extend Moogle::Error
  raise e
end