Skip to content

Commit c5b5a18

Browse files
sarco3ti7an
authored andcommitted
Suppressions Api
1 parent 440de0d commit c5b5a18

File tree

11 files changed

+686
-5
lines changed

11 files changed

+686
-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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
require_relative 'mailtrap/contact_lists_api'
1010
require_relative 'mailtrap/contact_fields_api'
1111
require_relative 'mailtrap/contact_imports_api'
12+
require_relative 'mailtrap/suppressions_api'
1213

1314
module Mailtrap
1415
# @!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
@@ -157,10 +157,11 @@ def send(mail)
157157

158158
# Performs a GET request to the specified path
159159
# @param path [String] The request path
160+
# @param query_params [Hash] Query parameters to append to the URL (optional)
160161
# @return [Hash, nil] The JSON response
161162
# @!macro api_errors
162-
def get(path)
163-
perform_request(:get, general_api_host, path)
163+
def get(path, query_params = {})
164+
perform_request(:get, general_api_host, path, nil, query_params)
164165
end
165166

166167
# Performs a POST request to the specified path
@@ -213,13 +214,21 @@ def batch_request_path
213214
"/api/batch#{"/#{inbox_id}" if sandbox}"
214215
end
215216

216-
def perform_request(method, host, path, body = nil)
217+
def perform_request(method, host, path, body = nil, query_params = {})
217218
http_client = http_client_for(host)
219+
path = build_path_with_query(path, query_params) if query_params.any?
220+
218221
request = setup_request(method, path, body)
219222
response = http_client.request(request)
220223
handle_response(response)
221224
end
222225

226+
def build_path_with_query(base_path, query_params = {})
227+
uri = URI(base_path)
228+
uri.query = URI.encode_www_form(query_params)
229+
uri.to_s
230+
end
231+
223232
def setup_request(method, path, body = nil)
224233
request = case method
225234
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)