Skip to content

Commit a9f7b42

Browse files
authored
Merge pull request #73 from railsware/suppressions-api
Add Suppressions API
2 parents 440de0d + d2e34e2 commit a9f7b42

File tree

12 files changed

+719
-24
lines changed

12 files changed

+719
-24
lines changed

examples/contacts_api.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
require 'mailtrap'
22

3+
account_id = 3229
34
client = Mailtrap::Client.new(api_key: 'your-api-key')
4-
contact_lists = Mailtrap::ContactListsAPI.new 3229, client
5-
contacts = Mailtrap::ContactsAPI.new 3229, client
6-
contact_fields = Mailtrap::ContactFieldsAPI.new 3229, client
7-
contact_imports = Mailtrap::ContactImportsAPI.new 3229, client
5+
contact_lists = Mailtrap::ContactListsAPI.new(account_id, client)
6+
contacts = Mailtrap::ContactsAPI.new(account_id, client)
7+
contact_fields = Mailtrap::ContactFieldsAPI.new(account_id, client)
8+
contact_imports = Mailtrap::ContactImportsAPI.new(account_id, client)
89

910
# Set your API credentials as environment variables
1011
# export MAILTRAP_API_KEY='your-api-key'

examples/email_templates_api.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
require 'mailtrap'
22

3+
account_id = 3229
34
client = Mailtrap::Client.new(api_key: 'your-api-key')
4-
templates = Mailtrap::EmailTemplatesAPI.new 3229, client
5+
templates = Mailtrap::EmailTemplatesAPI.new(account_id, client)
56

67
# Set your API credentials as environment variables
78
# export MAILTRAP_API_KEY='your-api-key'

examples/suppressions_api.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
require 'mailtrap'
2+
3+
account_id = 3229
4+
client = Mailtrap::Client.new(api_key: 'your-api-key')
5+
suppressions = Mailtrap::SuppressionsAPI.new(account_id, client)
6+
7+
# Set your API credentials as environment variables
8+
# export MAILTRAP_API_KEY='your-api-key'
9+
# export MAILTRAP_ACCOUNT_ID=your-account-id
10+
#
11+
# suppressions = Mailtrap::SuppressionsAPI.new
12+
13+
# Get all suppressions
14+
suppressions.list
15+
# =>
16+
# [
17+
# #<struct Mailtrap::Suppression
18+
# id="64d71bf3-1276-417b-86e1-8e66f138acfe",
19+
# type="unsubscription",
20+
# created_at="2024-12-26T09:40:44.161Z",
21+
22+
# sending_stream="transactional",
23+
# domain_name="example.com",
24+
# message_bounce_category="",
25+
# message_category="Welcome email",
26+
# message_client_ip="123.123.123.123",
27+
# message_created_at="2024-12-26T07:10:00.889Z",
28+
# message_esp_response="",
29+
# message_esp_server_type="",
30+
# message_outgoing_ip="1.1.1.1",
31+
# message_recipient_mx_name="Other Providers",
32+
# message_sender_email="[email protected]",
33+
# message_subject="Welcome!">
34+
# ]
35+
36+
# Get suppressions for the email
37+
list = suppressions.list(email: '[email protected]')
38+
39+
# Delete a suppression
40+
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: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,15 @@ def initialize( # rubocop:disable Metrics/ParameterLists
119119
# @!macro api_errors
120120
# @raise [Mailtrap::MailSizeError] If the message is too large.
121121
def send_batch(base, requests)
122-
perform_request(:post, api_host, batch_request_path, {
123-
base:,
124-
requests:
125-
})
122+
perform_request(
123+
method: :post,
124+
host: api_host,
125+
path: batch_request_path,
126+
body: {
127+
base:,
128+
requests:
129+
}
130+
)
126131
end
127132

128133
# Sends an email
@@ -152,15 +157,26 @@ def send_batch(base, requests)
152157
# @!macro api_errors
153158
# @raise [Mailtrap::MailSizeError] If the message is too large
154159
def send(mail)
155-
perform_request(:post, api_host, send_path, mail)
160+
perform_request(
161+
method: :post,
162+
host: api_host,
163+
path: send_path,
164+
body: mail
165+
)
156166
end
157167

158168
# Performs a GET request to the specified path
159169
# @param path [String] The request path
170+
# @param query_params [Hash] Query parameters to append to the URL (optional)
160171
# @return [Hash, nil] The JSON response
161172
# @!macro api_errors
162-
def get(path)
163-
perform_request(:get, general_api_host, path)
173+
def get(path, query_params = {})
174+
perform_request(
175+
method: :get,
176+
host: general_api_host,
177+
path:,
178+
query_params:
179+
)
164180
end
165181

166182
# Performs a POST request to the specified path
@@ -169,7 +185,12 @@ def get(path)
169185
# @return [Hash, nil] The JSON response
170186
# @!macro api_errors
171187
def post(path, body = nil)
172-
perform_request(:post, general_api_host, path, body)
188+
perform_request(
189+
method: :post,
190+
host: general_api_host,
191+
path:,
192+
body:
193+
)
173194
end
174195

175196
# Performs a PATCH request to the specified path
@@ -178,15 +199,24 @@ def post(path, body = nil)
178199
# @return [Hash, nil] The JSON response
179200
# @!macro api_errors
180201
def patch(path, body = nil)
181-
perform_request(:patch, general_api_host, path, body)
202+
perform_request(
203+
method: :patch,
204+
host: general_api_host,
205+
path:,
206+
body:
207+
)
182208
end
183209

184210
# Performs a DELETE request to the specified path
185211
# @param path [String] The request path
186212
# @return [Hash, nil] The JSON response
187213
# @!macro api_errors
188214
def delete(path)
189-
perform_request(:delete, general_api_host, path)
215+
perform_request(
216+
method: :delete,
217+
host: general_api_host,
218+
path:
219+
)
190220
end
191221

192222
private
@@ -213,23 +243,27 @@ def batch_request_path
213243
"/api/batch#{"/#{inbox_id}" if sandbox}"
214244
end
215245

216-
def perform_request(method, host, path, body = nil)
246+
def perform_request(method:, host:, path:, query_params: {}, body: nil)
217247
http_client = http_client_for(host)
218-
request = setup_request(method, path, body)
248+
249+
uri = URI::HTTPS.build(host:, path:)
250+
uri.query = URI.encode_www_form(query_params) if query_params.any?
251+
252+
request = setup_request(method, uri, body)
219253
response = http_client.request(request)
220254
handle_response(response)
221255
end
222256

223-
def setup_request(method, path, body = nil)
257+
def setup_request(method, uri_or_path, body = nil)
224258
request = case method
225259
when :get
226-
Net::HTTP::Get.new(path)
260+
Net::HTTP::Get.new(uri_or_path)
227261
when :post
228-
Net::HTTP::Post.new(path)
262+
Net::HTTP::Post.new(uri_or_path)
229263
when :patch
230-
Net::HTTP::Patch.new(path)
264+
Net::HTTP::Patch.new(uri_or_path)
231265
when :delete
232-
Net::HTTP::Delete.new(path)
266+
Net::HTTP::Delete.new(uri_or_path)
233267
else
234268
raise ArgumentError, "Unsupported HTTP method: #{method}"
235269
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: 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

0 commit comments

Comments
 (0)