Skip to content
Open
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
8 changes: 8 additions & 0 deletions examples/api_keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ def list
puts keys
end

def list_paginated
# List with pagination parameters
paginated_keys = Resend::ApiKeys.list({ limit: 10, after: "key_id_here" })
puts "Paginated response:"
puts paginated_keys
puts "Has more: #{paginated_keys[:has_more]}" if paginated_keys[:has_more]
end

def remove
key = Resend::ApiKeys.create({name: "t"})
puts "created api key id: #{key[:id]}"
Expand Down
5 changes: 5 additions & 0 deletions examples/audiences.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ def example
audiences = Resend::Audiences.list
puts audiences

# Example with pagination
paginated_audiences = Resend::Audiences.list({ limit: 5 })
puts "Paginated audiences (limit 5):"
puts paginated_audiences

Resend::Audiences.remove audience[:id]
puts "deleted #{audience[:id]}"
end
Expand Down
5 changes: 5 additions & 0 deletions examples/broadcasts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
broadcasts = Resend::Broadcasts.list
puts broadcasts

# Example with pagination
paginated_broadcasts = Resend::Broadcasts.list({ limit: 15, after: "broadcast_id_here" })
puts "Paginated broadcasts (limit 15, after broadcast_id_here):"
puts paginated_broadcasts

retrieved = Resend::Broadcasts.get(broadcast[:id])
puts "retrieved #{retrieved[:id]}"

Expand Down
5 changes: 5 additions & 0 deletions examples/contacts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ def example
contacts = Resend::Contacts.list(audience_id)
puts contacts

# Example with pagination
paginated_contacts = Resend::Contacts.list(audience_id, { limit: 10 })
puts "Paginated contacts (limit 10):"
puts paginated_contacts

# delete by id
del = Resend::Contacts.remove(audience_id, contact[:id])

Expand Down
5 changes: 5 additions & 0 deletions examples/domains.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ def update
def list
domains = Resend::Domains.list
puts domains

# Example with pagination
paginated_domains = Resend::Domains.list({ limit: 20, before: "domain_id_here" })
puts "Paginated domains (limit 20, before domain_id_here):"
puts paginated_domains
end

def remove
Expand Down
83 changes: 83 additions & 0 deletions examples/with_pagination.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# frozen_string_literal: true

require_relative "../lib/resend"

raise if ENV["RESEND_API_KEY"].nil?

Resend.api_key = ENV["RESEND_API_KEY"]

puts "=== Resend Ruby SDK Pagination Examples ==="
puts

# API Keys pagination
puts "1. API Keys Pagination:"
puts "List first 5 API keys:"
api_keys = Resend::ApiKeys.list({ limit: 5 })
puts api_keys
puts "Has more: #{api_keys[:has_more]}" if api_keys.key?(:has_more)
puts

# Audiences pagination
puts "2. Audiences Pagination:"
puts "List first 10 audiences:"
audiences = Resend::Audiences.list({ limit: 10 })
puts audiences
puts "Has more: #{audiences[:has_more]}" if audiences.key?(:has_more)
puts

# If we have audiences, demonstrate pagination with 'after'
if audiences[:data] && !audiences[:data].empty?
first_audience_id = audiences[:data].first[:id]
puts "List audiences after '#{first_audience_id}':"
next_audiences = Resend::Audiences.list({ limit: 5, after: first_audience_id })
puts next_audiences
puts
end

# Domains pagination
puts "3. Domains Pagination:"
puts "List first 3 domains:"
domains = Resend::Domains.list({ limit: 3 })
puts domains
puts "Has more: #{domains[:has_more]}" if domains.key?(:has_more)
puts

# Broadcasts pagination
puts "4. Broadcasts Pagination:"
puts "List first 2 broadcasts:"
broadcasts = Resend::Broadcasts.list({ limit: 2 })
puts broadcasts
puts "Has more: #{broadcasts[:has_more]}" if broadcasts.key?(:has_more)
puts

