Skip to content

add projects API #67

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

add projects API #67

wants to merge 4 commits into from

Conversation

jurgens
Copy link

@jurgens jurgens commented Aug 5, 2025

Motivation

add API support for projects

Changes

  • add projects API wrapper and project struct
  • add specs and rubydocs

Summary by CodeRabbit

  • New Features

    • Introduced project management capabilities, allowing users to list, retrieve, create, update, and delete projects via the API.
  • Bug Fixes

    • Improved error handling for authorization failures and invalid project operations.
  • Tests

    • Added comprehensive tests to verify project management functionality and error scenarios.
  • Documentation

    • Enhanced inline documentation for new project-related features and methods.

Copy link

coderabbitai bot commented Aug 5, 2025

Walkthrough

This change introduces a new Mailtrap::Project data structure and a Mailtrap::ProjectsAPI class for managing projects via the Mailtrap API. The main library file is updated to require the new API module. Comprehensive RSpec tests and VCR cassettes are added to validate project creation, retrieval, updating, deletion, and error handling.

Changes

Cohort / File(s) Change Summary
Mailtrap Library Integration
lib/mailtrap.rb
Adds require_relative 'mailtrap/projects_api' to integrate the new Projects API functionality into the main Mailtrap library.
Project Data Structure
lib/mailtrap/project.rb
Introduces Mailtrap::Project as a keyword-initialized struct with attributes (id, name, share_links, inboxes, permissions), custom initialization, newly_created?, and to_h methods.
Projects API Implementation
lib/mailtrap/projects_api.rb
Adds Mailtrap::ProjectsAPI class with methods for listing, retrieving, creating, updating, and deleting projects, using a base API module and response mapping to Project objects.
Project Data Structure Tests
spec/mailtrap/project_spec.rb
Adds RSpec tests for Mailtrap::Project covering initialization, newly_created?, and to_h method behavior, including edge cases for nil attributes.
Projects API Tests
spec/mailtrap/projects_api_spec.rb
Adds RSpec tests for Mailtrap::ProjectsAPI methods, verifying correct mapping, error handling, and integration with VCR for HTTP interaction simulation.
VCR Cassette Fixtures
spec/fixtures/vcr_cassettes/Mailtrap_ProjectsAPI/_list/maps_response_data_to_Project_objects.yml,
spec/fixtures/vcr_cassettes/Mailtrap_ProjectsAPI/_list/when_api_key_is_incorrect/raises_authorization_error.yml
Adds VCR cassette YAML files capturing Mailtrap API responses for project listing and authorization error scenarios, supporting automated test replay.

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant ProjectsAPI
  participant MailtrapAPI

  Client->>ProjectsAPI: list/get/create/update/delete
  ProjectsAPI->>MailtrapAPI: HTTP request (GET/POST/PATCH/DELETE)
  MailtrapAPI-->>ProjectsAPI: API response (JSON)
  ProjectsAPI-->>Client: Project object(s) or error
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • i7an
  • IgorDobryn

Poem

A rabbit hopped in code delight,
New projects now are in plain sight!
APIs to fetch, to build, to mend,
With structs and specs that never end.
VCR cassettes keep tests in line—
This patch is looking mighty fine!
🐇✨

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 51fc8bb and bf7278a.

