Class: MailerLite::Webhooks

Inherits:
Object
  • Object
show all
Defined in:
lib/mailerlite/webhooks/webhooks.rb

Overview

This is a class for manipulating the Webhooks from MailerLite API.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client: MailerLite::Client.new) ⇒ Webhooks

Inits the ‘Webhooks` class with the specified `client`.

Parameters:

  • client (MailerLite::Client) (defaults to: MailerLite::Client.new)

    the ‘Client` instance to use



11
12
13
# File 'lib/mailerlite/webhooks/webhooks.rb', line 11

def initialize(client: MailerLite::Client.new)
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



6
7
8
# File 'lib/mailerlite/webhooks/webhooks.rb', line 6

def client
  @client
end

Instance Method Details

#create(events:, url:, name: nil) ⇒ HTTP::Response

Create a Webhook

Parameters:

  • name (String) (defaults to: nil)

    the name of the Webhook to create

  • events (Array)

    the events, must one from the list of supported events

  • url (String)

    the events, can be text, number or date

Returns:

  • (HTTP::Response)

    the response from the API



36
37
38
39
40
# File 'lib/mailerlite/webhooks/webhooks.rb', line 36

def create(events:, url:, name: nil)
  params = { 'events' => events, 'url' => url }
  params['name'] = name if name
  client.http.post("#{MAILERLITE_API_URL}/webhooks", json: params.compact)
end

#delete(webhook_id) ⇒ HTTP::Response

Deletes the specified Webhook.

Parameters:

  • webhook_id (String)

    the ID of the Webhook to delete

Returns:

  • (HTTP::Response)

    the response from the API



63
64
65
# File 'lib/mailerlite/webhooks/webhooks.rb', line 63

def delete(webhook_id)
  client.http.delete("#{MAILERLITE_API_URL}/webhooks/#{webhook_id}")
end

#get(webhook_id) ⇒ HTTP::Response

Returns the details of the specified webhooks

Parameters:

  • webhook_id (String)

    the ID of the webhooks to fetch

Returns:

  • (HTTP::Response)

    the response from the API



26
27
28
# File 'lib/mailerlite/webhooks/webhooks.rb', line 26

def get(webhook_id)
  client.http.get("#{MAILERLITE_API_URL}/webhooks/#{webhook_id}")
end

#listHTTP::Response

Returns a list of Webhooks

Returns:

  • (HTTP::Response)

    the response from the API



18
19
20
# File 'lib/mailerlite/webhooks/webhooks.rb', line 18

def list
  client.http.get("#{MAILERLITE_API_URL}/webhooks")
end

#update(webhook_id:, events: nil, name: nil, url: nil, enabled: nil) ⇒ HTTP::Response

Update the specified Webhook

Parameters:

  • webhook_id (String)

    the ID of the Webhook to update

  • name (String) (defaults to: nil)

    the name to update

  • events (Array) (defaults to: nil)

    the events to update

  • url (String) (defaults to: nil)

    the url to update

  • enabled (Boolean) (defaults to: nil)

    the enabled to update

Returns:

  • (HTTP::Response)

    the response from the API



50
51
52
53
54
55
56
57
# File 'lib/mailerlite/webhooks/webhooks.rb', line 50

def update(webhook_id:, events: nil, name: nil, url: nil, enabled: nil)
  params = {}
  params['events'] = events if events
  params['name'] = name if name
  params['url'] = url if url
  params['enabled'] = enabled if enabled
  client.http.put("#{MAILERLITE_API_URL}/webhooks/#{webhook_id}", json: params.compact)
end