diff --git a/README.md b/README.md index d94bbd0..0144c85 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 (experimental) – [`templates_api.rb`](examples/templates_api.rb) +- Email Templates API – [`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 0000000..e76bd28 --- /dev/null +++ b/examples/templates_api.rb @@ -0,0 +1,45 @@ +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(per_page: 50, 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 44dca60..920f90d 100644 --- a/lib/mailtrap.rb +++ b/lib/mailtrap.rb @@ -11,6 +11,7 @@ require_relative 'mailtrap/sub_accounts_api' require_relative 'mailtrap/billing_api' require_relative 'mailtrap/email_templates_api' +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/template.rb b/lib/mailtrap/template.rb new file mode 100644 index 0000000..f1deec2 --- /dev/null +++ b/lib/mailtrap/template.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +module Mailtrap + # Data Transfer Object for Template + # @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) + # @attr_reader data [Array