From 4f6619c60643e07fb1118df500eb26f86414783c Mon Sep 17 00:00:00 2001 From: IzikAJ Date: Mon, 5 Oct 2026 17:20:37 +0300 Subject: [PATCH 1/4] Add TemplatesAPI for the paginated /api/templates endpoints - New Mailtrap::TemplatesAPI is account-scoped and sends flat request bodies; list returns TemplatesListResponse with the raw pagination hash and accepts per_page and token. - Single-template responses are unwrapped from the data envelope, so get/create/update return Mailtrap::Template like EmailTemplatesAPI did. - EmailTemplatesAPI and EmailTemplate are marked @deprecated in YARD only, with no runtime warning, so existing callers are unaffected. - Specs are WebMock-stubbed; no VCR cassettes are added. --- README.md | 3 +- examples/templates_api.rb | 44 +++++++ lib/mailtrap.rb | 2 + lib/mailtrap/email_template.rb | 1 + lib/mailtrap/email_templates_api.rb | 1 + lib/mailtrap/template.rb | 38 ++++++ lib/mailtrap/templates_api.rb | 87 ++++++++++++++ spec/mailtrap/template_spec.rb | 35 ++++++ spec/mailtrap/templates_api_spec.rb | 173 ++++++++++++++++++++++++++++ 9 files changed, 383 insertions(+), 1 deletion(-) create mode 100644 examples/templates_api.rb create mode 100644 lib/mailtrap/template.rb create mode 100644 lib/mailtrap/templates_api.rb create mode 100644 spec/mailtrap/template_spec.rb create mode 100644 spec/mailtrap/templates_api_spec.rb diff --git a/README.md b/README.md index d94bbd0d..044b8a9b 100644 --- a/README.md +++ b/README.md @@ -211,7 +211,8 @@ General: - API Tokens API – [`api_tokens_api.rb`](examples/api_tokens_api.rb) - Billing API – [`billing_api.rb`](examples/billing_api.rb) - Permissions API – [`permissions_api.rb`](examples/permissions_api.rb) -- Templates API – [`email_templates_api.rb`](examples/email_templates_api.rb) +- Templates API (paginated) – [`templates_api.rb`](examples/templates_api.rb) +- Email Templates API (deprecated) – [`email_templates_api.rb`](examples/email_templates_api.rb) - Action Mailer – [`action_mailer.rb`](examples/action_mailer.rb) - Verifying webhook signatures – [`webhooks_signature_verification.ru`](examples/webhooks_signature_verification.ru) diff --git a/examples/templates_api.rb b/examples/templates_api.rb new file mode 100644 index 00000000..1b8eda3d --- /dev/null +++ b/examples/templates_api.rb @@ -0,0 +1,44 @@ +require 'mailtrap' + +account_id = 3229 +client = Mailtrap::Client.new(api_key: 'your-api-key') +templates = Mailtrap::TemplatesAPI.new(account_id, client) + +# Set your API credentials as environment variables +# export MAILTRAP_API_KEY='your-api-key' +# export MAILTRAP_ACCOUNT_ID=your-account-id +# +# templates = Mailtrap::TemplatesAPI.new + +# Create a new Template +template = templates.create( + name: 'Welcome Email', + subject: 'Welcome to Acme!', + category: 'Welcome', + body_html: '
Welcome, {{name}}!
', + body_text: 'Welcome, {{name}}!' +) +# => # + +# Get all Templates (paginated) +list = templates.list(per_page: 50, token: 1) +# => #], pagination={...}> +list.data +# => [#] +list.pagination +# => {:token=>1, :prev_token=>nil, :next_token=>2, ...} + +# Follow the pagination to the next page +templates.list(token: list.pagination[:next_token]) if list.pagination[:next_token] + +# Get a single Template +template = templates.get(template.id) +# => # + +# Update a Template (partial) +template = templates.update(template.id, subject: 'Welcome aboard!') +# => # + +# Delete a Template (returns nil) +templates.delete(template.id) +# => nil diff --git a/lib/mailtrap.rb b/lib/mailtrap.rb index 44dca60c..d2f0edd4 100644 --- a/lib/mailtrap.rb +++ b/lib/mailtrap.rb @@ -11,6 +11,8 @@ require_relative 'mailtrap/sub_accounts_api' require_relative 'mailtrap/billing_api' require_relative 'mailtrap/email_templates_api' +require_relative 'mailtrap/template' +require_relative 'mailtrap/templates_api' require_relative 'mailtrap/contacts_api' require_relative 'mailtrap/contact_lists_api' require_relative 'mailtrap/contact_fields_api' diff --git a/lib/mailtrap/email_template.rb b/lib/mailtrap/email_template.rb index 6df191da..67549cc2 100644 --- a/lib/mailtrap/email_template.rb +++ b/lib/mailtrap/email_template.rb @@ -15,6 +15,7 @@ module Mailtrap # @attr_reader body_text [String] The plain text content # @attr_reader created_at [String] The creation timestamp # @attr_reader updated_at [String] The last update timestamp + # @deprecated Use {Mailtrap::Template} EmailTemplate = Struct.new( :id, :uuid, diff --git a/lib/mailtrap/email_templates_api.rb b/lib/mailtrap/email_templates_api.rb index 0a487914..7f072386 100644 --- a/lib/mailtrap/email_templates_api.rb +++ b/lib/mailtrap/email_templates_api.rb @@ -4,6 +4,7 @@ require_relative 'email_template' module Mailtrap + # @deprecated Use {Mailtrap::TemplatesAPI} class EmailTemplatesAPI include BaseAPI diff --git a/lib/mailtrap/template.rb b/lib/mailtrap/template.rb new file mode 100644 index 00000000..b2f5d727 --- /dev/null +++ b/lib/mailtrap/template.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +module Mailtrap + # Data Transfer Object for Template + # @see https://api-docs.mailtrap.io/docs/mailtrap-api-docs/templates + # @attr_reader id [Integer] The template ID + # @attr_reader uuid [String] The template UUID + # @attr_reader name [String] The template name + # @attr_reader subject [String] The email subject + # @attr_reader category [String] The template category + # @attr_reader body_html [String] The HTML content + # @attr_reader body_text [String] The plain text content + # @attr_reader created_at [String] The creation timestamp + # @attr_reader updated_at [String] The last update timestamp + Template = Struct.new( + :id, + :uuid, + :name, + :subject, + :category, + :body_html, + :body_text, + :created_at, + :updated_at, + keyword_init: true + ) + + # Response from listing templates (paginated) + # @see https://api-docs.mailtrap.io/docs/mailtrap-api-docs/templates + # @attr_reader data [Array