Repository navigation
Add Subscription Topics and topic fields on campaigns, contacts, imports, suppressions, email logs and webhooks #144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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') | ||
| subscription_topics = Mailtrap::SubscriptionTopicsAPI.new(client) | ||
|
|
||
| # Subscription topics are created in the Mailtrap app; the API reads them, so that campaigns | ||
| # (subscription_topic_id) and contacts (subscription_topics) can refer to their IDs. | ||
| begin | ||
| # Get the subscription topics (paginated, by ID; deleted topics are left out) | ||
| list = subscription_topics.list(per_page: 50) | ||
| # => #<struct Mailtrap::SubscriptionTopicsListResponse | ||
| # data=[#<struct Mailtrap::SubscriptionTopic ...>], | ||
| # pagination={:token=>1, :prev_token=>nil, :next_token=>2, ...}> | ||
| list.data | ||
| # => [#<struct Mailtrap::SubscriptionTopic | ||
| # id=12, | ||
| # name="Product updates", | ||
| # description="News about our product", | ||
| # domain_id=nil, | ||
| # deleted_at=nil, | ||
| # created_at="2026-11-01T10:00:00Z", | ||
| # updated_at="2026-11-02T10:00:00Z">] | ||
| # domain_id is nil for a topic that covers every sending domain | ||
|
|
||
| # Get the next page | ||
| next_token = list.pagination[:next_token] | ||
| subscription_topics.list(per_page: 50, token: next_token) if next_token | ||
|
|
||
| # Get a single subscription topic; a deleted one is returned too, with deleted_at set | ||
| topic = list.data.first | ||
| subscription_topics.get(topic.id) if topic | ||
| # => #<struct Mailtrap::SubscriptionTopic id=12, name="Product updates", ...> | ||
| rescue Mailtrap::RejectionError | ||
| puts 'Subscription topics are not available for this account' | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -318,15 +318,15 @@ def handle_response(response) # rubocop:disable Metrics/CyclomaticComplexity, Me | |
| when Net::HTTPUnauthorized | ||
| raise Mailtrap::AuthorizationError, response_errors(response) | ||
| when Net::HTTPForbidden | ||
| raise Mailtrap::RejectionError, response_errors(response) | ||
| raise Mailtrap::RejectionError, response.body.to_s.empty? ? ['forbidden'] : response_errors(response) | ||
| when Net::HTTPPayloadTooLarge | ||
| raise Mailtrap::MailSizeError, ['message too large'] | ||
| when Net::HTTPTooManyRequests | ||
| raise Mailtrap::RateLimitError, ['too many requests'] | ||
| when Net::HTTPClientError | ||
| raise Mailtrap::Error, ["client error '#{response.body}'"] | ||
| when Net::HTTPServerError | ||
| raise Mailtrap::Error, ['server error'] | ||
| raise Mailtrap::Error, server_errors(response) | ||
| else | ||
| raise Mailtrap::Error, ["unexpected status code=#{response.code}"] | ||
| end | ||
|
|
@@ -349,6 +349,15 @@ def response_errors(response) | |
| end | ||
| end | ||
|
|
||
| # The body's errors when it has them, such as the contacts 503 that names the contact to update | ||
| def server_errors(response) | ||
| body = json_response(response.body) if json_response?(response) | ||
| errors = body.is_a?(Hash) ? Array(body[:errors] || body[:error]).compact : [] | ||
| errors.empty? ? ['server error'] : errors | ||
| rescue JSON::ParserError | ||
| ['server error'] | ||
|
Comment on lines
+357
to
+358
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [could] Nothing tests this fallback: you can delete the |
||
| end | ||
|
|
||
| def json_response?(response) | ||
| response.content_type == 'application/json' | ||
| end | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.