Skip to content

Commit 2d3933d

Browse files
authored
feat: Pagination support for list methods (#126)
1 parent 42082cd commit 2d3933d

File tree

14 files changed

+361
-11
lines changed

14 files changed

+361
-11
lines changed

examples/api_keys.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ def list
1919
puts keys
2020
end
2121

22+
def list_paginated
23+
# List with pagination parameters
24+
paginated_keys = Resend::ApiKeys.list({ limit: 10, after: "key_id_here" })
25+
puts "Paginated response:"
26+
puts paginated_keys
27+
puts "Has more: #{paginated_keys[:has_more]}" if paginated_keys[:has_more]
28+
end
29+
2230
def remove
2331
key = Resend::ApiKeys.create({name: "t"})
2432
puts "created api key id: #{key[:id]}"

examples/audiences.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ def example
1919
audiences = Resend::Audiences.list
2020
puts audiences
2121

22+
# Example with pagination
23+
paginated_audiences = Resend::Audiences.list({ limit: 5 })
24+
puts "Paginated audiences (limit 5):"
25+
puts paginated_audiences
26+
2227
Resend::Audiences.remove audience[:id]
2328
puts "deleted #{audience[:id]}"
2429
end

examples/broadcasts.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
broadcasts = Resend::Broadcasts.list
4040
puts broadcasts
4141

42+
# Example with pagination
43+
paginated_broadcasts = Resend::Broadcasts.list({ limit: 15, after: "broadcast_id_here" })
44+
puts "Paginated broadcasts (limit 15, after broadcast_id_here):"
45+
puts paginated_broadcasts
46+
4247
retrieved = Resend::Broadcasts.get(broadcast[:id])
4348
puts "retrieved #{retrieved[:id]}"
4449

examples/contacts.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ def example
4343
contacts = Resend::Contacts.list(audience_id)
4444
puts contacts
4545

46+
# Example with pagination
47+
paginated_contacts = Resend::Contacts.list(audience_id, { limit: 10 })
48+
puts "Paginated contacts (limit 10):"
49+
puts paginated_contacts
50+
4651
# delete by id
4752
del = Resend::Contacts.remove(audience_id, contact[:id])
4853

examples/domains.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ def update
4949
def list
5050
domains = Resend::Domains.list
5151
puts domains
52+
53+
# Example with pagination
54+
paginated_domains = Resend::Domains.list({ limit: 20, before: "domain_id_here" })
55+
puts "Paginated domains (limit 20, before domain_id_here):"
56+
puts paginated_domains
5257
end
5358

5459
def remove

examples/with_pagination.rb

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "../lib/resend"
4+
5+
raise if ENV["RESEND_API_KEY"].nil?
6+
7+
Resend.api_key = ENV["RESEND_API_KEY"]
8+
9+
puts "=== Resend Ruby SDK Pagination Examples ==="
10+
puts
11+
12+
# API Keys pagination
13+
puts "1. API Keys Pagination:"
14+
puts "List first 5 API keys:"
15+
api_keys = Resend::ApiKeys.list({ limit: 5 })
16+
puts api_keys
17+
puts "Has more: #{api_keys[:has_more]}" if api_keys.key?(:has_more)
18+
puts
19+
20+
# Audiences pagination
21+
puts "2. Audiences Pagination:"
22+
puts "List first 10 audiences:"
23+
audiences = Resend::Audiences.list({ limit: 10 })
24+
puts audiences
25+
puts "Has more: #{audiences[:has_more]}" if audiences.key?(:has_more)
26+
puts
27+
28+
# If we have audiences, demonstrate pagination with 'after'
29+
if audiences[:data] && !audiences[:data].empty?
30+
first_audience_id = audiences[:data].first[:id]
31+
puts "List audiences after '#{first_audience_id}':"
32+
next_audiences = Resend::Audiences.list({ limit: 5, after: first_audience_id })
33+
puts next_audiences
34+
puts
35+
end
36+
37+
# Domains pagination
38+
puts "3. Domains Pagination:"
39+
puts "List first 3 domains:"
40+
domains = Resend::Domains.list({ limit: 3 })
41+
puts domains
42+
puts "Has more: #{domains[:has_more]}" if domains.key?(:has_more)
43+
puts
44+
45+
# Broadcasts pagination
46+
puts "4. Broadcasts Pagination:"
47+
puts "List first 2 broadcasts:"
48+
broadcasts = Resend::Broadcasts.list({ limit: 2 })
49+
puts broadcasts
50+
puts "Has more: #{broadcasts[:has_more]}" if broadcasts.key?(:has_more)
51+
puts
52+
53+
# Contacts pagination (requires an audience ID)
54+
puts "5. Contacts Pagination:"
55+
if audiences[:data] && !audiences[:data].empty?
56+
audience_id = audiences[:data].first[:id]
57+
if audience_id && !audience_id.empty?
58+
puts "List first 5 contacts from audience '#{audience_id}':"
59+
begin
60+
contacts = Resend::Contacts.list(audience_id, { limit: 5 })
61+
puts contacts
62+
puts "Has more: #{contacts[:has_more]}" if contacts.key?(:has_more)
63+
rescue Resend::Error => e
64+
puts "Error listing contacts: #{e.message}"
65+
puts "(This might happen if the audience has no contacts or access is restricted)"
66+
end
67+
else
68+
puts "Audience ID is empty, skipping contacts pagination example"
69+
end
70+
else
71+
puts "No audiences available for contacts pagination example"
72+
end
73+
puts
74+
75+
puts "=== Pagination Parameters ==="
76+
puts "• limit: Number of items to retrieve (1-100)"
77+
puts "• after: ID after which to retrieve more items"
78+
puts "• before: ID before which to retrieve more items"
79+
puts
80+
puts "Example usage:"
81+
puts "Resend::ApiKeys.list({ limit: 25, after: 'key_123' })"
82+
puts "Resend::Domains.list({ limit: 10, before: 'domain_456' })"
83+
puts "Resend::Contacts.list('audience_id', { limit: 50 })"

lib/resend.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
# Utils
77
require "httparty"
88
require "json"
9+
require "cgi"
910
require "resend/errors"
1011
require "resend/client"
1112
require "resend/request"
13+
require "resend/pagination_helper"
1214

1315
# API Operations
1416
require "resend/audiences"

lib/resend/api_keys.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ def create(params)
1111
end
1212

1313
# https://resend.com/docs/api-reference/api-keys/list-api-keys
14-
def list
15-
path = "api-keys"
14+
def list(params = {})
15+
path = Resend::PaginationHelper.build_paginated_path("api-keys", params)
1616
Resend::Request.new(path, {}, "get").perform
1717
end
1818

lib/resend/audiences.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ def get(audience_id = "")
1717
end
1818

1919
# https://resend.com/docs/api-reference/audiences/list-audiences
20-
def list
21-
path = "audiences"
20+
def list(params = {})
21+
path = Resend::PaginationHelper.build_paginated_path("audiences", params)
2222
Resend::Request.new(path, {}, "get").perform
2323
end
2424

lib/resend/broadcasts.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ def send(params = {})
2323
end
2424

2525
# https://resend.com/docs/api-reference/broadcasts/list-broadcasts
26-
def list
27-
path = "broadcasts"
26+
def list(params = {})
27+
path = Resend::PaginationHelper.build_paginated_path("broadcasts", params)
2828
Resend::Request.new(path, {}, "get").perform
2929
end
3030

0 commit comments

Comments
 (0)