Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions examples/suppressions_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'mailtrap'

client = Mailtrap::Client.new(api_key: 'your-api-key')
suppressions = Mailtrap::SuppressionsAPI.new 3229, client
Comment on lines +3 to +4
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Remove hardcoded credentials from example.

The example contains hardcoded API credentials which could be accidentally committed or copied by users. Consider using placeholder text or environment variables by default.

-client = Mailtrap::Client.new(api_key: 'your-api-key')
-suppressions = Mailtrap::SuppressionsAPI.new 3229, client
+# Set your API credentials as environment variables
+# export MAILTRAP_API_KEY='your-api-key'
+# export MAILTRAP_ACCOUNT_ID=your-account-id
+suppressions = Mailtrap::SuppressionsAPI.new
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
client = Mailtrap::Client.new(api_key: 'your-api-key')
suppressions = Mailtrap::SuppressionsAPI.new 3229, client
# Set your API credentials as environment variables
# export MAILTRAP_API_KEY='your-api-key'
# export MAILTRAP_ACCOUNT_ID=your-account-id
suppressions = Mailtrap::SuppressionsAPI.new
🤖 Prompt for AI Agents
In examples/suppressions_api.rb around lines 3 to 4, the API key is hardcoded
directly in the code, which risks accidental exposure. Replace the hardcoded API
key string with a placeholder text like 'your-api-key' or fetch it from an
environment variable to avoid embedding sensitive credentials in the example.


# Set your API credentials as environment variables
# export MAILTRAP_API_KEY='your-api-key'
# export MAILTRAP_ACCOUNT_ID=your-account-id
#
# suppressions = Mailtrap::SuppressionsAPI.new

# Get all suppressions
list = suppressions.list
# =>
# [
# Mailtrap::Suppression.new(
# id: "64d71bf3-1276-417b-86e1-8e66f138acfe",
# type: "unsubscription",
# created_at: "2024-12-26T09:40:44.161Z",
# email: "[email protected]",
# sending_stream: "transactional",
# domain_name: "sender.com",
# message_bounce_category: nil,
# message_category: "Welcome email",
# message_client_ip: "123.123.123.123",
# message_created_at: "2024-12-26T07:10:00.889Z",
# message_outgoing_ip: "1.1.1.1",
# message_recipient_mx_name: "Other Providers",
# message_sender_email: "[email protected]",
# message_subject: "Welcome!"
# )
# ]

# Delete a suppression
suppressions.delete(list.first.id)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add nil check before accessing list.first.

The code assumes the suppressions list is not empty, which could cause a NoMethodError if no suppressions exist.

-suppressions.delete(list.first.id)
+suppressions.delete(list.first.id) if list.any?
🤖 Prompt for AI Agents
In examples/suppressions_api.rb at line 16, add a nil check before accessing
list.first to prevent a NoMethodError when the suppressions list is empty.
Modify the code to verify that list.first is not nil before calling .id and
passing it to suppressions.delete, ensuring safe handling of empty lists.

2 changes: 2 additions & 0 deletions lib/mailtrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
require_relative 'mailtrap/mail'
require_relative 'mailtrap/errors'
require_relative 'mailtrap/version'
require_relative 'mailtrap/base_api'
require_relative 'mailtrap/email_templates_api'
require_relative 'mailtrap/contacts_api'
require_relative 'mailtrap/contact_lists_api'
require_relative 'mailtrap/contact_fields_api'
require_relative 'mailtrap/suppressions_api'

module Mailtrap
# @!macro api_errors
Expand Down
4 changes: 2 additions & 2 deletions lib/mailtrap/base_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ def base_delete(id)
client.delete("#{base_path}/#{id}")
end

def base_list
response = client.get(base_path)
def base_list(query_params = {})
response = client.get(base_path, query_params)
response.map { |item| handle_response(item) }
end

Expand Down
15 changes: 12 additions & 3 deletions lib/mailtrap/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ def send(mail)

# Performs a GET request to the specified path
# @param path [String] The request path
# @param query_params [Hash] Query parameters to append to the URL (optional)
# @return [Hash, nil] The JSON response
# @!macro api_errors
def get(path)
perform_request(:get, general_api_host, path)
def get(path, query_params = {})
perform_request(:get, general_api_host, path, nil, query_params)
end

# Performs a POST request to the specified path
Expand Down Expand Up @@ -121,13 +122,21 @@ def send_path
"/api/send#{sandbox ? "/#{inbox_id}" : ""}"
end

def perform_request(method, host, path, body = nil)
def perform_request(method, host, path, body = nil, query_params = {})
http_client = http_client_for(host)
path = build_path_with_query(path, query_params) if query_params.any?

request = setup_request(method, path, body)
response = http_client.request(request)
handle_response(response)
end

def build_path_with_query(base_path, query_params = {})
uri = URI(base_path)
uri.query = URI.encode_www_form(query_params)
uri.to_s
end

def setup_request(method, path, body = nil)
request = case method
when :get
Expand Down
46 changes: 46 additions & 0 deletions lib/mailtrap/suppression.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

module Mailtrap
# Data Transfer Object for Suppression
# @see https://api-docs.mailtrap.io/docs/mailtrap-api-docs/f8144826d885a-list-and-search-suppressions
# @attr_reader id [String] The suppression UUID
# @attr_reader type [String] The suppression type
# @attr_reader created_at [String] The creation timestamp
# @attr_reader email [String] The email address
# @attr_reader sending_stream [String] The sending stream
# @attr_reader domain_name [String, nil] The domain name
# @attr_reader message_bounce_category [String, nil] The bounce category
# @attr_reader message_category [String, nil] The message category
# @attr_reader message_client_ip [String, nil] The client IP
# @attr_reader message_created_at [String, nil] The message creation timestamp
# @attr_reader message_esp_response [String, nil] The ESP response
# @attr_reader message_esp_server_type [String, nil] The ESP server type
# @attr_reader message_outgoing_ip [String, nil] The outgoing IP
# @attr_reader message_recipient_mx_name [String, nil] The recipient MX name
# @attr_reader message_sender_email [String, nil] The sender email
# @attr_reader message_subject [String, nil] The message subject
Suppression = Struct.new(
:id,
:type,
:created_at,
:email,
:sending_stream,
:domain_name,
:message_bounce_category,
:message_category,
:message_client_ip,
:message_created_at,
:message_esp_response,
:message_esp_server_type,
:message_outgoing_ip,
:message_recipient_mx_name,
:message_sender_email,
:message_subject,
keyword_init: true
) do
# @return [Hash] The suppression attributes as a hash
def to_h
super.compact
end
end
end
37 changes: 37 additions & 0 deletions lib/mailtrap/suppressions_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

require_relative 'base_api'
require_relative 'suppression'

module Mailtrap
class SuppressionsAPI
include BaseAPI

self.response_class = Suppression

# Lists all suppressions for the account
# @param email [String] Email address to filter suppressions (optional)
# @return [Array<Suppression>] Array of suppression objects
# @!macro api_errors
def list(email: nil)
query_params = {}
query_params[:email] = email if email

base_list(query_params)
end

# Deletes a suppression
# @param suppression_id [String] The suppression UUID
# @return nil
# @!macro api_errors
def delete(suppression_id)
client.delete("#{base_path}/#{suppression_id}")
end

private

def base_path
"/api/accounts/#{account_id}/suppressions"
end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading