Skip to content

Add the simple-editor email, brand style and file endpoints - #40

Merged
dinomh merged 30 commits into
mainfrom
feature/cmac-1016-add-the-simple-editor-email-endpoints-to-the-ruby-sdk
Nov 12, 2026
Merged

dinomh merged 30 commits into
mainfrom
feature/cmac-1016-add-the-simple-editor-email-endpoints-to-the-ruby-sdk

Conversation

@tomasmartty

@tomasmartty tomasmartty commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Why?

The MailerLite API adds endpoints to read and edit emails that are built with the simple editor. It also adds two related endpoints: brand styles and files. This PR adds all 16 endpoints to the Ruby SDK, so that Ruby users can build and edit these emails without raw HTTP requests.

Merge and release this only after the endpoints are in the public API documentation.

Changes

  1. New MailerLite::Emails class (lib/mailerlite/emails/emails.rb)

    Method Endpoint
    get GET /emails/{email}
    get_html GET /emails/{email}/html
    get_block GET /emails/{email}/blocks/{block}
    create_block POST /emails/{email}/blocks
    update_block PATCH /emails/{email}/blocks/{block}
    delete_block DELETE /emails/{email}/blocks/{block}
    move_block POST /emails/{email}/blocks/{block}/move
    validate_block POST /emails/blocks/validate
    update_settings PATCH /emails/{email}/settings
    validate_settings POST /emails/{email}/settings/validate
    create_template POST /emails/{email}/templates
    list_versions GET /emails/{email}/versions
    create_version POST /emails/{email}/versions
    restore_version POST /emails/{email}/versions/{version}/restore
    • One ID is a positional argument. When a method needs more values, all of them are keyword arguments, as in Campaigns.
    • editor_content, placement and settings are Hashes. The SDK sends them as they are, so the camelCase keys of editor_content do not change.
    • delete_block sends {"etag": ...} in the DELETE body. create_version sends no body.
  2. New MailerLite::BrandStyles class: get calls GET /brand-styles. brand is null when the account has no brand styles.

  3. New MailerLite::Files class: list calls GET /files, with the name and type filters, sort, page and limit. The name is Files, not File, so that it does not hide the File class of Ruby.

  4. Responses and errors

    • The new methods return an HTTP::Response, like all other methods of the gem. They do not raise for an API error.
    • The response has two new methods:
      • api_error returns nil for a 2xx response. For other responses, it returns a MailerLite::ApiError with status, code, etag, errors and api_message. It also works when the body is not JSON, for example a 503 HTML page.
      • idempotent_replay? is true when the API returned a stored response (Idempotent-Replayed: true, without case).
    • An old etag gives 409 with code == MailerLite::Emails::VERSION_CONFLICT and the current etag.
  5. Checks before the request: the SDK checks a value only when the API cannot see the problem, or when the SDK code must read the value to build the request. If a check fails, the SDK raises the error and sends no request.

    • Email, block and version IDs in the URL path must contain only ASCII digits. Integers are also accepted. Otherwise the SDK raises MailerLite::EmptyIdError, MailerLite::InvalidIdError or TypeError. Reason: the API decodes an escaped / in a path, so an escaped ID could reach a different endpoint.
    • update_settings merges the etag into the settings Hash. A settings value that is not a Hash raises TypeError, and a Hash with an etag key raises ArgumentError.
    • The filter of Files#list must be a Hash, because the SDK builds filter[name] from its keys.
    • The SDK sends all other values as the caller gives them: placement (also block_id for start and end), the etag (also an empty etag), type, editor_content, the template name and the settings of validate_settings. The API checks them and answers with 422, or with valid: false on the two validate endpoints.
  6. Idempotency keys: the 8 email writes accept an optional idempotency_key: argument, and the SDK sends it as the Idempotency-Key header. If a request with the same key already succeeded, the API returns the stored response again and does not do the write two times. This makes it safe to retry a write after a network error. Without a key (nil), the SDK sends no header. The SDK sends a key as the caller gives it, and the API returns 400 for a key that is not valid.

  7. Campaigns (lib/mailerlite/campaigns/campaigns.rb): Campaigns#create already sends each email Hash as it is. The YARD doc now describes the editor and template_id keys, and there is a new constant, Campaigns::EDITOR_SIMPLE. Set editor: MailerLite::Campaigns::EDITOR_SIMPLE to create an email with the simple editor, and then edit it with MailerLite::Emails. Only create accepts these keys.

  8. Shared helpers: lib/mailerlite/params.rb (the ID and settings checks, and the query builder), request.rb, api_response.rb and errors.rb. The query builder drops nil values and keeps empty strings.

  9. Constants: block types, block positions (AT_START, AT_END, BEFORE, AFTER, because END is a Ruby keyword), brand modes, file types and file sorts.

  10. README: new "Emails", "Brand styles" and "Files" sections, with one example for each method, a "Retry a write with an idempotency key" example, and a campaign example with the simple editor.

  11. Version: 2.1.0 in lib/mailerlite/version.rb. The CHANGELOG.md entries are under ## [Unreleased], after the entries that main already has there.

Compatibility

All changes add to the public API. No existing method signature or return value changes, and the existing methods do not change their behavior.

Tests

  • The new specs use WebMock stubs and JSON fixtures with example values. They do not use VCR cassettes, so they need no account.
  • Each new method has a spec. The spec checks the HTTP method, the full URL with the query string, the headers, the exact raw body and the response.
  • Other specs cover these cases:
    • 409 with version_conflict, 422 with errors, and 503 with an HTML body;
    • create_version with 201 and with 200, and with an empty raw body;
    • brand: null, and a file name filter with spaces;
    • each ID check, and that no request is sent;
    • values that the API checks (an empty etag, a placement without block_id, a wrong type): the SDK sends the exact body and returns the API error;
    • the idempotency key header, the replay flag (true, True, no header), and keys that are not valid, which the SDK sends as given;
    • the campaign create body: editor is present and content is absent.
  • rspec (246 examples, after the rebase onto main) passes on Ruby 3.1. RuboCop finds no new offenses.
  • All new README examples were run against the WebMock stubs.
  • All 16 endpoints were also run against a development instance of the API, through the SDK. These runs included a full flow that passes each new etag to the next write, the error cases 409 (old etag), 404 (unknown block) and 422 (invalid file type), and an idempotency check: a retry with the same key returned the stored 201 with the same block ID, the same key with a different body returned 422, and a retry without a key returned 409.

How to test

  1. Run bundle exec rspec spec/*rspec.rb.
  2. Get an API token for a test account. Create a campaign with editor: MailerLite::Campaigns::EDITOR_SIMPLE, and get the email ID from data.emails[0].id of the response.
  3. Run the README examples in this order: "Get an email", "Insert a block", "Move a block", "Delete a block". Pass the etag of each response to the next write.
  4. Make sure that a write with an old etag returns a response where api_error.code is MailerLite::Emails::VERSION_CONFLICT.
  5. Run the README example "Retry a write with an idempotency key" two times with the same key and the same etag. Make sure that the second response has the same block ID and that idempotent_replay? is true.

How to release

  1. Wait until the endpoints are in the public API documentation.
  2. In CHANGELOG.md, change ## [Unreleased] to ## [2.1.0] - <date>. This entry also has the changes that are already on main.
  3. Merge, then push the tag v2.1.0. The tag starts the publish workflow. This is a minor version because all changes are additions.

Rollback

No data or configuration changes. To roll back, revert the merge commit and release a new patch version.

🤖 Generated with Claude Code

@tomasmartty tomasmartty self-assigned this Oct 29, 2026
@tomasmartty
tomasmartty marked this pull request as ready for review October 29, 2026 15:31
@tomasmartty
tomasmartty requested a review from dinomh October 29, 2026 15:32
@mailerlite-kritika

mailerlite-kritika Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Re-runViewConfidence Score: 4/5

[Medium risk] The description says to merge and release only after the endpoints appear in the public API documentation, and the release steps say to wait for that too. Nothing in the repository, the description or the account shows that this was met, so it is an unmet prerequisite for merging. The account also reports the CHANGELOG heading as "[Unreleased]", while the description says the 2.1.0 entry carries an "Unreleased" date. The change is additive, with no existing behavior changed, and the new code …

Findings

Earlier findings (1 dismissed)

Summary

Adds simple-editor Emails, BrandStyles and Files endpoints with shared request helpers

Adds Emails, BrandStyles and Files classes. They send requests through a shared Request.call, which extends each HTTP::Response with api_error and idempotent_replay?. Params checks only URL-path IDs, the update_settings Hash and the Files#list filter before any request is sent. Since the last review the CHANGELOG heading moved from ## [2.1.0] - Unreleased to ## [Unreleased], which fits the other pending entries already in the file. version.rb still says 2.1.0.

What's good

  • Path IDs are validated strictly (ASCII digits only, \A/\z anchors, encoding checked) before the request is built, which closes the escaped-slash path risk.
  • The specs check the HTTP method, the raw body, the headers and the query string, and they cover error paths such as 409, 422 and a non-JSON 503.
  • ApiError keeps only plain values, so it survives Marshal, and it parses non-JSON bodies safely.
Review context · 0/5 commands
  • Commands run: none

Reviews (6) · Last reviewed commit: "refactor: leave value checks to the API" · claude-sonnet-5-5

Comment thread spec/emails_rspec.rb Resolved
tomasmartty and others added 23 commits November 11, 2026 16:31
Add the JSON fixtures for the email, brand style and file endpoints.
Add a helper that reads them in the specs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add the error classes for the client-side checks.
Add the Params module. It checks path IDs, etags, Hash and String
values, block placements, settings and idempotency keys. It also
builds query parameters and drops nil values.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add the ApiResponse module. The new resources extend each response
with it. idempotent_replay? reads the Idempotent-Replayed header
without case. api_error returns nil for a 2xx status, and an ApiError
for other statuses.

ApiError has the status, the API code, the etag, the field errors and
the API message. It keeps only plain values, so Marshal works. The SDK
does not raise it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add Request.call. It sends a JSON body only when there is one, so a
write without a body sends no body. It sends PATCH and DELETE bodies,
adds the query, and adds the Idempotency-Key header when the caller
gives a key. It reads the base URL when the request runs, and returns
the response with the ApiResponse methods.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add the Emails class for the emails built with the simple editor, and
the constants for the version conflict code, the block types, the
block positions and the brand modes. Add Emails#get and its README
section.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add Emails#get_html. It returns the HTML of an email and its etag.
Add its README section.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add Emails#get_block. It returns a block of an email, its placement
and the etag of the email. Add its README section.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add Emails#create_block. It inserts a block in an email. It checks the
email ID, the etag, the block type, the content and the block_id of
the placement before the request. Add its README section.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add Emails#update_block. It sends a PATCH request that replaces the
content of a block. Add its README section.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add Emails#delete_block. It sends a DELETE request with the etag in
the body, and returns the response with the new etag. Add its README
section.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add Emails#move_block. It moves a block to a new placement in the
email. Add its README section.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add Emails#validate_block. It checks a block without an email and
without an etag. Add its README section.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add Emails#update_settings. It sends a PATCH request with the keys of
the settings Hash and the etag. It rejects settings that contain an
etag key. Add its README section.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add Emails#validate_settings. It checks a change to the design
settings without an etag, and saves nothing. Add its README section.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add Emails#create_template. It saves an email as a new template. Add
its README section.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add Emails#list_versions. It lists the saved versions of an email,
with an optional page and limit. Add its README section.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add Emails#create_version. It saves a version of an email and sends
no request body. Add its README section.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add Emails#restore_version. It restores an email to a saved version
and returns the email with the new etag. Add its README section.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add the BrandStyles class and BrandStyles#get. It returns the brand,
default and preset palettes of the account. Add its README section.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add the Files class and Files#list. It lists the files of the media
library with an optional filter, sort, page and limit. It drops nil
values from the query. Add the constants for the file types and the
sort values, and the README section.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add the EDITOR_SIMPLE constant to Campaigns. Document the editor and
template_id keys of the campaign emails. Campaigns#create already
sends each email as it is, so the request code does not change. Add a
README section and tests for the request body.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add the optional idempotency_key keyword to the 8 email writes. The
SDK checks the key and sends it in the Idempotency-Key header. The
two validate methods do not accept a key. Add a README section that
shows a retry with the same key after a network error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Set the version to 2.1.0, because the change adds methods and does
not break the current methods. Add the CHANGELOG entry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tomasmartty and others added 7 commits November 11, 2026 16:32
The API uses block_id only for the before and after positions. For
the other positions, it discards the value without a check.

Before this change, the SDK checked block_id for each position. A
placement such as { position: 'end', block_id: nil } raised
EmptyIdError, but the API accepts it.

Now Params.placement checks block_id only for before and after. For
these positions, a missing block_id raises EmptyIdError. For the
other positions, the SDK removes block_id from the body that it sends.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The validate example used a button with attrs.href and content. The
API does not document this shape.

Now the example sends a paragraph with textAlign set to sideways, as
in the API documentation. A note tells that the block is not valid,
so the response has valid: false and errors. The matching spec sends
the same block and gets an invalid result.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
No spec uses this fixture now. The validate example and its spec show
an invalid block.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An empty string unsets only a brand colour. An empty editor document
unsets a footer text. For other colours, sizes, button.border_radius
and brand.style, an empty string gives a 422 error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The new specs expect the header Authorization: Bearer dummy_token, but
only the CI workflow set that token. With a different token or with no
token, the WebMock stubs did not match.

A shared context now sets the token before each example and restores
the old value after it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Remove doc comments that repeat the code in the internal Params and Request helpers and in the specs.
Remove notes that only explain names to the reviewer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The SDK now checks only the IDs in the URL path and the settings Hash of
update_settings. It sends the placement, the etag, the block values, the
template name and the idempotency key as the caller gives them, and the
API answers with its errors.

Remove EmptyEtagError and InvalidIdempotencyKeyError.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tomasmartty
tomasmartty force-pushed the feature/cmac-1016-add-the-simple-editor-email-endpoints-to-the-ruby-sdk branch from 761d723 to 9bc0b51 Compare November 11, 2026 15:33
@tomasmartty
tomasmartty added this pull request to stack #43 November 12, 2026 10:57
@dinomh
dinomh merged commit 42274b1 into main Nov 12, 2026
3 checks passed
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.

2 participants