Skip to content

Commit 8579b71

Browse files
committed
Suppressions Api
1 parent 825b934 commit 8579b71

File tree

11 files changed

+687
-5
lines changed

11 files changed

+687
-5
lines changed

examples/suppressions_api.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
# [
16+
# Mailtrap::Suppression.new(
17+
# id: "64d71bf3-1276-417b-86e1-8e66f138acfe",
18+
# type: "unsubscription",
19+
# created_at: "2024-12-26T09:40:44.161Z",
20+
# email: "[email protected]",
21+
# sending_stream: "transactional",
22+
# domain_name: "sender.com",
23+
# message_bounce_category: nil,
24+
# message_category: "Welcome email",
25+
# message_client_ip: "123.123.123.123",
26+
# message_created_at: "2024-12-26T07:10:00.889Z",
27+
# message_outgoing_ip: "1.1.1.1",
28+
# message_recipient_mx_name: "Other Providers",
29+
# message_sender_email: "[email protected]",
30+
# message_subject: "Welcome!"
31+
# )
32+
# ]
33+
34+
# Delete a suppression
35+
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/base_api.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ def base_delete(id)
6464
client.delete("#{base_path}/#{id}")
6565
end
6666

67-
def base_list
68-
response = client.get(base_path)
67+
def base_list(query_params = {})
68+
response = client.get(base_path, query_params)
6969
response.map { |item| handle_response(item) }
7070
end
7171

lib/mailtrap/client.rb

Lines changed: 12 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,13 +122,21 @@ 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)
127+
path = build_path_with_query(path, query_params) if query_params.any?
128+
126129
request = setup_request(method, path, body)
127130
response = http_client.request(request)
128131
handle_response(response)
129132
end
130133

134+
def build_path_with_query(base_path, query_params = {})
135+
uri = URI(base_path)
136+
uri.query = URI.encode_www_form(query_params)
137+
uri.to_s
138+
end
139+
131140
def setup_request(method, path, body = nil)
132141
request = case method
133142
when :get

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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
10+
self.response_class = Suppression
11+
12+
# Lists all suppressions for the account
13+
# @param email [String] Email address to filter suppressions (optional)
14+
# @return [Array<Suppression>] Array of suppression objects
15+
# @!macro api_errors
16+
def list(email: nil)
17+
query_params = {}
18+
query_params[:email] = email if email
19+
20+
base_list(query_params)
21+
end
22+
23+
# Deletes a suppression
24+
# @param suppression_id [String] The suppression UUID
25+
# @return nil
26+
# @!macro api_errors
27+
def delete(suppression_id)
28+
client.delete("#{base_path}/#{suppression_id}")
29+
end
30+
31+
private
32+
33+
def base_path
34+
"/api/accounts/#{account_id}/suppressions"
35+
end
36+
end
37+
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)