📒 Files selected for processing (1)
  • lib/mailtrap/project.rb (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • lib/mailtrap/project.rb
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (5)
lib/mailtrap/project.rb (3)

6-6: Fix documentation comment.

The documentation refers to "contact ID" but should reference "project ID" since this is a Project struct.

-  # @attr_reader id [String] The contact ID
+  # @attr_reader id [String] The project ID

24-27: Fix documentation comment and approve implementation.

The documentation refers to "contact" but should reference "project". The implementation logic is correct.

-    # @return [Boolean] Whether the contact was newly created
+    # @return [Boolean] Whether the project was newly created

29-32: Fix documentation comment and approve implementation.

The documentation refers to "contact attributes" but should reference "project attributes". The implementation using super.compact is correct for removing nil values.

-    # @return [Hash] The contact attributes as a hash
+    # @return [Hash] The project attributes as a hash
spec/mailtrap/projects_api_spec.rb (2)

80-93: Remove duplicate test context.

The "with hash request" context duplicates the main create test with identical request data and expectations. Consider removing this redundant test case.

-    context 'with hash request' do
-      let(:request) do
-        {
-          name: 'New Project'
-        }
-      end
-
-      it 'maps response data to Project object' do
-        expect(create).to be_a(Mailtrap::Project)
-        expect(create).to have_attributes(
-          name: 'New Project'
-        )
-      end
-    end

133-146: Remove duplicate test context.

Similar to the create method, this "with hash request" context duplicates the main update test. Consider removing this redundant test case.

-    context 'with hash request' do
-      let(:request) do
-        {
-          name: 'Updated Project'
-        }
-      end
-
-      it 'maps response data to Project object' do
-        expect(update).to be_a(Mailtrap::Project)
-        expect(update).to have_attributes(
-          name: 'Updated Project'
-        )
-      end
-    end
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 022fa3b and 0c1090c.

📒 Files selected for processing (7)
  • lib/mailtrap.rb (1 hunks)
  • lib/mailtrap/project.rb (1 hunks)
  • lib/mailtrap/projects_api.rb (1 hunks)
  • spec/fixtures/vcr_cassettes/Mailtrap_ProjectsAPI/_list/maps_response_data_to_Project_objects.yml (1 hunks)
  • spec/fixtures/vcr_cassettes/Mailtrap_ProjectsAPI/_list/when_api_key_is_incorrect/raises_authorization_error.yml (1 hunks)
  • spec/mailtrap/project_spec.rb (1 hunks)
  • spec/mailtrap/projects_api_spec.rb (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
spec/mailtrap/project_spec.rb (1)
lib/mailtrap/project.rb (2)
  • newly_created? (25-27)
  • to_h (30-32)
lib/mailtrap/projects_api.rb (1)
lib/mailtrap/base_api.rb (7)
  • supported_options (27-29)
  • response_class (31-33)
  • base_list (67-70)
  • base_get (46-49)
  • base_create (51-55)
  • base_update (57-61)
  • base_delete (63-65)
🔇 Additional comments (14)
spec/fixtures/vcr_cassettes/Mailtrap_ProjectsAPI/_list/maps_response_data_to_Project_objects.yml (1)

1-167: LGTM!

This VCR cassette properly captures a 401 Unauthorized response scenario for testing API error handling. The structure and content are appropriate for testing the ProjectsAPI authentication flow.

lib/mailtrap/project.rb (2)

11-18: LGTM!

The struct definition with keyword initialization is well-structured and follows Ruby conventions.


19-22: LGTM!

The custom initialize method properly extracts the action parameter before calling the parent constructor. This approach allows the struct to track creation/update state while maintaining the struct's functionality.

lib/mailtrap.rb (1)

11-11: LGTM!

The require statement is properly placed and follows the established pattern for integrating API modules into the main library.

spec/fixtures/vcr_cassettes/Mailtrap_ProjectsAPI/_list/when_api_key_is_incorrect/raises_authorization_error.yml (1)

1-167: No duplication detected—fixtures cover distinct scenarios
Both VCR cassettes exercise different responses (one maps project data; the other returns a 401 error with {"error":"Incorrect API token"}), and the header differences you saw (nonces, request IDs, runtimes, etc.) are expected dynamic values. These files serve unique test purposes, so this duplication concern can be closed.

spec/mailtrap/project_spec.rb (3)

3-60: LGTM!

The initialization tests provide excellent coverage of the Project struct, including complex nested data structures for share_links, inboxes, and permissions. The test data is realistic and comprehensive.


62-105: LGTM!

The newly_created? method tests cover all relevant scenarios:

  • Action is 'created' → returns true
  • Action is 'updated' → returns false
  • Action is nil → returns true

This thoroughly validates the method's logic from lib/mailtrap/project.rb.


107-183: LGTM!

The to_h method tests comprehensively verify:

  • Hash conversion with all attributes present
  • Proper handling of nil values (compact behavior)

This validates the super.compact implementation in the Project struct.

spec/mailtrap/projects_api_spec.rb (4)

1-8: LGTM!

Good use of frozen string literal and proper RSpec setup with VCR integration. The environment variable fallbacks provide flexibility for local testing.


9-31: Comprehensive test coverage for the list method.

Good testing of both successful response mapping and authorization error handling. The error assertions properly validate error type, message content, and the messages array.


33-62: Well-structured get method tests.

The test appropriately creates a project dependency for the positive test case and properly validates both successful retrieval and not found error scenarios.


172-197: Proper delete method testing.

Good coverage of both successful deletion (returning nil) and not found error scenarios. The test structure is consistent with other method tests.

lib/mailtrap/projects_api.rb (2)

1-13: Proper API class setup.

Good use of frozen string literal, appropriate dependencies, and correct BaseAPI integration. The supported options and response class are properly configured.


57-66: Private methods correctly implemented.

The base_path constructs the appropriate API endpoint and wrap_request properly wraps options in the expected project key format.

@yanchuk yanchuk requested review from IgorDobryn and i7an August 5, 2025 14:20
@yanchuk yanchuk linked an issue Aug 5, 2025 that may be closed by this pull request
Comment on lines +13 to +17
expect(list).to all(be_a(Mailtrap::Project))
expect(list.first).to have_attributes(
id: be_a(Integer),
name: be_a(String)
)
Copy link
Contributor

Choose a reason for hiding this comment

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

all(be_a(Mailtrap::Project)) already ensures specific type. What is the intention for asserting specific attributes on first item?

end

# @return [Boolean] Whether the project was newly created
def newly_created?
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 not needed for projects api.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support of Emails Sandbox (Testing) API: Projects
3 participants