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
28 changes: 28 additions & 0 deletions examples/oauth_grants.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require_relative "../lib/resend"

raise if ENV["RESEND_API_KEY"].nil?

Resend.api_key = ENV["RESEND_API_KEY"]

def list
grants = Resend::OAuthGrants.list
puts grants
end

def list_paginated
paginated_grants = Resend::OAuthGrants.list({ limit: 10, after: "grant_id_here" })
puts "Paginated response:"
puts paginated_grants
puts "Has more: #{paginated_grants[:has_more]}" if paginated_grants[:has_more]
end

def revoke
grant = Resend::OAuthGrants.revoke("oauth_grant_id_here")
puts grant
end

list
# list_paginated
# revoke
1 change: 1 addition & 0 deletions lib/resend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
require "resend/logs"
require "resend/topics"
require "resend/webhooks"
require "resend/oauth_grants"
require "resend/automations"
require "resend/automations/runs"
require "resend/events"
Expand Down
20 changes: 20 additions & 0 deletions lib/resend/oauth_grants.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module Resend
# oauth grants api wrapper
module OAuthGrants
class << self
# https://resend.com/docs/api-reference/oauth/list-grants
def list(params = {})
path = Resend::PaginationHelper.build_paginated_path("oauth/grants", params)
Resend::Request.new(path, {}, "get").perform
end

# https://resend.com/docs/api-reference/oauth/revoke-grant
def revoke(oauth_grant_id = "")
path = "oauth/grants/#{oauth_grant_id}"
Resend::Request.new(path, {}, "delete").perform
end
end
end
end
77 changes: 77 additions & 0 deletions spec/oauth_grants_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# frozen_string_literal: true

RSpec.describe "OAuth Grants" do
shared_examples "oauth grants api" do
describe "list" do
it "should list oauth grants" do

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P3: Custom agent: No should in tests

Use a declarative description without "should". For example, "lists oauth grants" is clearer and more direct — it describes what the test verifies rather than what the code should hypothetically do.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At spec/oauth_grants_spec.rb, line 6:

<comment>Use a declarative description without "should". For example, "lists oauth grants" is clearer and more direct — it describes what the test verifies rather than what the code should hypothetically do.</comment>

<file context>
@@ -0,0 +1,77 @@
+RSpec.describe "OAuth Grants" do
+  shared_examples "oauth grants api" do
+    describe "list" do
+      it "should list oauth grants" do
+        resp = {
+          "object": "list",
</file context>

resp = {
"object": "list",
"has_more": false,
"data": [
{
"id": "b6d24b8e-af0b-4c3c-be0c-359bbd97381e",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2: The test mock responses use symbol keys for nested objects (via "id": Ruby syntax which creates :id), and the assertions access nested fields with [:id] — but in production, nested hash keys remain as strings from JSON parsing.

The SDK's process_response (in request.rb) only transforms top-level keys to symbols via data.transform_keys!(&:to_sym). Nested hashes (like items in the data array) are not deep-transformed, so they retain their original string keys from the API response.

Because the mock bypasses perform entirely (via allow_any_instance_of(…).to receive(:perform).and_return(resp)), no key transformation happens at all — the mock is returned as-is with symbol keys. This means the tests pass with mocks but the assertions would fail with actual API data.

Consider using string keys ('id' => …) in the mock response for nested objects and accessing nested fields with string keys (['id']) in assertions to match the real response format.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At spec/oauth_grants_spec.rb, line 12:

<comment>The test mock responses use symbol keys for nested objects (via `"id":` Ruby syntax which creates `:id`), and the assertions access nested fields with `[:id]` — but in production, nested hash keys remain as strings from JSON parsing.

The SDK's `process_response` (in `request.rb`) only transforms top-level keys to symbols via `data.transform_keys!(&:to_sym)`. Nested hashes (like items in the `data` array) are **not** deep-transformed, so they retain their original string keys from the API response.

Because the mock bypasses `perform` entirely (via `allow_any_instance_of(…).to receive(:perform).and_return(resp)`), no key transformation happens at all — the mock is returned as-is with symbol keys. This means the tests pass with mocks but the assertions would fail with actual API data.

Consider using string keys (`'id' => …`) in the mock response for nested objects and accessing nested fields with string keys (`['id']`) in assertions to match the real response format.</comment>

<file context>
@@ -0,0 +1,77 @@
+          "has_more": false,
+          "data": [
+            {
+              "id": "b6d24b8e-af0b-4c3c-be0c-359bbd97381e",
+              "client_id": "client_123",
+              "scopes": ["emails:send"],
</file context>

"client_id": "client_123",
"scopes": ["emails:send"],
"resource": nil,
"created_at": "2023-04-21T01:31:02.671414+00:00",
"revoked_at": nil,
"revoked_reason": nil,
"client": {
"name": "My App",
"logo_uri": "https://example.com/logo.png"
}
}
]
}
allow_any_instance_of(Resend::Request).to receive(:perform).and_return(resp)
grants = Resend::OAuthGrants.list
expect(grants[:data].length).to eql(1)
expect(grants[:data].first[:id]).to eql("b6d24b8e-af0b-4c3c-be0c-359bbd97381e")
expect(grants[:has_more]).to be(false)
end

it "should build a paginated path" do

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P3: Custom agent: No should in tests

Use a declarative description without "should". "builds a paginated path" is more direct and follows the rule's guidance.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At spec/oauth_grants_spec.rb, line 33:

<comment>Use a declarative description without "should". "builds a paginated path" is more direct and follows the rule's guidance.</comment>

<file context>
@@ -0,0 +1,77 @@
+        expect(grants[:has_more]).to be(false)
+      end
+
+      it "should build a paginated path" do
+        expect(Resend::PaginationHelper).to receive(:build_paginated_path)
+          .with("oauth/grants", { limit: 10 })
</file context>

expect(Resend::PaginationHelper).to receive(:build_paginated_path)
.with("oauth/grants", { limit: 10 })
.and_return("oauth/grants?limit=10")
allow_any_instance_of(Resend::Request).to receive(:perform).and_return({})
Resend::OAuthGrants.list({ limit: 10 })
end
end

describe "revoke" do
it "should revoke an oauth grant" do
resp = {
"object": "oauth_grant",
"id": "b6d24b8e-af0b-4c3c-be0c-359bbd97381e",
"revoked_at": "2023-04-22T10:00:00.000000+00:00",
"revoked_reason": "user_requested"
}
allow_any_instance_of(Resend::Request).to receive(:perform).and_return(resp)
grant = Resend::OAuthGrants.revoke("b6d24b8e-af0b-4c3c-be0c-359bbd97381e")
expect(grant[:id]).to eql("b6d24b8e-af0b-4c3c-be0c-359bbd97381e")
expect(grant[:revoked_reason]).to eql("user_requested")
end
end
end

context "static api_key" do
before do
Resend.configure do |config|
config.api_key = "re_123"
end
end

include_examples "oauth grants api"
end

context "dynamic api_key" do
before do
Resend.configure do |config|
config.api_key = -> { "re_123" }
end
end

include_examples "oauth grants api"
end
end