Skip to content
Merged
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
26 changes: 9 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,7 @@ client.send_batch(
)
```

### Email Templates API

```ruby
require 'mailtrap'

client = Mailtrap::Client.new(api_key: 'your-api-key')
templates = Mailtrap::EmailTemplatesAPI.new 3229, client

templates.create(
name: 'Welcome Email',
subject: 'Welcome to Mailtrap!',
body_html: '<h1>Hello</h1>',
body_text: 'Hello',
category: 'welcome'
)
```
Comment on lines -108 to -123
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Email templates are already a big chunk of examples, I see no reason to have it here.

### Usage Examples

Refer to the [`examples`](examples) folder for more examples:

Expand All @@ -129,6 +114,7 @@ Refer to the [`examples`](examples) folder for more examples:
- [Batch Sending](examples/batch.rb)
- [ActionMailer](examples/action_mailer.rb)
- [Email Templates API](examples/email_templates_api.rb)
- [Projects API](examples/projects_api.rb)

### Content-Transfer-Encoding

Expand Down Expand Up @@ -178,12 +164,18 @@ If you use classes which have `Sending` namespace, remove the namespace like in

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
To install this gem onto your local machine, run `bundle exec rake install`.

To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).

## Contributing

Bug reports and pull requests are welcome on [GitHub](https://github.com/mailtrap/mailtrap-ruby). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](CODE_OF_CONDUCT.md).

All contributions are required to have rspec tests covering its functionality.

Please be sure to update [README](README.md) with new examples and features when applicable.

## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
Expand Down
28 changes: 28 additions & 0 deletions examples/projects_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'mailtrap'

account_id = 3229
client = Mailtrap::Client.new(api_key: 'your-api-key')
projects_api = Mailtrap::ProjectsAPI.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
#
# projects_api = Mailtrap::ProjectsAPI.new

# Get all projects
projects_api.list

# Create a new project
project = projects_api.create(
name: 'Example Project'
)

# Get a project
project = projects_api.get(project.id)

# Update a project
project = projects_api.update(project.id, name: 'New Project name')

# Delete a project
projects_api.delete(project.id)
1 change: 1 addition & 0 deletions lib/mailtrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require_relative 'mailtrap/contact_fields_api'
require_relative 'mailtrap/contact_imports_api'
require_relative 'mailtrap/suppressions_api'
require_relative 'mailtrap/projects_api'

module Mailtrap
# @!macro api_errors
Expand Down
5 changes: 0 additions & 5 deletions lib/mailtrap/contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,5 @@ def initialize(options)
def newly_created?
@action != 'updated'
end

# @return [Hash] The contact attributes as a hash
def to_h
super.compact
end
end
end
7 changes: 1 addition & 6 deletions lib/mailtrap/contact_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,5 @@ module Mailtrap
# Allowed values: text, integer, float, boolean, date
# @attr_reader merge_tag [String] Personalize your campaigns by adding a merge tag.
# This field will be replaced with unique contact details for each recipient (max 80 characters)
ContactField = Struct.new(:id, :name, :data_type, :merge_tag, keyword_init: true) do
# @return [Hash] The contact field attributes as a hash
def to_h
super.compact
end
end
ContactField = Struct.new(:id, :name, :data_type, :merge_tag, keyword_init: true)
end
7 changes: 1 addition & 6 deletions lib/mailtrap/contact_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,5 @@ module Mailtrap
:updated_contacts_count,
:contacts_over_limit_count,
keyword_init: true
) do
# @return [Hash] The contact attributes as a hash
def to_h
super.compact
end
end
)
end
7 changes: 1 addition & 6 deletions lib/mailtrap/contact_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,5 @@ module Mailtrap
# @see https://api-docs.mailtrap.io/docs/mailtrap-api-docs/6ec7a37234af2-contact-list
# @attr_reader id [Integer] The contact list ID
# @attr_reader name [String] The name of the contact list
ContactList = Struct.new(:id, :name, keyword_init: true) do
# @return [Hash] The contact list attributes as a hash
def to_h
super.compact
end
end
ContactList = Struct.new(:id, :name, keyword_init: true)
end
7 changes: 1 addition & 6 deletions lib/mailtrap/email_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,5 @@ module Mailtrap
:created_at,
:updated_at,
keyword_init: true
) do
# @return [Hash] The template attributes as a hash
def to_h
super.compact
end
end
)
end
57 changes: 57 additions & 0 deletions lib/mailtrap/inbox.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

module Mailtrap
# Data Transfer Object for Inbox
# @see https://api-docs.mailtrap.io/docs/mailtrap-api-docs/ee252e413d78a-create-project
# @attr_reader id [Integer] The inbox ID
# @attr_reader name [String] The inbox name
# @attr_reader username [String] The inbox username
# @attr_reader password [String, nil] The inbox password
# @attr_reader max_size [Integer] The maximum inbox size in MB
# @attr_reader status [String] The inbox status
# @attr_reader email_username [String] The email username
# @attr_reader email_username_enabled [Boolean] Whether the email username is enabled
# @attr_reader sent_messages_count [Integer] The count of sent messages
# @attr_reader forwarded_messages_count [Integer] The count of forwarded messages
# @attr_reader used [Integer] The used inbox size in MB
# @attr_reader forward_from_email_address [String] The forwarding email address
# @attr_reader project_id [Integer] The associated project ID
# @attr_reader domain [String] The inbox domain
# @attr_reader pop3_domain [String] The POP3 domain
# @attr_reader email_domain [String] The email domain
# @attr_reader api_domain [String] The API domain
# @attr_reader emails_count [Integer] The total number of emails
# @attr_reader emails_unread_count [Integer] The number of unread emails
# @attr_reader last_message_sent_at [String, nil] The timestamp of the last sent message
# @attr_reader smtp_ports [Array<Integer>] The list of SMTP ports
# @attr_reader pop3_ports [Array<Integer>] The list of POP3 ports
# @attr_reader max_message_size [Integer] The maximum message size in MB
# @attr_reader permissions [Hash] List of permissions
Inbox = Struct.new(
:id,
:name,
:username,
:password,
:max_size,
:status,
:email_username,
:email_username_enabled,
:sent_messages_count,
:forwarded_messages_count,
:used,
:forward_from_email_address,
:project_id,
:domain,
:pop3_domain,
:email_domain,
:api_domain,
:emails_count,
:emails_unread_count,
:last_message_sent_at,
:smtp_ports,
:pop3_ports,
:max_message_size,
:permissions,
keyword_init: true
)
end
20 changes: 20 additions & 0 deletions lib/mailtrap/project.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module Mailtrap
# Data Transfer Object for Project
# @see https://api-docs.mailtrap.io/docs/mailtrap-api-docs/ee252e413d78a-create-project
# @attr_reader id [Integer] The project ID
# @attr_reader name [String] The project name
# @attr_reader share_links [Hash] Admin and viewer share links
# @attr_reader inboxes [Array<Mailtrap::Inbox>] Array of inboxes
# @attr_reader permissions [Hash] List of permissions
#
Project = Struct.new(
:id,
:name,
:share_links,
:inboxes,
:permissions,
keyword_init: true
)
end
77 changes: 77 additions & 0 deletions lib/mailtrap/projects_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# frozen_string_literal: true

require_relative 'base_api'
require_relative 'project'
require_relative 'inbox'

module Mailtrap
class ProjectsAPI
include BaseAPI

self.supported_options = %i[name]

self.response_class = Project

# Lists all projects for the account
# @return [Array<Project>] Array of projects
# @!macro api_errors
def list
base_list
end

# Retrieves a specific project
# @param project_id [Integer] The project ID
# @return [Project] Project object
# @!macro api_errors
def get(project_id)
base_get(project_id)
end

# Creates a new project
# @param [Hash] options The parameters to create
# @option options [String] :name The project name
# @return [Project] Created project object
# @!macro api_errors
# @raise [ArgumentError] If invalid options are provided
def create(options)
base_create(options)
end

# Updates an existing project
# @param project_id [Integer] The project ID
# @param [Hash] options The parameters to update
# @return [Project] Updated project object
# @!macro api_errors
# @raise [ArgumentError] If invalid options are provided
def update(project_id, options)
base_update(project_id, options)
end

# Deletes a project
# @param project_id [Integer] The project ID
# @return nil
# @!macro api_errors
def delete(project_id)
base_delete(project_id)
end
Comment on lines +50 to +56
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Update the return value documentation.

The YARD comment states @return nil, but base_delete returns the response from client.delete, which the specs expect to be a hash like { id: project_id }.

📝 Suggested fix
-    # @return nil
+    # @return [Hash] The deleted project response, e.g., { id: project_id }
🤖 Prompt for AI Agents
In @lib/mailtrap/projects_api.rb around lines 50 - 56, The YARD @return is
incorrect: the delete(project_id) method calls base_delete(project_id) which
returns the client response (e.g., a Hash like { id: project_id }), not nil;
update the comment for delete to reflect the actual return type and shape (for
example "@return [Hash] response hash containing the deleted project id" or
similar) and ensure any related docs/macro references match the new return
description; keep the method body unchanged (it should still call
base_delete(project_id)).


private

def handle_response(response)
Copy link
Contributor

Choose a reason for hiding this comment

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

must be private.

build_entity(
response.merge(
inboxes: response[:inboxes]&.map { |inbox| build_entity(inbox, Mailtrap::Inbox) }
Copy link
Contributor

Choose a reason for hiding this comment

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

it is still not covered by any spec.

),
response_class
)
end

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

def wrap_request(options)
{ project: options }
end
end
end
7 changes: 1 addition & 6 deletions lib/mailtrap/suppression.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,5 @@ module Mailtrap
: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
Loading