# Contacts pagination (requires an audience ID)
puts "5. Contacts Pagination:"
if audiences[:data] && !audiences[:data].empty?
audience_id = audiences[:data].first[:id]
if audience_id && !audience_id.empty?
puts "List first 5 contacts from audience '#{audience_id}':"
begin
contacts = Resend::Contacts.list(audience_id, { limit: 5 })
puts contacts
puts "Has more: #{contacts[:has_more]}" if contacts.key?(:has_more)
rescue Resend::Error => e
puts "Error listing contacts: #{e.message}"
puts "(This might happen if the audience has no contacts or access is restricted)"
end
else
puts "Audience ID is empty, skipping contacts pagination example"
end
else
puts "No audiences available for contacts pagination example"
end
puts

puts "=== Pagination Parameters ==="
puts "• limit: Number of items to retrieve (1-100)"
puts "• after: ID after which to retrieve more items"
puts "• before: ID before which to retrieve more items"
puts
puts "Example usage:"
puts "Resend::ApiKeys.list({ limit: 25, after: 'key_123' })"
puts "Resend::Domains.list({ limit: 10, before: 'domain_456' })"
puts "Resend::Contacts.list('audience_id', { limit: 50 })"
2 changes: 2 additions & 0 deletions lib/resend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
# Utils
require "httparty"
require "json"
require "cgi"
require "resend/errors"
require "resend/client"
require "resend/request"
require "resend/pagination_helper"

# API Operations
require "resend/audiences"
Expand Down
4 changes: 2 additions & 2 deletions lib/resend/api_keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ def create(params)
end

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

Expand Down
4 changes: 2 additions & 2 deletions lib/resend/audiences.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def get(audience_id = "")
end

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

Expand Down
4 changes: 2 additions & 2 deletions lib/resend/broadcasts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def send(params = {})
end

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

Expand Down
5 changes: 3 additions & 2 deletions lib/resend/contacts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ def get(audience_id, id)
# List contacts in an audience
#
# @param audience_id [String] the audience id
# @param params [Hash] optional pagination parameters
# https://resend.com/docs/api-reference/contacts/list-contacts
def list(audience_id)
path = "audiences/#{audience_id}/contacts"
def list(audience_id, params = {})
path = Resend::PaginationHelper.build_paginated_path("audiences/#{audience_id}/contacts", params)
Resend::Request.new(path, {}, "get").perform
end

Expand Down
6 changes: 3 additions & 3 deletions lib/resend/domains.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ def get(domain_id = "")
Resend::Request.new(path, {}, "get").perform
end

# https://resend.com/docs/api-reference/api-keys/list-api-keys
def list
path = "domains"
# https://resend.com/docs/api-reference/domains/list-domains
def list(params = {})
path = Resend::PaginationHelper.build_paginated_path("domains", params)
Resend::Request.new(path, {}, "get").perform
end

Expand Down
30 changes: 30 additions & 0 deletions lib/resend/pagination_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module Resend
# Helper class for building paginated query strings
class PaginationHelper
class << self
# Builds a paginated path with query parameters
#
# @param base_path [String] the base API path
# @param query_params [Hash] optional pagination parameters
# @option query_params [Integer] :limit Number of items to retrieve (max 100)
# @option query_params [String] :after ID after which to retrieve more items
# @option query_params [String] :before ID before which to retrieve more items
# @return [String] the path with query parameters
def build_paginated_path(base_path, query_params = nil)
return base_path if query_params.nil? || query_params.empty?

# Filter out nil values and convert to string keys
filtered_params = query_params.compact.transform_keys(&:to_s)

# Build query string
query_string = filtered_params.map { |k, v| "#{k}=#{CGI.escape(v.to_s)}" }.join("&")

return base_path if query_string.empty?

"#{base_path}?#{query_string}"
end
end
end
end
Loading