Skip to content

Add billing API#93

Merged
i7an merged 5 commits intomainfrom
MT-19862-billing-resources
Mar 2, 2026
Merged

Add billing API#93
i7an merged 5 commits intomainfrom
MT-19862-billing-resources

Conversation

@DagonWat
Copy link
Contributor

@DagonWat DagonWat commented Feb 17, 2026

Motivation

  • Add Billing API

Changes

  • Add new model and new API handler

Summary by CodeRabbit

  • New Features

    • Added Billing API integration to retrieve account billing usage information, including billing cycles and testing/sending plan usage data.
  • Documentation

    • Updated changelog and API reference documentation to include Billing API and Templates API entries; refreshed documentation URL references.
  • Tests

    • Added comprehensive test coverage for Billing API functionality and authentication error handling.

@coderabbitai
Copy link

coderabbitai bot commented Feb 17, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a94d87f and bb0f7b8.

📒 Files selected for processing (11)
  • CHANGELOG.md
  • README.md
  • examples/billing_api.rb
  • lib/mailtrap.rb
  • lib/mailtrap/billing_api.rb
  • lib/mailtrap/billing_usage.rb
  • lib/mailtrap/project.rb
  • spec/fixtures/vcr_cassettes/Mailtrap_BillingAPI/_usage/maps_response_data_to_Billing_object.yml
  • spec/fixtures/vcr_cassettes/Mailtrap_BillingAPI/_usage/when_api_key_is_incorrect/raises_authorization_error.yml
  • spec/mailtrap/billing_api_spec.rb
  • spec/mailtrap/billing_usage_spec.rb
✅ Files skipped from review due to trivial changes (2)
  • CHANGELOG.md
  • spec/fixtures/vcr_cassettes/Mailtrap_BillingAPI/_usage/when_api_key_is_incorrect/raises_authorization_error.yml
🚧 Files skipped from review as they are similar to previous changes (5)
  • lib/mailtrap.rb
  • lib/mailtrap/billing_api.rb
  • lib/mailtrap/project.rb
  • spec/mailtrap/billing_api_spec.rb
  • README.md

📝 Walkthrough

Walkthrough

Introduces a new Billing API module to the Mailtrap gem. Adds BillingAPI class with a usage method to fetch billing information, backed by a BillingUsage data transfer object. Includes documentation, examples, comprehensive tests with VCR fixtures, and library integration updates.

Changes

