Class: Nylas::Tunnel

Inherits:
Object
  • Object
show all
Defined in:
lib/nylas/services/tunnel.rb

Overview

Class containing methods to spin up a developmental websocket connection to test webhooks

Class Method Summary collapse

Class Method Details

.open_webhook_tunnel(api, config = {}) ⇒ Object

Open a webhook tunnel and register it with the Nylas API

  1. Creates a UUID

  2. Opens a websocket connection to Nylas’ webhook forwarding service, with the UUID as a header

  3. Creates a new webhook pointed at the forwarding service with the UUID as the path

When an event is received by the forwarding service, it will push directly to this websocket connection

Parameters:

  • api (Nylas::API)

    The configured Nylas API client

  • config (Hash) (defaults to: {})

    Configuration for the webhook tunnel, including callback functions, region, and events to subscribe to



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/nylas/services/tunnel.rb', line 19

def self.open_webhook_tunnel(api, config = {})
  tunnel_id = SecureRandom.uuid
  triggers = config[:triggers] || WebhookTrigger.constants(false).map { |c| WebhookTrigger.const_get c }
  region = config[:region] || "us"
  websocket_domain = "tunnel.nylas.com"
  callback_domain = "cb.nylas.com"

  EM.run do
    setup_websocket_client(websocket_domain, api, tunnel_id, region, config)
    register_webhook_callback(api, callback_domain, tunnel_id, triggers)
  end
end

.register_webhook_callback(api, callback_domain, tunnel_path, triggers) ⇒ Nylas::Webhook

Register callback with the Nylas forwarding service which will pass messages to the websocket

Parameters:

  • api (Nylas::API)

    The configured Nylas API client

  • callback_domain (String)

    The domain name of the callback

  • tunnel_path (String)

    The path to the tunnel

  • triggers (Array<WebhookTrigger>)

    The list of triggers to subscribe to

Returns:



38
39
40
41
42
43
44
45
46
# File 'lib/nylas/services/tunnel.rb', line 38

def self.register_webhook_callback(api, callback_domain, tunnel_path, triggers)
  callback_url = "https://#{callback_domain}/#{tunnel_path}"

  api.webhooks.create(
    callback_url: callback_url,
    state: WebhookState::ACTIVE,
    triggers: triggers
  )
end