Skip to content

Commit 3d0d0da

Browse files
committed
Suppressions Api
1 parent 825b934 commit 3d0d0da

File tree

10 files changed

+642
-3
lines changed

10 files changed

+642
-3
lines changed

examples/suppressions_api.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require 'mailtrap'
2+
3+
client = Mailtrap::Client.new(api_key: 'your-api-key')
4+
suppressions = Mailtrap::SuppressionsAPI.new 3229, client
5+
6+
# Set your API credentials as environment variables
7+
# export MAILTRAP_API_KEY='your-api-key'
8+
# export MAILTRAP_ACCOUNT_ID=your-account-id
9+
#
10+
# suppressions = Mailtrap::SuppressionsAPI.new
11+
12+
# Get all suppressions
13+
list = suppressions.list
14+
15+
# Delete a suppression
16+
suppressions.delete(list.first.id)

lib/mailtrap.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
require_relative 'mailtrap/mail'
55
require_relative 'mailtrap/errors'
66
require_relative 'mailtrap/version'
7+
require_relative 'mailtrap/base_api'
78
require_relative 'mailtrap/email_templates_api'
89
require_relative 'mailtrap/contacts_api'
910
require_relative 'mailtrap/contact_lists_api'
1011
require_relative 'mailtrap/contact_fields_api'
12+
require_relative 'mailtrap/suppressions_api'
1113

1214
module Mailtrap
1315
# @!macro api_errors

lib/mailtrap/client.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,11 @@ def send(mail)
6767

6868
# Performs a GET request to the specified path
6969
# @param path [String] The request path
70+
# @param query_params [Hash] Query parameters to append to the URL (optional)
7071
# @return [Hash, nil] The JSON response
7172
# @!macro api_errors
72-
def get(path)
73-
perform_request(:get, general_api_host, path)
73+
def get(path, query_params = {})
74+
perform_request(:get, general_api_host, path, nil, query_params)
7475
end
7576

7677
# Performs a POST request to the specified path
@@ -121,9 +122,10 @@ def send_path
121122
"/api/send#{sandbox ? "/#{inbox_id}" : ""}"
122123
end
123124

124-
def perform_request(method, host, path, body = nil)
125+
def perform_request(method, host, path, body = nil, query_params = {})
125126
http_client = http_client_for(host)
126127
request = setup_request(method, path, body)
128+
request.query = URI.encode_www_form(query_params) if query_params.any?
127129
response = http_client.request(request)
128130
handle_response(response)
129131
end

lib/mailtrap/suppression.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# frozen_string_literal: true
2+
3+
module Mailtrap
4+
# Data Transfer Object for Suppression
5+
# @see https://api-docs.mailtrap.io/docs/mailtrap-api-docs/f8144826d885a-list-and-search-suppressions
6+
# @attr_reader id [String] The suppression UUID
7+
# @attr_reader type [String] The suppression type
8+
# @attr_reader created_at [String] The creation timestamp
9+
# @attr_reader email [String] The email address
10+
# @attr_reader sending_stream [String] The sending stream
11+
# @attr_reader domain_name [String, nil] The domain name
12+
# @attr_reader message_bounce_category [String, nil] The bounce category
13+
# @attr_reader message_category [String, nil] The message category
14+
# @attr_reader message_client_ip [String, nil] The client IP
15+
# @attr_reader message_created_at [String, nil] The message creation timestamp
16+
# @attr_reader message_esp_response [String, nil] The ESP response
17+
# @attr_reader message_esp_server_type [String, nil] The ESP server type
18+
# @attr_reader message_outgoing_ip [String, nil] The outgoing IP
19+
# @attr_reader message_recipient_mx_name [String, nil] The recipient MX name
20+
# @attr_reader message_sender_email [String, nil] The sender email
21+
# @attr_reader message_subject [String, nil] The message subject
22+
Suppression = Struct.new(
23+
:id,
24+
:type,
25+
:created_at,
26+
:email,
27+
:sending_stream,
28+
:domain_name,
29+
:message_bounce_category,
30+
:message_category,
31+
:message_client_ip,
32+
:message_created_at,
33+
:message_esp_response,
34+
:message_esp_server_type,
35+
:message_outgoing_ip,
36+
:message_recipient_mx_name,
37+
:message_sender_email,
38+
:message_subject,
39+
keyword_init: true
40+
) do
41+
# @return [Hash] The suppression attributes as a hash
42+
def to_h
43+
super.compact
44+
end
45+
end
46+
end

lib/mailtrap/suppressions_api.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# frozen_string_literal: true
2+
3+
require_relative 'base_api'
4+
require_relative 'suppression'
5+
6+
module Mailtrap
7+
class SuppressionsAPI
8+
include BaseAPI
9+
# Lists all suppressions for the account
10+
# @param email [String] Email address to filter suppressions (optional)
11+
# @return [Array<Suppression>] Array of suppression objects
12+
# @!macro api_errors
13+
def list(email: nil)
14+
query_params = {}
15+
query_params[:email] = email if email
16+
17+
response = client.get(base_path, query_params)
18+
response.map { |suppression| build_entity(suppression, Suppression) }
19+
end
20+
21+
# Deletes a suppression
22+
# @param suppression_id [String] The suppression UUID
23+
# @return nil
24+
# @!macro api_errors
25+
def delete(suppression_id)
26+
client.delete("#{base_path}/#{suppression_id}")
27+
end
28+
29+
private
30+
31+
def base_path
32+
"/api/accounts/#{@account_id}/suppressions"
33+
end
34+
end
35+
end

spec/fixtures/vcr_cassettes/Mailtrap_SuppressionsAPI/vcr_list/maps_response_data_to_Suppression_objects.yml

Lines changed: 166 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)