Cohort / File(s) Summary
Billing API Core
lib/mailtrap/billing_api.rb, lib/mailtrap/billing_usage.rb
New BillingAPI class wrapping a GET request to /api/accounts/{account_id}/billing/usage endpoint; returns BillingUsage DTO with nested billing, testing, and sending attributes.
Library Integration
lib/mailtrap.rb, lib/mailtrap/project.rb
Added require_relative for billing_api module; updated documentation reference URL in Project class.
Documentation & Examples
CHANGELOG.md, README.md, examples/billing_api.rb
Added Billing API entry to changelog; updated README with API references; added Ruby usage example demonstrating client instantiation and usage method invocation.
Test Coverage
spec/mailtrap/billing_api_spec.rb, spec/mailtrap/billing_usage_spec.rb
New test suites covering BillingAPI#usage method with success and authorization error scenarios; BillingUsage DTO initialization with nested attributes validation.
Test Fixtures
spec/fixtures/vcr_cassettes/Mailtrap_BillingAPI/_usage/*
VCR cassettes capturing HTTP interactions: successful billing usage response (200) and incorrect API token error response (401).

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested reviewers

  • IgorDobryn

Poem

🐰 A billing API hops into place,
With usage data we now can trace,
From testing plans to sending deals,
The Mailtrap ecosystem feels
Complete with fixtures, tests, and cheer! 💚

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description is largely incomplete; it covers Motivation and Changes sections but lacks detailed change descriptions, testing instructions, and other template sections. Expand Changes section with specific details about added classes/methods; add How to test section with test instructions; optionally include Images/GIFs section if applicable.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Add billing API' directly and clearly summarizes the main change in the changeset, which introduces a new Billing API module with supporting models and tests.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch MT-19862-billing-resources

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@DagonWat DagonWat marked this pull request as draft February 17, 2026 13:19
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: 1

🧹 Nitpick comments (1)
spec/mailtrap/billing_api_spec.rb (1)

27-33: Consider simplifying the error expectation block.

The block form of raise_error works but could be made more concise. That said, since it intentionally tests both error.message (string) and error.messages (array), this is reasonable if the two accessors have distinct behavior.

♻️ Optional: Simplified error assertion (if `messages` check isn't needed separately)
-      it 'raises authorization error' do
-        expect { get }.to raise_error do |error|
-          expect(error).to be_a(Mailtrap::AuthorizationError)
-          expect(error.message).to include('Incorrect API token')
-          expect(error.messages.any? { |msg| msg.include?('Incorrect API token') }).to be true
-        end
-      end
+      it 'raises authorization error' do
+        expect { get }.to raise_error(Mailtrap::AuthorizationError, /Incorrect API token/)
+      end
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@spec/mailtrap/billing_api_spec.rb` around lines 27 - 33, The current block
form of expect { get }.to raise_error can be simplified: replace the multiline
block in the spec with a single matcher call like expect { get }.to
raise_error(Mailtrap::AuthorizationError, /Incorrect API token/) to assert both
the class and the message in one line; if you still need to assert the
error.messages array, keep a short two-step assertion instead—first use expect {
get }.to raise_error(Mailtrap::AuthorizationError) to capture the error, then
assert on error.messages to check the array contents (refer to the raise_error
matcher and the error.messages accessor used in the existing spec).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@lib/mailtrap/billing_api.rb`:
- Line 13: The YARD `@return` tag uses angle brackets instead of YARD's required
square-bracket syntax; update the doc comment that currently reads "@return
<Billing>" to "@return [Billing]" so the return type is correctly recognized by
YARD (i.e., replace the angle brackets with square brackets in the `@return` tag).

---

Nitpick comments:
In `@spec/mailtrap/billing_api_spec.rb`:
- Around line 27-33: The current block form of expect { get }.to raise_error can
be simplified: replace the multiline block in the spec with a single matcher
call like expect { get }.to raise_error(Mailtrap::AuthorizationError, /Incorrect
API token/) to assert both the class and the message in one line; if you still
need to assert the error.messages array, keep a short two-step assertion
instead—first use expect { get }.to raise_error(Mailtrap::AuthorizationError) to
capture the error, then assert on error.messages to check the array contents
(refer to the raise_error matcher and the error.messages accessor used in the
existing spec).

@i7an i7an force-pushed the MT-19862-billing-resources branch from a94d87f to e81d19c Compare March 2, 2026 11:42
@i7an i7an marked this pull request as ready for review March 2, 2026 11:46
@i7an i7an requested review from IgorDobryn, Copilot and i7an March 2, 2026 11:46
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a new Billing API surface to the Mailtrap Ruby SDK to fetch per-account billing cycle and usage information, along with tests and a usage example.

Changes:

  • Introduce Mailtrap::BillingAPI#usage and Mailtrap::BillingUsage DTO for mapping the billing usage response.
  • Add RSpec coverage (unit + VCR) and recorded cassettes for the new endpoint.
  • Update docs/examples entry points (README, example script, changelog) and refresh a Project docs link.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
spec/mailtrap/billing_usage_spec.rb Unit spec for the new BillingUsage DTO struct mapping.
spec/mailtrap/billing_api_spec.rb VCR-backed spec validating BillingAPI#usage behavior and auth error handling.
spec/fixtures/vcr_cassettes/Mailtrap_BillingAPI/_get/when_api_key_is_incorrect/raises_authorization_error.yml VCR cassette for unauthorized billing usage request.
spec/fixtures/vcr_cassettes/Mailtrap_BillingAPI/_get/maps_response_data_to_Billing_object.yml VCR cassette for successful billing usage request.
lib/mailtrap/project.rb Updates Project documentation link.
lib/mailtrap/billing_usage.rb Adds BillingUsage DTO struct.
lib/mailtrap/billing_api.rb Adds BillingAPI with #usage endpoint call and entity mapping.
lib/mailtrap.rb Wires BillingAPI into the main require tree.
examples/billing_api.rb Adds example demonstrating how to call the Billing API.
README.md Adds Billing example link to the examples list.
CHANGELOG.md Notes Billing API addition under Unreleased.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

let(:account_id) { ENV.fetch('MAILTRAP_ACCOUNT_ID', 1_111_111) }
let(:client) { Mailtrap::Client.new(api_key: ENV.fetch('MAILTRAP_API_KEY', 'local-api-key')) }

describe '#get' do
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

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

The example group is labeled describe '#get', but the API method being exercised is #usage. This makes the spec/cassette naming misleading (the VCR cassette path also ends up under _get). Rename the describe block (and corresponding cassette directory) to #usage for clarity and consistency with the rest of the API specs.

Suggested change
describe '#get' do
describe '#usage' do

Copilot uses AI. Check for mistakes.
README.md Outdated
- Templates CRUD – [`email_templates_api.rb`](examples/email_templates_api.rb)
- Action Mailer – [`action_mailer.rb`](examples/action_mailer.rb)
- Accounts API – [`accounts_api.rb`](examples/accounts_api.rb)
- Billing – [`billing_api.rb`](examples/billing_api.rb)
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

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

In the examples list, this entry is labeled just "Billing" while the other API entries are labeled with "... API" (e.g., "Accounts API"). Consider renaming it to "Billing API" to avoid ambiguity and keep the list consistent.

Suggested change
- Billing – [`billing_api.rb`](examples/billing_api.rb)
- Billing API [`billing_api.rb`](examples/billing_api.rb)

Copilot uses AI. Check for mistakes.
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.

🧹 Nitpick comments (1)
spec/mailtrap/billing_usage_spec.rb (1)

42-74: Consider reusing attributes in the expectation to avoid payload drift.

The expected structure is duplicated verbatim, which can become noisy to maintain.

♻️ Optional simplification
-    it 'creates a billing data with all attributes' do
-      expect(billing).to match_struct(
-        billing: {
-          cycle_start: '2024-02-15T21:11:59.624Z',
-          cycle_end: '2024-02-15T21:11:59.624Z'
-        },
-        testing: {
-          plan: {
-            name: 'Individual'
-          },
-          usage: {
-            sent_messages_count: {
-              current: 1234,
-              limit: 5000
-            },
-            forwarded_messages_count: {
-              current: 0,
-              limit: 100
-            }
-          }
-        },
-        sending: {
-          plan: {
-            name: 'Basic 10K'
-          },
-          usage: {
-            sent_messages_count: {
-              current: 6789,
-              limit: 10_000
-            }
-          }
-        }
-      )
-    end
+    it 'creates billing data with all attributes' do
+      expect(billing).to match_struct(attributes)
+    end
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@spec/mailtrap/billing_usage_spec.rb` around lines 42 - 74, The test
duplicates the expected billing payload inline; update the example to reuse the
existing attributes helper instead of hardcoding the whole structure: locate the
spec where `billing` is asserted with `match_struct(...)` and replace the
duplicated literal structure with a reference to the shared `attributes` (or
merge relevant keys from `attributes`) so the expectation derives values from
the single source of truth (`attributes`) rather than duplicating them.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@spec/mailtrap/billing_usage_spec.rb`:
- Around line 42-74: The test duplicates the expected billing payload inline;
update the example to reuse the existing attributes helper instead of hardcoding
the whole structure: locate the spec where `billing` is asserted with
`match_struct(...)` and replace the duplicated literal structure with a
reference to the shared `attributes` (or merge relevant keys from `attributes`)
so the expectation derives values from the single source of truth (`attributes`)
rather than duplicating them.

ℹ️ Review info

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a94d87f and 3e37b1c.

📒 Files selected for processing (11)
  • CHANGELOG.md
  • README.md
  • examples/billing_api.rb
  • lib/mailtrap.rb
  • lib/mailtrap/billing_api.rb
  • lib/mailtrap/billing_usage.rb
  • lib/mailtrap/project.rb
  • spec/fixtures/vcr_cassettes/Mailtrap_BillingAPI/_get/maps_response_data_to_Billing_object.yml
  • spec/fixtures/vcr_cassettes/Mailtrap_BillingAPI/_get/when_api_key_is_incorrect/raises_authorization_error.yml
  • spec/mailtrap/billing_api_spec.rb
  • spec/mailtrap/billing_usage_spec.rb
🚧 Files skipped from review as they are similar to previous changes (6)
  • examples/billing_api.rb
  • lib/mailtrap.rb
  • spec/fixtures/vcr_cassettes/Mailtrap_BillingAPI/_get/maps_response_data_to_Billing_object.yml
  • spec/mailtrap/billing_api_spec.rb
  • spec/fixtures/vcr_cassettes/Mailtrap_BillingAPI/_get/when_api_key_is_incorrect/raises_authorization_error.yml
  • CHANGELOG.md

@i7an i7an force-pushed the MT-19862-billing-resources branch from 3e37b1c to cda32ec Compare March 2, 2026 12:07
@i7an i7an force-pushed the MT-19862-billing-resources branch from cda32ec to 0170d27 Compare March 2, 2026 12:09
@i7an i7an merged commit 8fca8c1 into main Mar 2, 2026
4 checks passed
@i7an i7an deleted the MT-19862-billing-resources branch March 2, 2026 16:28
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.

4 participants