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
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
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
62 changes: 62 additions & 0 deletions lib/mailtrap/inbox.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# 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
) do
# @return [Hash] The inbox attributes as a hash
def to_h
super.compact
Copy link
Contributor

Choose a reason for hiding this comment

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

last_message_sent_at can be nil. Compacting will remove it. Is it necessary?

end
end
end
25 changes: 25 additions & 0 deletions lib/mailtrap/project.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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
) do
# @return [Hash] The Project attributes as a hash
def to_h
super.compact
end
end
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

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# First, let's find all delete methods in the lib directory
rg -n "def delete\(" lib/mailtrap --type rb

Repository: mailtrap/mailtrap-ruby

Length of output: 92


🏁 Script executed:

#!/bin/bash
# Check the spec file to verify delete expectations
find . -name "*projects_api*spec*" -type f

Repository: mailtrap/mailtrap-ruby

Length of output: 102


🏁 Script executed:

#!/bin/bash
# Look at the BaseAPI to understand base_delete behavior
find . -name "*base*api*" -type f | grep -E "\.(rb|py)$"

Repository: mailtrap/mailtrap-ruby

Length of output: 92


🏁 Script executed:

#!/bin/bash
# Search for delete methods without type flag
rg -n "def delete\(" lib/mailtrap/

Repository: mailtrap/mailtrap-ruby

Length of output: 489


🏁 Script executed:

#!/bin/bash
# Read the projects_api_spec.rb to verify delete expectations
cat ./spec/mailtrap/projects_api_spec.rb

Repository: mailtrap/mailtrap-ruby

Length of output: 5345


🏁 Script executed:

#!/bin/bash
# Read the base_api.rb to understand base_delete
cat ./lib/mailtrap/base_api.rb

Repository: mailtrap/mailtrap-ruby

Length of output: 2361


🏁 Script executed:

#!/bin/bash
# Read the Client class to see what delete returns
cat ./lib/mailtrap/client.rb | head -250

Repository: mailtrap/mailtrap-ruby

Length of output: 7799


🏁 Script executed:

#!/bin/bash
# Also check projects_api.rb to see the full context
cat ./lib/mailtrap/projects_api.rb

Repository: mailtrap/mailtrap-ruby

Length of output: 1692


🏁 Script executed:

#!/bin/bash
# Check other delete method YARD docs for consistency
grep -A 3 "def delete(" lib/mailtrap/*.rb | grep -E "(def delete|@return)"

Repository: mailtrap/mailtrap-ruby

Length of output: 467


🏁 Script executed:

#!/bin/bash
# Check a couple of other spec files to see if they also expect hash return
grep -A 2 "it 'returns" spec/mailtrap/*_spec.rb | grep -E "(it 'returns|expect.*delete)"

Repository: mailtrap/mailtrap-ruby

Length of output: 1662


Update #delete YARD documentation to reflect actual return value

The YARD comment for delete states # @return nil, but the spec in spec/mailtrap/projects_api_spec.rb expects it to return { id: project_id }. The implementation delegates to base_delete, which returns the raw response from client.delete.

Update the documentation to match the actual behavior:

-    # @return nil
+    # @return [Hash] Deleted project identifier, e.g. { id: project_id }
🤖 Prompt for AI Agents
In lib/mailtrap/projects_api.rb around lines 50 to 56, the YARD comment for
#delete incorrectly states `@return nil` while the method actually returns the
raw client response (expected by specs to be `{ id: project_id }`); update the
YARD block to reflect the actual return value (for example `# @return [Hash] the
deleted project response, e.g. { id: project_id }`) and ensure any other related
tags/comments describe that it delegates to base_delete/client.delete.


def build_entity(options, response_class)
response_class.new(
**options.slice(*(response_class.members - [:inboxes])),
inboxes: options[:inboxes]&.map do |inbox|
inbox.is_a?(Mailtrap::Inbox) ? inbox : Mailtrap::Inbox.new(**inbox)
end
)
end

private

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

def wrap_request(options)
{ project: options }
end
end
end
Loading